aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/timezone_conversion/civil.cpp
blob: 8b3fea54944dcc2dd7170f50f3d92be67fe665bd (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
#include "civil.h"

#include <util/stream/output.h>
#include <util/stream/format.h>
#include <util/string/ascii.h>

namespace {
    bool TryParseInt(TStringBuf& s, int& dst, size_t maxDigits) {
        int res = 0;
        size_t i = 0;
        while (i < maxDigits && !s.empty() && IsAsciiDigit(s[0])) {
            res = res * 10 + (s[0] - '0');
            ++i;
            s.Skip(1);
        }
        if (i == 0) {
            return false;
        }
        dst = res;
        return true;
    }

    bool TryParseUTCOffsetTimezone(TStringBuf name, int& offset) {
        static constexpr TStringBuf OFFSET_PREFIX = "UTC";
        if (!name.SkipPrefix(OFFSET_PREFIX)) {
            return false;
        }
        if (name.empty()) {
            return false;
        }
        bool negative;
        if (name[0] == '+') {
            negative = false;
        } else if (name[0] == '-') {
            negative = true;
        } else {
            return false;
        }
        name.Skip(1);
        int hour;
        int minute = 0;
        if (!TryParseInt(name, hour, 2) || hour > 24) {
            return false;
        }
        if (!name.empty()) {
            if (name[0] == ':') {
                name.Skip(1);
            }
            if (!TryParseInt(name, minute, 2) || minute >= 60) {
                return false;
            }
            if (!name.empty()) {
                return false;
            }
        }
        if (hour == 24 && minute != 0) {
            return false;
        }
        offset = (hour * 60 + minute) * 60;
        if (negative)
            offset = -offset;
        return true;
    }
} // anonymous namespace

namespace NDatetime {
    TTimeZone GetTimeZone(TStringBuf name) {
        int offset;
        if (TryParseUTCOffsetTimezone(name, offset)) {
            return GetFixedTimeZone(offset);
        }
        TTimeZone result;
        if (!cctz::load_time_zone(static_cast<std::string>(name), &result)) { 
            ythrow TInvalidTimezone() << "Failed to load time zone " << name << ", " << result.name();
        }
        return result;
    }

    TTimeZone GetFixedTimeZone(const long offset) {
        return cctz::fixed_time_zone(std::chrono::seconds(offset));
    }

    TCivilSecond Convert(const TInstant& absTime, const TTimeZone& tz) {
        return cctz::convert(TSystemClock::from_time_t(absTime.TimeT()), tz);
    }

    TCivilSecond Convert(const TInstant& absTime, TStringBuf tzName) {
        TTimeZone tz = GetTimeZone(tzName);
        return cctz::convert(TSystemClock::from_time_t(absTime.TimeT()), tz);
    }

    TInstant Convert(const TCivilSecond& tp, const TTimeZone& tz) {
        return TInstant::Seconds(cctz::convert(tp, tz).time_since_epoch().count());
    }

    TCivilSecond AddYears(const TCivilSecond& tp, TDiff diff) {
        TCivilYear newYear = Calc<TCivilYear>(tp, diff);
        return NDatetime::TCivilSecond(newYear.year(), tp.month(), tp.day(), tp.hour(), tp.minute(), tp.second());
    }

    TCivilSecond AddMonths(const TCivilSecond& tp, TDiff diff) {
        TCivilMonth newMonth = Calc<TCivilMonth>(tp, diff);
        return NDatetime::TCivilSecond(newMonth.year(), newMonth.month(), tp.day(), tp.hour(), tp.minute(), tp.second());
    }

    TCivilSecond AddDays(const TCivilSecond& tp, TDiff diff) {
        TCivilDay newDay = Calc<TCivilDay>(tp, diff);
        return NDatetime::TCivilSecond(newDay.year(), newDay.month(), newDay.day(), tp.hour(), tp.minute(), tp.second());
    }

    TCivilSecond AddHours(const TCivilSecond& tp, TDiff diff) {
        TCivilHour newHour = Calc<TCivilHour>(tp, diff);
        return NDatetime::TCivilSecond(newHour.year(), newHour.month(), newHour.day(), newHour.hour(), tp.minute(), tp.second());
    }

    TCivilSecond AddMinutes(const TCivilSecond& tp, TDiff diff) {
        TCivilMinute newMinute = Calc<TCivilMinute>(tp, diff);
        return NDatetime::TCivilSecond(newMinute.year(), newMinute.month(), newMinute.day(), newMinute.hour(), newMinute.minute(), tp.second());
    }

    TCivilSecond AddSeconds(const TCivilSecond& tp, TDiff diff) {
        return Calc<TCivilSecond>(tp, diff);
    }

    TCivilSecond AddCivil(const TCivilSecond& tp, TCivilDiff diff) {
        switch (diff.Unit) {
            case ECivilUnit::Second: {
                return AddSeconds(tp, diff.Value);
            }
            case ECivilUnit::Minute: {
                return AddMinutes(tp, diff.Value);
            }
            case ECivilUnit::Hour: {
                return AddHours(tp, diff.Value);
            }
            case ECivilUnit::Day: {
                return AddDays(tp, diff.Value);
            }
            case ECivilUnit::Month: {
                return AddMonths(tp, diff.Value);
            }
            case ECivilUnit::Year: {
                return AddYears(tp, diff.Value);
            }
            default: {
                ythrow yexception() << "Unexpected civil unit value " << static_cast<int>(diff.Unit);
            }
        }
    }

    TCivilDiff GetCivilDiff(const TCivilSecond& tpX, const TCivilSecond& tpY, ECivilUnit unit) {
        switch (unit) {
            case ECivilUnit::Second: {
                return {tpX - tpY, unit};
            }
            case ECivilUnit::Minute: {
                return {static_cast<TCivilMinute>(tpX) - static_cast<TCivilMinute>(tpY), unit};
            }
            case ECivilUnit::Hour: {
                return {static_cast<TCivilHour>(tpX) - static_cast<TCivilHour>(tpY), unit};
            }
            case ECivilUnit::Day: {
                return {static_cast<TCivilDay>(tpX) - static_cast<TCivilDay>(tpY), unit};
            }
            case ECivilUnit::Month: {
                return {static_cast<TCivilMonth>(tpX) - static_cast<TCivilMonth>(tpY), unit};
            }
            case ECivilUnit::Year: {
                return {static_cast<TCivilYear>(tpX) - static_cast<TCivilYear>(tpY), unit};
            }
            default: {
                ythrow yexception() << "Unexpected civil unit value " << static_cast<int>(unit);
            }
        }
    }
}

template <>
void Out<NDatetime::TCivilYear>(IOutputStream& out, const NDatetime::TCivilYear& y) {
    out << y.year();
}

template <>
void Out<NDatetime::TCivilMonth>(IOutputStream& out, const NDatetime::TCivilMonth& m) {
    out << NDatetime::TCivilYear(m) << '-' << LeftPad(m.month(), 2, '0');
}

template <>
void Out<NDatetime::TCivilDay>(IOutputStream& out, const NDatetime::TCivilDay& d) {
    out << NDatetime::TCivilMonth(d) << '-' << LeftPad(d.day(), 2, '0');
}

template <>
void Out<NDatetime::TCivilHour>(IOutputStream& out, const NDatetime::TCivilHour& h) {
    out << NDatetime::TCivilDay(h) << 'T' << LeftPad(h.hour(), 2, '0');
}

template <>
void Out<NDatetime::TCivilMinute>(IOutputStream& out, const NDatetime::TCivilMinute& m) {
    out << NDatetime::TCivilHour(m) << ':' << LeftPad(m.minute(), 2, '0');
}

template <>
void Out<NDatetime::TCivilSecond>(IOutputStream& out, const NDatetime::TCivilSecond& s) {
    out << NDatetime::TCivilMinute(s) << ':' << LeftPad(s.second(), 2, '0');
}

template <>
void Out<NDatetime::TWeekday>(IOutputStream& out, NDatetime::TWeekday wd) {
    using namespace cctz;
    switch (wd) {
        case weekday::monday:
            out << TStringBuf("Monday");
            break;
        case weekday::tuesday:
            out << TStringBuf("Tuesday");
            break;
        case weekday::wednesday:
            out << TStringBuf("Wednesday");
            break;
        case weekday::thursday:
            out << TStringBuf("Thursday");
            break;
        case weekday::friday:
            out << TStringBuf("Friday");
            break;
        case weekday::saturday:
            out << TStringBuf("Saturday");
            break;
        case weekday::sunday:
            out << TStringBuf("Sunday");
            break;
    }
}