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