haiku/headers/private/netservices2/HttpTime.h
Niels Sascha Reedijk cf54474e38 NetServices: Implement BHttpTime, parse_http_time() and format_http_time().
These utilities convert timestamp strings that are formatted according to
the HTTP RFC into BDateTime objects, and vice versa.

Change-Id: Ia2498944fb63d09233839f19d08f15d82a0a9685
2022-05-08 06:59:51 +01:00

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_