aboutsummaryrefslogtreecommitdiffstats
path: root/util/datetime/base.cpp
blob: f0df9b1b4006c39d631b973f40e2b9cea904f2e3 (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
#include "base.h"

#include <util/string/cast.h>
#include <util/stream/output.h>
#include <util/stream/mem.h>
#include <util/system/compat.h>
#include <util/memory/tempbuf.h>
#include <util/generic/string.h> 
#include <util/generic/strbuf.h>
#include <util/generic/yexception.h>

TString Strftime(const char* format, const struct tm* tm) { 
    size_t size = Max<size_t>(strlen(format) * 2 + 1, 107);
    for (;;) {
        TTempBuf buf(size);
        int r = strftime(buf.Data(), buf.Size(), format, tm);
        if (r != 0) {
            return TString(buf.Data(), r); 
        }
        size *= 2;
    }
}

template <>
TDuration FromStringImpl<TDuration, char>(const char* s, size_t len) {
    return TDuration::Parse(TStringBuf(s, len));
}

template <>
bool TryFromStringImpl<TDuration, char>(const char* s, size_t len, TDuration& result) {
    return TDuration::TryParse(TStringBuf(s, len), result);
}

namespace {
    template <size_t N>
    struct TPad {
        int I;
    };

    template <size_t N>
    inline TPad<N> Pad(int i) {
        return {i};
    }

    inline IOutputStream& operator<<(IOutputStream& o, const TPad<2>& p) {
        if (p.I < 10) {
            if (p.I >= 0) {
                o << '0';
            }
        }

        return o << p.I;
    }

    inline IOutputStream& operator<<(IOutputStream& o, const TPad<4>& p) {
        if (p.I < 1000) {
            if (p.I >= 0) {
                if (p.I < 10) {
                    o << '0' << '0' << '0';
                } else if (p.I < 100) {
                    o << '0' << '0';
                } else {
                    o << '0';
                }
            }
        }

        return o << p.I;
    }

    inline IOutputStream& operator<<(IOutputStream& o, const TPad<6>& p) {
        if (p.I < 100000) {
            if (p.I >= 0) {
                if (p.I < 10) {
                    o << '0' << '0' << '0' << '0' << '0';
                } else if (p.I < 100) {
                    o << '0' << '0' << '0' << '0';
                } else if (p.I < 1000) {
                    o << '0' << '0' << '0';
                } else if (p.I < 10000) {
                    o << '0' << '0';
                } else {
                    o << '0';
                }
            }
        }

        return o << p.I;
    }

    void WriteMicroSecondsToStream(IOutputStream& os, ui32 microSeconds) {
        os << '.' << Pad<6>(microSeconds);
    }

    void WriteTmToStream(IOutputStream& os, const struct tm& theTm) {
        os << Pad<4>(theTm.tm_year + 1900) << '-' << Pad<2>(theTm.tm_mon + 1) << '-' << Pad<2>(theTm.tm_mday) << 'T'
           << Pad<2>(theTm.tm_hour) << ':' << Pad<2>(theTm.tm_min) << ':' << Pad<2>(theTm.tm_sec);
    }

    template <bool PrintUpToSeconds, bool iso>
    void WritePrintableLocalTimeToStream(IOutputStream& os, const ::NPrivate::TPrintableLocalTime<PrintUpToSeconds, iso>& timeToPrint) {
        const TInstant& momentToPrint = timeToPrint.MomentToPrint;
        struct tm localTime;
        momentToPrint.LocalTime(&localTime);
        WriteTmToStream(os, localTime);
        if (!PrintUpToSeconds) {
            WriteMicroSecondsToStream(os, momentToPrint.MicroSecondsOfSecond());
        }
#ifndef _win_
        i64 utcOffsetInMinutes = localTime.tm_gmtoff / 60;
#else
        TIME_ZONE_INFORMATION tz;
        if (GetTimeZoneInformation(&tz) == TIME_ZONE_ID_INVALID) {
            ythrow TSystemError() << "Failed to get the system time zone";
        }
        i64 utcOffsetInMinutes = -tz.Bias;
#endif
        if (utcOffsetInMinutes == 0) {
            os << 'Z';
        } else {
            if (utcOffsetInMinutes < 0) {
                os << '-';
                utcOffsetInMinutes = -utcOffsetInMinutes;
            } else {
                os << '+';
            }
            os << Pad<2>(utcOffsetInMinutes / 60);
            if (iso) {
                os << ':';
            }
            os << Pad<2>(utcOffsetInMinutes % 60);
        }
    }
}

template <>
void Out<TDuration>(IOutputStream& os, TTypeTraits<TDuration>::TFuncParam duration) {
    os << duration.Seconds();
    WriteMicroSecondsToStream(os, duration.MicroSecondsOfSecond());
    os << 's';
}

template <>
void Out<TInstant>(IOutputStream& os, TTypeTraits<TInstant>::TFuncParam instant) {
    char buf[64];
    auto len = FormatDate8601(buf, sizeof(buf), instant.TimeT());

    // shouldn't happen due to current implementation of FormatDate8601() and GmTimeR()
    Y_ENSURE(len, TStringBuf("Out<TInstant>: year does not fit into an integer"));

    os.Write(buf, len - 1 /* 'Z' */);
    WriteMicroSecondsToStream(os, instant.MicroSecondsOfSecond());
    os << 'Z';
}

template <>
void Out<::NPrivate::TPrintableLocalTime<false, false>>(IOutputStream& os, TTypeTraits<::NPrivate::TPrintableLocalTime<false, false>>::TFuncParam localTime) {
    WritePrintableLocalTimeToStream(os, localTime);
}

template <>
void Out<::NPrivate::TPrintableLocalTime<false, true>>(IOutputStream& os, TTypeTraits<::NPrivate::TPrintableLocalTime<false, true>>::TFuncParam localTime) {
    WritePrintableLocalTimeToStream(os, localTime);
}

template <>
void Out<::NPrivate::TPrintableLocalTime<true, false>>(IOutputStream& os, TTypeTraits<::NPrivate::TPrintableLocalTime<true, false>>::TFuncParam localTime) {
    WritePrintableLocalTimeToStream(os, localTime);
}

template <>
void Out<::NPrivate::TPrintableLocalTime<true, true>>(IOutputStream& os, TTypeTraits<::NPrivate::TPrintableLocalTime<true, true>>::TFuncParam localTime) {
    WritePrintableLocalTimeToStream(os, localTime);
}

TString TDuration::ToString() const { 
    return ::ToString(*this);
}

TString TInstant::ToString() const { 
    return ::ToString(*this);
}

TString TInstant::ToRfc822String() const {
    return FormatGmTime("%a, %d %b %Y %H:%M:%S GMT");
}

TString TInstant::ToStringUpToSeconds() const { 
    char buf[64];
    auto len = FormatDate8601(buf, sizeof(buf), TimeT());
    if (!len) {
        ythrow yexception() << "TInstant::ToStringUpToSeconds: year does not fit into an integer";
    }
    return TString(buf, len); 
}

TString TInstant::ToIsoStringLocal() const {
    return ::ToString(FormatIsoLocal(*this));
}

TString TInstant::ToStringLocal() const { 
    return ::ToString(FormatLocal(*this));
}

TString TInstant::ToRfc822StringLocal() const {
    return FormatLocalTime("%a, %d %b %Y %H:%M:%S %Z");
}

TString TInstant::ToIsoStringLocalUpToSeconds() const {
    return ::ToString(FormatIsoLocalUpToSeconds(*this));
}

TString TInstant::ToStringLocalUpToSeconds() const { 
    return ::ToString(FormatLocalUpToSeconds(*this));
}

TString TInstant::FormatLocalTime(const char* format) const noexcept {
    struct tm theTm;
    LocalTime(&theTm);
    return Strftime(format, &theTm);
}

TString TInstant::FormatGmTime(const char* format) const noexcept {
    struct tm theTm;
    GmTime(&theTm);
    return Strftime(format, &theTm);
}

::NPrivate::TPrintableLocalTime<false, true> FormatIsoLocal(TInstant instant) {
    return ::NPrivate::TPrintableLocalTime<false, true>(instant);
}

::NPrivate::TPrintableLocalTime<false, false> FormatLocal(TInstant instant) {
    return ::NPrivate::TPrintableLocalTime<false, false>(instant);
}

::NPrivate::TPrintableLocalTime<true, true> FormatIsoLocalUpToSeconds(TInstant instant) {
    return ::NPrivate::TPrintableLocalTime<true, true>(instant);
}

::NPrivate::TPrintableLocalTime<true, false> FormatLocalUpToSeconds(TInstant instant) {
    return ::NPrivate::TPrintableLocalTime<true, false>(instant);
}

void Sleep(TDuration duration) {
    NanoSleep(duration.NanoSeconds());
}

void sprint_gm_date(char* buf, time_t when, long* sec) {
    struct tm theTm;
    ::Zero(theTm);
    GmTimeR(&when, &theTm);
    DateToString(buf, theTm);
    if (sec) {
        *sec = seconds(theTm);
    }
}

void DateToString(char* buf, const struct tm& theTm) {
    Y_ENSURE(0 <= theTm.tm_year + 1900 && theTm.tm_year + 1900 <= 9999, "invalid year " + ToString(theTm.tm_year + 1900) + ", year should be in range [0, 9999]");

    sprintf(buf, "%04d%02d%02d", theTm.tm_year + 1900, theTm.tm_mon + 1, theTm.tm_mday);
}

void DateToString(char* buf, time_t when, long* sec) {
    struct tm theTm;
    localtime_r(&when, &theTm);

    DateToString(buf, theTm);

    if (sec) {
        *sec = seconds(theTm);
    }
}

TString DateToString(const struct tm& theTm) {
    char buf[DATE_BUF_LEN];
    DateToString(buf, theTm);
    return buf;
}

TString DateToString(time_t when, long* sec) {
    char buf[DATE_BUF_LEN];
    DateToString(buf, when, sec);
    return buf;
}

TString YearToString(const struct tm& theTm) {
    Y_ENSURE(0 <= theTm.tm_year + 1900 && theTm.tm_year + 1900 <= 9999, "invalid year " + ToString(theTm.tm_year + 1900) + ", year should be in range [0, 9999]");
    char buf[16];
    sprintf(buf, "%04d", theTm.tm_year + 1900);
    return buf;
}

TString YearToString(time_t when) {
    struct tm theTm;
    localtime_r(&when, &theTm);

    return YearToString(theTm);
}

bool sscan_date(const char* date, struct tm& theTm) {
    int year, mon, mday;
    if (sscanf(date, "%4d%2d%2d", &year, &mon, &mday) != 3) {
        return false;
    }
    theTm.tm_year = year - 1900;
    theTm.tm_mon = mon - 1;
    theTm.tm_mday = mday;
    return true;
}

size_t FormatDate8601(char* buf, size_t len, time_t when) {
    struct tm theTm;
    struct tm* ret = GmTimeR(&when, &theTm);

    if (ret) {
        TMemoryOutput out(buf, len);

        WriteTmToStream(out, theTm);
        out << 'Z';

        return out.Buf() - buf;
    }

    return 0;
}

void SleepUntil(TInstant instant) {
    TInstant now = TInstant::Now();
    if (instant <= now) {
        return;
    }
    TDuration duration = instant - now;
    Sleep(duration);
}