aboutsummaryrefslogtreecommitdiffstats
path: root/util/datetime/parser.h
blob: 43b6df8da22cdbdc0edda1c2de9d1e6e0a3153dd (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#pragma once

// probably you do not need to include this file directly, use "util/datetime/base.h"

#include "base.h"

struct TDateTimeFields {
    TDateTimeFields() {
        Zero(*this);
        ZoneOffsetMinutes = 0;
        Hour = 0;
    }

    ui32 Year;
    ui32 Month;       // 1..12
    ui32 Day;         // 1 .. 31
    ui32 Hour;        // 0 .. 23
    ui32 Minute;      // 0 .. 59
    ui32 Second;      // 0 .. 60
    ui32 MicroSecond; // 0 .. 999999
    i32 ZoneOffsetMinutes;

    void SetLooseYear(ui32 year) {
        if (year < 60)
            year += 100;
        if (year < 160)
            year += 1900;
        Year = year;
    }

    bool IsOk() const noexcept {
        if (Year < 1970)
            return false;
        if (Month < 1 || Month > 12)
            return false;

        unsigned int maxMonthDay = 31;
        if (Month == 4 || Month == 6 || Month == 9 || Month == 11) {
            maxMonthDay = 30;
        } else if (Month == 2) {
            if (Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0))
                // leap year
                maxMonthDay = 29;
            else
                maxMonthDay = 28;
        }
        if (Day > maxMonthDay)
            return false;

        if (Hour > 23)
            return false;

        if (Minute > 59)
            return false;

        // handle leap second which is explicitly allowed by ISO 8601:2004(E) $2.2.2
        // https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
        if (Second > 60)
            return false;

        if (MicroSecond > 999999)
            return false;

        if (Year == 1970 && Month == 1 && Day == 1) {
            if ((i64)(3600 * Hour + 60 * Minute + Second) < (60 * ZoneOffsetMinutes))
                return false;
        }

        return true;
    }

    TInstant ToInstant(TInstant defaultValue) const {
        time_t tt = ToTimeT(-1);
        if (tt == -1)
            return defaultValue;
        return TInstant::Seconds(tt) + TDuration::MicroSeconds(MicroSecond);
    }

    time_t ToTimeT(time_t defaultValue) const {
        if (!IsOk())
            return defaultValue;
        struct tm tm;
        Zero(tm);
        tm.tm_year = Year - 1900;
        tm.tm_mon = Month - 1;
        tm.tm_mday = Day;
        tm.tm_hour = Hour;
        tm.tm_min = Minute;
        tm.tm_sec = Second;
        time_t tt = TimeGM(&tm);
        if (tt == -1)
            return defaultValue;
        return tt - ZoneOffsetMinutes * 60;
    }
};

class TDateTimeParserBase {
public:
    const TDateTimeFields& GetDateTimeFields() const {
        return DateTimeFields;
    }

protected:
    TDateTimeFields DateTimeFields;
    int cs; // for ragel
    int Sign;
    int I;
    int Dc;

protected:
    TDateTimeParserBase()
        : DateTimeFields()
        , cs(0)
        , Sign(0)
        , I(0xDEADBEEF) // to guarantee unittest break if ragel code is incorrect
        , Dc(0xDEADBEEF)
    {
    }

    inline TInstant GetResult(int firstFinalState, TInstant defaultValue) const {
        if (cs < firstFinalState)
            return defaultValue;
        return DateTimeFields.ToInstant(defaultValue);
    }
};

#define DECLARE_PARSER(CLASS)                            \
    struct CLASS: public TDateTimeParserBase {           \
        CLASS();                                         \
        bool ParsePart(const char* input, size_t len);   \
        TInstant GetResult(TInstant defaultValue) const; \
    };

DECLARE_PARSER(TIso8601DateTimeParser)
DECLARE_PARSER(TRfc822DateTimeParser)
DECLARE_PARSER(THttpDateTimeParser)
DECLARE_PARSER(TX509ValidityDateTimeParser)
DECLARE_PARSER(TX509Validity4yDateTimeParser)

#undef DECLARE_PARSER

struct TDurationParser {
    int cs;

    ui64 I;
    ui32 Dc;

    i32 MultiplierPower; // 6 for seconds, 0 for microseconds, -3 for nanoseconds
    i32 Multiplier;
    ui64 IntegerPart;
    ui32 FractionPart;
    ui32 FractionDigits;

    TDurationParser();
    bool ParsePart(const char* input, size_t len);
    TDuration GetResult(TDuration defaultValue) const;
};

/**
Depcrecated cause of default hour offset (+4 hours)
@see IGNIETFERRO-823
*/
struct TDateTimeFieldsDeprecated {
    TDateTimeFieldsDeprecated() {
        Zero(*this);
        ZoneOffsetMinutes = (i32)TDuration::Hours(4).Minutes(); // legacy code
        Hour = 11;
    }

    ui32 Year;
    ui32 Month;       // 1..12
    ui32 Day;         // 1 .. 31
    ui32 Hour;        // 0 .. 23
    ui32 Minute;      // 0 .. 59
    ui32 Second;      // 0 .. 60
    ui32 MicroSecond; // 0 .. 999999
    i32 ZoneOffsetMinutes;

    void SetLooseYear(ui32 year) {
        if (year < 60)
            year += 100;
        if (year < 160)
            year += 1900;
        Year = year;
    }

    bool IsOk() const noexcept {
        if (Year < 1970)
            return false;
        if (Month < 1 || Month > 12)
            return false;

        unsigned int maxMonthDay = 31;
        if (Month == 4 || Month == 6 || Month == 9 || Month == 11) {
            maxMonthDay = 30;
        } else if (Month == 2) {
            if (Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0))
                // leap year
                maxMonthDay = 29;
            else
                maxMonthDay = 28;
        }
        if (Day > maxMonthDay)
            return false;

        if (Hour > 23)
            return false;

        if (Minute > 59)
            return false;

        if (Second > 60)
            return false;

        if (MicroSecond > 999999)
            return false;

        if (Year == 1970 && Month == 1 && Day == 1) {
            if ((i64)(3600 * Hour + 60 * Minute + Second) < (60 * ZoneOffsetMinutes))
                return false;
        }

        return true;
    }

    TInstant ToInstant(TInstant defaultValue) const {
        time_t tt = ToTimeT(-1);
        if (tt == -1)
            return defaultValue;
        return TInstant::Seconds(tt) + TDuration::MicroSeconds(MicroSecond);
    }

    time_t ToTimeT(time_t defaultValue) const {
        if (!IsOk())
            return defaultValue;
        struct tm tm;
        Zero(tm);
        tm.tm_year = Year - 1900;
        tm.tm_mon = Month - 1;
        tm.tm_mday = Day;
        tm.tm_hour = Hour;
        tm.tm_min = Minute;
        tm.tm_sec = Second;
        time_t tt = TimeGM(&tm);
        if (tt == -1)
            return defaultValue;
        return tt - ZoneOffsetMinutes * 60;
    }
};

class TDateTimeParserBaseDeprecated {
public:
    const TDateTimeFieldsDeprecated& GetDateTimeFields() const {
        return DateTimeFields;
    }

protected:
    TDateTimeFieldsDeprecated DateTimeFields;
    int cs; // for ragel
    int Sign;
    int I;
    int Dc;

protected:
    TDateTimeParserBaseDeprecated()
        : DateTimeFields()
        , cs(0)
        , Sign(0)
        , I(0xDEADBEEF) // to guarantee unittest break if ragel code is incorrect
        , Dc(0xDEADBEEF)
    {
    }

    inline TInstant GetResult(int firstFinalState, TInstant defaultValue) const {
        if (cs < firstFinalState)
            return defaultValue;
        return DateTimeFields.ToInstant(defaultValue);
    }
};

#define DECLARE_PARSER(CLASS)                            \
    struct CLASS: public TDateTimeParserBaseDeprecated { \
        CLASS();                                         \
        bool ParsePart(const char* input, size_t len);   \
        TInstant GetResult(TInstant defaultValue) const; \
    };

DECLARE_PARSER(TIso8601DateTimeParserDeprecated)
DECLARE_PARSER(TRfc822DateTimeParserDeprecated)
DECLARE_PARSER(THttpDateTimeParserDeprecated)
DECLARE_PARSER(TX509ValidityDateTimeParserDeprecated)
DECLARE_PARSER(TX509Validity4yDateTimeParserDeprecated)

#undef DECLARE_PARSER