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