aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/GregorianDate.cpp
blob: f28194781c222f9981ddacb899a6a4b5e2800c12 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <Functions/GregorianDate.h>

#include <Common/Exception.h>
#include <IO/ReadBuffer.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteBufferFromString.h>
#include <IO/WriteHelpers.h>


namespace DB
{

namespace ErrorCodes
{
    extern const int CANNOT_PARSE_INPUT_ASSERTION_FAILED;
    extern const int CANNOT_PARSE_DATE;
    extern const int CANNOT_FORMAT_DATETIME;
    extern const int LOGICAL_ERROR;
}

namespace
{
    inline constexpr bool is_leap_year(int32_t year)
    {
        return (year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0));
    }

    inline constexpr uint8_t monthLength(bool is_leap_year, uint8_t month)
    {
        switch (month)
        {
        case  1: return 31;
        case  2: return is_leap_year ? 29 : 28;
        case  3: return 31;
        case  4: return 30;
        case  5: return 31;
        case  6: return 30;
        case  7: return 31;
        case  8: return 31;
        case  9: return 30;
        case 10: return 31;
        case 11: return 30;
        case 12: return 31;
        default:
            std::terminate();
        }
    }

    /** Integer division truncated toward negative infinity.
      */
    template <typename I, typename J>
    inline constexpr I div(I x, J y)
    {
        const auto y_cast = static_cast<I>(y);
        if (x > 0 && y_cast < 0)
            return ((x - 1) / y_cast) - 1;
        else if (x < 0 && y_cast > 0)
            return ((x + 1) / y_cast) - 1;
        else
            return x / y_cast;
    }

    /** Integer modulus, satisfying div(x, y)*y + mod(x, y) == x.
      */
    template <typename I, typename J>
    inline constexpr I mod(I x, J y)
    {
        const auto y_cast = static_cast<I>(y);
        const auto r = x % y_cast;
        if ((x > 0 && y_cast < 0) || (x < 0 && y_cast > 0))
            return r == 0 ? static_cast<I>(0) : r + y_cast;
        else
            return r;
    }

    /** Like std::min(), but the type of operands may differ.
      */
    template <typename I, typename J>
    inline constexpr I min(I x, J y)
    {
        const auto y_cast = static_cast<I>(y);
        return x < y_cast ? x : y_cast;
    }

    inline char readDigit(ReadBuffer & in)
    {
        char c;
        if (!in.read(c))
            throw Exception(ErrorCodes::CANNOT_PARSE_INPUT_ASSERTION_FAILED, "Cannot parse input: expected a digit at the end of stream");
        else if (c < '0' || c > '9')
            throw Exception(ErrorCodes::CANNOT_PARSE_INPUT_ASSERTION_FAILED, "Cannot read input: expected a digit but got something else");
        else
            return c - '0';
    }

    inline bool tryReadDigit(ReadBuffer & in, char & c)
    {
        if (in.read(c) && c >= '0' && c <= '9')
        {
            c -= '0';
            return true;
        }

        return false;
    }
}

void GregorianDate::init(ReadBuffer & in)
{
    year_ = readDigit(in) * 1000
          + readDigit(in) * 100
          + readDigit(in) * 10
          + readDigit(in);

    assertChar('-', in);

    month_ = readDigit(in) * 10
           + readDigit(in);

    assertChar('-', in);

    day_of_month_ = readDigit(in) * 10
                + readDigit(in);

    assertEOF(in);

    if (month_ < 1 || month_ > 12 || day_of_month_ < 1 || day_of_month_ > monthLength(is_leap_year(year_), month_))
        throw Exception(ErrorCodes::CANNOT_PARSE_DATE, "Invalid date, out of range (year: {}, month: {}, day_of_month: {}).");
}

bool GregorianDate::tryInit(ReadBuffer & in)
{
    char c[8];

    if (   !tryReadDigit(in, c[0])
        || !tryReadDigit(in, c[1])
        || !tryReadDigit(in, c[2])
        || !tryReadDigit(in, c[3])
        || !checkChar('-', in)
        || !tryReadDigit(in, c[4])
        || !tryReadDigit(in, c[5])
        || !checkChar('-', in)
        || !tryReadDigit(in, c[6])
        || !tryReadDigit(in, c[7])
        || !in.eof())
    {
        return false;
    }

    year_ = c[0] * 1000 + c[1] * 100 + c[2] * 10 + c[3];
    month_ = c[4] * 10 + c[5];
    day_of_month_ = c[6] * 10 + c[7];

    if (month_ < 1 || month_ > 12 || day_of_month_ < 1 || day_of_month_ > monthLength(is_leap_year(year_), month_))
        return false;

    return true;
}

GregorianDate::GregorianDate(ReadBuffer & in)
{
    init(in);
}

void GregorianDate::init(int64_t modified_julian_day)
{
    const OrdinalDate ord(modified_julian_day);
    const MonthDay md(is_leap_year(ord.year()), ord.dayOfYear());

    year_  = ord.year();
    month_ = md.month();
    day_of_month_ = md.dayOfMonth();
}

bool GregorianDate::tryInit(int64_t modified_julian_day)
{
    OrdinalDate ord;
    if (!ord.tryInit(modified_julian_day))
        return false;

    MonthDay md(is_leap_year(ord.year()), ord.dayOfYear());

    year_  = ord.year();
    month_ = md.month();
    day_of_month_ = md.dayOfMonth();

    return true;
}

GregorianDate::GregorianDate(int64_t modified_julian_day)
{
    init(modified_julian_day);
}

int64_t GregorianDate::toModifiedJulianDay() const
{
    const MonthDay md(month_, day_of_month_);

    const auto day_of_year = md.dayOfYear(is_leap_year(year_));

    const OrdinalDate ord(year_, day_of_year);
    return ord.toModifiedJulianDay();
}

bool GregorianDate::tryToModifiedJulianDay(int64_t & res) const
{
    const MonthDay md(month_, day_of_month_);
    const auto day_of_year = md.dayOfYear(is_leap_year(year_));
    OrdinalDate ord;

    if (!ord.tryInit(year_, day_of_year))
        return false;

    res = ord.toModifiedJulianDay();
    return true;
}

template <typename ReturnType>
ReturnType GregorianDate::writeImpl(WriteBuffer & buf) const
{
    if (year_ < 0 || year_ > 9999)
    {
        if constexpr (std::is_same_v<ReturnType, void>)
            throw Exception(ErrorCodes::CANNOT_FORMAT_DATETIME,
                "Impossible to stringify: year too big or small: {}", year_);
        else
            return false;
    }
    else
    {
        auto y = year_;
        writeChar('0' + y / 1000, buf); y %= 1000;
        writeChar('0' + y /  100, buf); y %=  100;
        writeChar('0' + y /   10, buf); y %=   10;
        writeChar('0' + y       , buf);

        writeChar('-', buf);

        auto m = month_;
        writeChar('0' + m / 10, buf); m %= 10;
        writeChar('0' + m     , buf);

        writeChar('-', buf);

        auto d = day_of_month_;
        writeChar('0' + d / 10, buf); d %= 10;
        writeChar('0' + d     , buf);
    }

    return ReturnType(true);
}

std::string GregorianDate::toString() const
{
    WriteBufferFromOwnString buf;
    write(buf);
    return buf.str();
}

void OrdinalDate::init(int32_t year, uint16_t day_of_year)
{
    year_ = year;
    day_of_year_ = day_of_year;

    if (day_of_year < 1 || day_of_year > (is_leap_year(year) ? 366 : 365))
        throw Exception(ErrorCodes::LOGICAL_ERROR, "Invalid ordinal date: {}-{}", year, day_of_year);
}

bool OrdinalDate::tryInit(int32_t year, uint16_t day_of_year)
{
    year_ = year;
    day_of_year_ = day_of_year;

    return !(day_of_year < 1 || day_of_year > (is_leap_year(year) ? 366 : 365));
}

void OrdinalDate::init(int64_t modified_julian_day)
{
    if (!tryInit(modified_julian_day))
        throw Exception(
            ErrorCodes::CANNOT_FORMAT_DATETIME,
            "Value cannot be represented as date because it's out of range");
}

bool OrdinalDate::tryInit(int64_t modified_julian_day)
{
    /// This function supports day number from -678941 to 2973119 (which represent 0000-01-01 and 9999-12-31 respectively).

    if (modified_julian_day < -678941)
        return false;

    if (modified_julian_day > 2973119)
        return false;

    const auto a         = modified_julian_day + 678575;
    const auto quad_cent = div(a, 146097);
    const auto b         = mod(a, 146097);
    const auto cent      = min(div(b, 36524), 3);
    const auto c         = b - cent * 36524;
    const auto quad      = div(c, 1461);
    const auto d         = mod(c, 1461);
    const auto y         = min(div(d, 365), 3);

    day_of_year_ = d - y * 365 + 1;
    year_ = static_cast<int32_t>(quad_cent * 400 + cent * 100 + quad * 4 + y + 1);

    return true;
}


OrdinalDate::OrdinalDate(int32_t year, uint16_t day_of_year)
{
    init(year, day_of_year);
}

OrdinalDate::OrdinalDate(int64_t modified_julian_day)
{
    init(modified_julian_day);
}

int64_t OrdinalDate::toModifiedJulianDay() const noexcept
{
    const auto y = year_ - 1;

    return day_of_year_
        + 365 * y
        + div(y, 4)
        - div(y, 100)
        + div(y, 400)
        - 678576;
}

MonthDay::MonthDay(uint8_t month, uint8_t day_of_month)
    : month_(month)
    , day_of_month_(day_of_month)
{
    if (month < 1 || month > 12)
        throw Exception(ErrorCodes::LOGICAL_ERROR, "Invalid month: {}", month);
    /* We can't validate day_of_month here, because we don't know if
     * it's a leap year. */
}

MonthDay::MonthDay(bool is_leap_year, uint16_t day_of_year)
{
    if (day_of_year < 1 || day_of_year > (is_leap_year ? 366 : 365))
        throw Exception(ErrorCodes::LOGICAL_ERROR, "Invalid day of year: {}{}",
                        (is_leap_year ? "leap, " : "non-leap, "), day_of_year);

    month_ = 1;
    uint16_t d = day_of_year;
    while (true)
    {
        const auto len = monthLength(is_leap_year, month_);
        if (d <= len)
            break;
        ++month_;
        d -= len;
    }
    day_of_month_ = d;
}

uint16_t MonthDay::dayOfYear(bool is_leap_year) const
{
    if (day_of_month_ < 1 || day_of_month_ > monthLength(is_leap_year, month_))
    {
        throw Exception(ErrorCodes::LOGICAL_ERROR, "Invalid day of month: {}{}-{}",
            (is_leap_year ? "leap, " : "non-leap, "), month_, day_of_month_);
    }
    const auto k = month_ <= 2 ? 0 : is_leap_year ? -1 :-2;
    return (367 * month_ - 362) / 12 + k + day_of_month_;
}

template void GregorianDate::writeImpl<void>(WriteBuffer & buf) const;
template bool GregorianDate::writeImpl<bool>(WriteBuffer & buf) const;

}