blob: 2528223443e6a4a27777d3f60377ba7810c07bf7 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
#pragma once
#include <Core/Types.h>
namespace DB
{
class ReadBuffer;
class WriteBuffer;
/// Proleptic Gregorian calendar date.
class GregorianDate
{
public:
GregorianDate() {}
void init(ReadBuffer & in);
bool tryInit(ReadBuffer & in);
/** Construct from date in text form 'YYYY-MM-DD' by reading from
* ReadBuffer.
*/
explicit GregorianDate(ReadBuffer & in);
void init(int64_t modified_julian_day);
bool tryInit(int64_t modified_julian_day);
/** Construct from Modified Julian Day. The type T is an
* integral type which should be at least 32 bits wide, and
* should preferably signed.
*/
explicit GregorianDate(int64_t modified_julian_day);
/** Convert to Modified Julian Day. The type T is an integral type
* which should be at least 32 bits wide, and should preferably
* signed.
*/
int64_t toModifiedJulianDay() const;
bool tryToModifiedJulianDay(int64_t & res) const;
/** Write the date in text form 'YYYY-MM-DD' to a buffer.
*/
void write(WriteBuffer & buf) const
{
writeImpl<void>(buf);
}
bool tryWrite(WriteBuffer & buf) const
{
return writeImpl<bool>(buf);
}
/** Convert to a string in text form 'YYYY-MM-DD'.
*/
std::string toString() const;
int32_t year() const noexcept
{
return year_;
}
uint8_t month() const noexcept
{
return month_;
}
uint8_t dayOfMonth() const noexcept
{
return day_of_month_;
}
private:
int32_t year_ = 0;
uint8_t month_ = 0;
uint8_t day_of_month_ = 0;
template <typename ReturnType>
ReturnType writeImpl(WriteBuffer & buf) const;
};
/** ISO 8601 Ordinal Date.
*/
class OrdinalDate
{
public:
OrdinalDate() {}
void init(int32_t year, uint16_t day_of_year);
bool tryInit(int32_t year, uint16_t day_of_year);
void init(int64_t modified_julian_day);
bool tryInit(int64_t modified_julian_day);
OrdinalDate(int32_t year, uint16_t day_of_year);
/** Construct from Modified Julian Day. The type T is an
* integral type which should be at least 32 bits wide, and
* should preferably signed.
*/
explicit OrdinalDate(int64_t modified_julian_day);
/** Convert to Modified Julian Day. The type T is an integral
* type which should be at least 32 bits wide, and should
* preferably be signed.
*/
int64_t toModifiedJulianDay() const noexcept;
int32_t year() const noexcept
{
return year_;
}
uint16_t dayOfYear() const noexcept
{
return day_of_year_;
}
private:
int32_t year_ = 0;
uint16_t day_of_year_ = 0;
};
class MonthDay
{
public:
/** Construct from month and day. */
MonthDay(uint8_t month, uint8_t day_of_month);
/** Construct from day of year in Gregorian or Julian
* calendars to month and day.
*/
MonthDay(bool is_leap_year, uint16_t day_of_year);
/** Convert month and day in Gregorian or Julian calendars to
* day of year.
*/
uint16_t dayOfYear(bool is_leap_year) const;
uint8_t month() const noexcept
{
return month_;
}
uint8_t dayOfMonth() const noexcept
{
return day_of_month_;
}
private:
uint8_t month_ = 0;
uint8_t day_of_month_ = 0;
};
}
|