xref: /haiku/headers/private/netservices2/HttpTime.h (revision 52c4471a3024d2eb81fe88e2c3982b9f8daa5e56)
1 /*
2  * Copyright 2010-2022 Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _B_HTTP_TIME_H_
6 #define _B_HTTP_TIME_H_
7 
8 #include <ctime>
9 
10 #include <DateTime.h>
11 #include <ErrorsExt.h>
12 #include <String.h>
13 
14 namespace BPrivate {
15 
16 namespace Network {
17 
18 enum class BHttpTimeFormat : int8 { RFC1123 = 0, RFC850, AscTime };
19 
20 
21 class BHttpTime
22 {
23 public:
24 	// Error type
25 	class InvalidInput;
26 
27 	// Constructors
28 								BHttpTime() noexcept;
29 								BHttpTime(BDateTime date);
30 								BHttpTime(const BString& dateString);
31 
32 	// Date modification
33 			void				SetTo(const BString& string);
34 			void				SetTo(BDateTime date);
35 
36 
37 	// Date Access
38 			BDateTime			DateTime() const noexcept;
39 			BHttpTimeFormat		DateTimeFormat() const noexcept;
40 			BString				ToString(BHttpTimeFormat outputFormat
41 									= BHttpTimeFormat::RFC1123) const;
42 
43 private:
44 			void				_Parse(const BString& dateString);
45 
46 			BDateTime			fDate;
47 			BHttpTimeFormat		fDateFormat;
48 };
49 
50 
51 class BHttpTime::InvalidInput : public BError
52 {
53 public:
54 								InvalidInput(const char* origin, BString input);
55 
56 	virtual	const char*			Message() const noexcept override;
57 	virtual	BString				DebugMessage() const override;
58 
59 			BString				input;
60 };
61 
62 
63 // Convenience functions
64 BDateTime parse_http_time(const BString& string);
65 BString format_http_time(BDateTime timestamp,
66 	BHttpTimeFormat outputFormat = BHttpTimeFormat::RFC1123);
67 
68 
69 } // namespace Network
70 
71 } // namespace BPrivate
72 
73 #endif // _B_HTTP_TIME_H_
74