aboutsummaryrefslogtreecommitdiffstats
path: root/util/draft/datetime.cpp
blob: 401d6eda1eaa8f0078bbfb58c232d4c95c196f45 (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
#include "datetime.h" 
 
#include <util/ysaveload.h>

#include <util/system/atomic.h> 
#include <util/system/fasttime.h> 
#include <util/datetime/base.h> 
#include <util/datetime/systime.h> 
#include <util/stream/output.h>
#include <util/stream/mem.h>
#include <util/string/cast.h>
#include <util/string/printf.h>
 
namespace NDatetime { 
    const ui32 MonthDays[2][12] = { 
        {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, //nleap
        {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}  //leap
    }; 
 
    const ui32 MonthDaysNewYear[2][13] = {
        {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}, //nleap
        {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}  //leap
    }; 
 
    void YDayToMonthAndDay(ui32 yday, bool isleap, ui32* month, ui32* mday) {
        const ui32* begin = MonthDaysNewYear[isleap] + 1;
        const ui32* end = begin + 12;
        // [31, ..., 365] or [31, ..., 366] (12 elements)

        const ui32* pos = UpperBound(begin, end, yday);
        Y_ENSURE(pos != end, "day no. " << yday << " does not exist in " << (isleap ? "leap" : "non-leap") << " year");

        *month = pos - begin;
        *mday = yday - *(pos - 1) + 1;

        Y_ASSERT((*month < 12) && (1 <= *mday) && (*mday <= MonthDays[isleap][*month]));
    }

    struct TTimeData { 
        i32 IsDst = 0; 
        i32 GMTOff = 0; 
 
        TTimeData(time_t t) { 
            struct ::tm tt; 
            ::localtime_r(&t, &tt); 
#ifndef _win_ 
            GMTOff = tt.tm_gmtoff; 
#else
            TIME_ZONE_INFORMATION tz; 
            switch (GetTimeZoneInformation(&tz)) { 
                case TIME_ZONE_ID_UNKNOWN: 
                    GMTOff = tz.Bias * -60; 
                    break; 
                case TIME_ZONE_ID_STANDARD: 
                    GMTOff = (tz.Bias + tz.StandardBias) * -60; 
                    break; 
                case TIME_ZONE_ID_DAYLIGHT: 
                    GMTOff = (tz.Bias + tz.DaylightBias) * -60; 
                    break; 
                default: 
                    break; 
            } 
#endif
            IsDst = tt.tm_isdst; 
        }
    };

    TSimpleTM TSimpleTM::CurrentUTC() { 
        return New((time_t)TInstant::MicroSeconds(InterpolatedMicroSeconds()).Seconds()); 
    } 
 
    TSimpleTM TSimpleTM::New(time_t t, i32 gmtoff, i8 isdst) { 
        time_t tt = t + gmtoff + isdst * 3600; 
        struct tm tmSys; 
        Zero(tmSys); 
        GmTimeR(&tt, &tmSys); 
        tmSys.tm_isdst = isdst; 
#ifndef _win_ 
        tmSys.tm_gmtoff = gmtoff; 
#endif 
 
        return New(tmSys); 
    }
 
    TSimpleTM TSimpleTM::NewLocal(time_t t) { 
        TTimeData d(t); 
        return New(t, d.GMTOff, d.IsDst); 
    }
 
    TSimpleTM TSimpleTM::New(const struct tm& t) {
        TSimpleTM res;
        res.IsDst = t.tm_isdst; 
        res.Sec = t.tm_sec;
        res.Min = t.tm_min;
        res.Hour = t.tm_hour;
        res.WDay = t.tm_wday;
        res.Mon = t.tm_mon;
        res.MDay = t.tm_mday;
        res.Year = t.tm_year;
        res.YDay = t.tm_yday;
        res.IsLeap = LeapYearAD(res.Year + 1900); 
#ifndef _win_ 
        res.GMTOff = t.tm_gmtoff;
#endif 
        return res;
    }
 
    TSimpleTM& TSimpleTM::SetRealDate(ui32 year, ui32 mon, ui32 mday, ui32 hour, ui32 min, ui32 sec, i32 isdst) { 
        mday = ::Max<ui32>(mday, 1); 
        mon = ::Min<ui32>(::Max<ui32>(mon, 1), 12); 
        year = ::Max<ui32>(year, 1900); 
 
        IsLeap = LeapYearAD(year); 
        Year = year - 1900; 
        Mon = mon - 1; 
        MDay = ::Min<ui32>(mday, MonthDays[IsLeap][Mon]); 
        Hour = Max<ui32>() == hour ? Hour : ::Min<ui32>(hour, 23); 
        Min = Max<ui32>() == min ? Min : ::Min<ui32>(min, 59); 
        Sec = Max<ui32>() == sec ? Sec : ::Min<ui32>(sec, 60); 
        IsDst = isdst; 
 
        return RegenerateFields(); 
    } 
 
    TSimpleTM& TSimpleTM::RegenerateFields() { 
        return *this = New(AsTimeT(), GMTOff, IsDst);
    } 
 
    TSimpleTM& TSimpleTM::Add(EField f, i32 amount) {
        if (!amount) {
            return *this;
        }
 
        switch (f) {
            default:
                return *this;
            case F_DAY:
                amount *= 24;
                [[fallthrough]];
            case F_HOUR:
                amount *= 60;
                [[fallthrough]];
            case F_MIN:
                amount *= 60;
                [[fallthrough]];
            case F_SEC: { 
                return *this = New(AsTimeT() + amount, GMTOff, IsDst);
            } 
            case F_YEAR: {
                i32 y = amount + (i32)Year;
                y = ::Min<i32>(Max<i32>(y, 0), 255 /*max year*/);
 
                // YDay may correspond to different MDay if it's March or greater and the years have different leap status
                if (Mon > 1) {
                    YDay += (i32)LeapYearAD(RealYear()) - (i32)LeapYearAD(RealYear()); 
                }
 
                Year = y;
                IsLeap = LeapYearAD(RealYear()); 
                return RegenerateFields(); 
            }
            case F_MON: {
                i32 m = amount + Mon;
                i32 y = (m < 0 ? (-12 + m) : m) / 12;
                m = m - y * 12;
 
                if (y) { 
                    Add(F_YEAR, y);
                } 
 
                if (m >= 0 && m < 12) {
                    MDay = ::Min<ui32>(MonthDays[IsLeap][m], MDay);
                    Mon = m; 
                }

                return RegenerateFields(); 
            }
        } 
    } 

    TString TSimpleTM::ToString(const char* fmt) const {
        struct tm t = *this;
        return Strftime(fmt, &t); 
    } 
 
    time_t TSimpleTM::AsTimeT() const { 
        struct tm t = AsStructTmLocal(); 
        return TimeGM(&t) - GMTOff - IsDst * 3600; 
    }
 
    struct tm TSimpleTM::AsStructTmUTC() const { 
        struct tm res; 
        Zero(res); 
        time_t t = AsTimeT(); 
        return *GmTimeR(&t, &res); 
    } 
 
    struct tm TSimpleTM::AsStructTmLocal() const { 
        struct tm t;
        Zero(t);
        t.tm_isdst = IsDst;
        t.tm_sec = Sec;
        t.tm_min = Min;
        t.tm_hour = Hour;
        t.tm_wday = WDay;
        t.tm_mon = Mon;
        t.tm_mday = MDay;
        t.tm_year = Year;
        t.tm_yday = YDay;
#ifndef _win_ 
        t.tm_gmtoff = GMTOff;
#endif 
        return t;
    }
} 

template <>
void In<TMonth>(IInputStream& in, TMonth& t) {
    char buf[4];
    LoadPodArray(&in, buf, 4);
    t.Year = FromString<ui16>(buf, 4);
    LoadPodArray(&in, buf, 2);
    t.Month = ui8(FromString<ui16>(buf, 2)) - 1;
}

template <>
void Out<TMonth>(IOutputStream& o, const TMonth& t) {
    o << t.Year << Sprintf("%.2hu", (ui16)(t.Month + 1));
}

template <>
TMonth FromStringImpl<TMonth, char>(const char* s, size_t len) {
    TMonth res;
    TMemoryInput in(s, len);
    in >> res;
    return res;
}