cf54474e38
These utilities convert timestamp strings that are formatted according to the HTTP RFC into BDateTime objects, and vice versa. Change-Id: Ia2498944fb63d09233839f19d08f15d82a0a9685
72 lines
1.4 KiB
C++
72 lines
1.4 KiB
C++
/*
|
|
* Copyright 2010-2022 Haiku Inc. All rights reserved.
|
|
* Distributed under the terms of the MIT License.
|
|
*/
|
|
#ifndef _B_HTTP_TIME_H_
|
|
#define _B_HTTP_TIME_H_
|
|
|
|
#include <ctime>
|
|
|
|
#include <DateTime.h>
|
|
#include <ErrorsExt.h>
|
|
#include <String.h>
|
|
|
|
namespace BPrivate {
|
|
|
|
namespace Network {
|
|
|
|
enum class BHttpTimeFormat : int8 {
|
|
RFC1123 = 0,
|
|
RFC850,
|
|
AscTime
|
|
};
|
|
|
|
|
|
class BHttpTime {
|
|
public:
|
|
// Error type
|
|
class InvalidInput : public BError {
|
|
public:
|
|
InvalidInput(const char* origin, BString input);
|
|
|
|
virtual const char* Message() const noexcept override;
|
|
virtual BString DebugMessage() const override;
|
|
|
|
BString input;
|
|
};
|
|
|
|
// Constructors
|
|
BHttpTime() noexcept;
|
|
BHttpTime(BDateTime date);
|
|
BHttpTime(const BString& dateString);
|
|
|
|
// Date modification
|
|
void SetTo(const BString& string);
|
|
void SetTo(BDateTime date);
|
|
|
|
|
|
// Date Access
|
|
BDateTime DateTime() const noexcept;
|
|
BHttpTimeFormat DateTimeFormat() const noexcept;
|
|
BString ToString(BHttpTimeFormat outputFormat = BHttpTimeFormat::RFC1123) const;
|
|
|
|
private:
|
|
void _Parse(const BString& dateString);
|
|
|
|
BDateTime fDate;
|
|
BHttpTimeFormat fDateFormat;
|
|
};
|
|
|
|
|
|
// Convenience functions
|
|
BDateTime parse_http_time(const BString& string);
|
|
BString format_http_time(BDateTime timestamp,
|
|
BHttpTimeFormat outputFormat = BHttpTimeFormat::RFC1123);
|
|
|
|
|
|
} // namespace Network
|
|
|
|
} // namespace BPrivate
|
|
|
|
#endif // _B_HTTP_TIME_H_
|