line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#pragma once |
2
|
|
|
|
|
|
|
#include |
3
|
|
|
|
|
|
|
#include |
4
|
|
|
|
|
|
|
#include |
5
|
|
|
|
|
|
|
#include |
6
|
|
|
|
|
|
|
#include |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
namespace panda { namespace time { |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
using ptime_t = int64_t; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
const size_t ZONE_ABBR_MAX = 7; /* max length of local type abbrev name (MSK, EST, EDT, ...) */ |
13
|
|
|
|
|
|
|
const size_t ZONE_ABBR_MIN = 3; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
/* |
16
|
|
|
|
|
|
|
epoch max is calculated for gmtime(s=59,m=59,h=23,mday=31,y=2**31-1) - 24 hours for possible tz offsets |
17
|
|
|
|
|
|
|
epoch min is calculated for gmtime(s=00,m=00,h=00,mday=01,y=-2**31) + 24 hours for possible tz offsets |
18
|
|
|
|
|
|
|
*/ |
19
|
|
|
|
|
|
|
static const constexpr ptime_t EPOCH_MAX = 67767976233446399l; |
20
|
|
|
|
|
|
|
static const constexpr ptime_t EPOCH_MIN = -67768100567884800l; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
struct datetime { |
23
|
|
|
|
|
|
|
ptime_t sec; |
24
|
|
|
|
|
|
|
ptime_t min; |
25
|
|
|
|
|
|
|
ptime_t hour; |
26
|
|
|
|
|
|
|
ptime_t mday; |
27
|
|
|
|
|
|
|
ptime_t mon; |
28
|
|
|
|
|
|
|
int32_t yday; |
29
|
|
|
|
|
|
|
int32_t wday; |
30
|
|
|
|
|
|
|
int32_t year; |
31
|
|
|
|
|
|
|
int32_t isdst; |
32
|
|
|
|
|
|
|
int32_t gmtoff; |
33
|
|
|
|
|
|
|
union { |
34
|
|
|
|
|
|
|
char zone[ZONE_ABBR_MAX+1]; |
35
|
|
|
|
|
|
|
int64_t n_zone; |
36
|
|
|
|
|
|
|
}; |
37
|
|
|
|
|
|
|
}; |
38
|
|
|
|
|
|
|
using dt = datetime; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
const int DAYS_IN_MONTH [][12] = { |
41
|
|
|
|
|
|
|
{31,28,31,30,31,30,31,31,30,31,30,31}, |
42
|
|
|
|
|
|
|
{31,29,31,30,31,30,31,31,30,31,30,31}, |
43
|
|
|
|
|
|
|
}; |
44
|
|
|
|
|
|
|
|
45
|
62214
|
|
|
|
|
|
inline int is_leap_year (int32_t year) { |
46
|
62214
|
100
|
|
|
|
|
return (year % 4) == 0 && ((year % 25) != 0 || (year % 16) == 0); |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
inline int days_in_month (int32_t year, uint8_t month) { |
50
|
|
|
|
|
|
|
return DAYS_IN_MONTH[is_leap_year(year)][month]; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
// DAYS PASSED SINCE 1 Jan 0001 00:00:00 TILL 1 Jan 00:00:00 |
54
|
31341
|
|
|
|
|
|
inline ptime_t christ_days (int32_t year) { |
55
|
31341
|
|
|
|
|
|
ptime_t yearpos = (ptime_t)year + 2147483999U; |
56
|
31341
|
|
|
|
|
|
ptime_t ret = yearpos*365; |
57
|
31341
|
|
|
|
|
|
yearpos >>= 2; |
58
|
31341
|
|
|
|
|
|
ret += yearpos; |
59
|
31341
|
|
|
|
|
|
yearpos /= 25; |
60
|
31341
|
|
|
|
|
|
ret -= yearpos - (yearpos >> 2) + (ptime_t)146097*5368710; |
61
|
31341
|
|
|
|
|
|
return ret; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
11
|
|
|
|
|
|
inline void dt2tm (tm& to, const datetime& from) { |
65
|
11
|
|
|
|
|
|
to.tm_sec = from.sec; |
66
|
11
|
|
|
|
|
|
to.tm_min = from.min; |
67
|
11
|
|
|
|
|
|
to.tm_hour = from.hour; |
68
|
11
|
|
|
|
|
|
to.tm_mday = from.mday; |
69
|
11
|
|
|
|
|
|
to.tm_mon = from.mon; |
70
|
11
|
|
|
|
|
|
to.tm_year = from.year-1900; |
71
|
11
|
|
|
|
|
|
to.tm_isdst = from.isdst; |
72
|
11
|
|
|
|
|
|
to.tm_wday = from.wday; |
73
|
11
|
|
|
|
|
|
to.tm_yday = from.yday; |
74
|
|
|
|
|
|
|
#ifndef _WIN32 |
75
|
11
|
|
|
|
|
|
to.tm_gmtoff = from.gmtoff; |
76
|
11
|
|
|
|
|
|
to.tm_zone = const_cast(from.zone); |
77
|
|
|
|
|
|
|
#endif |
78
|
11
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
inline void tm2dt (datetime& to, const tm& from) { |
81
|
|
|
|
|
|
|
to.sec = from.tm_sec; |
82
|
|
|
|
|
|
|
to.min = from.tm_min; |
83
|
|
|
|
|
|
|
to.hour = from.tm_hour; |
84
|
|
|
|
|
|
|
to.mday = from.tm_mday; |
85
|
|
|
|
|
|
|
to.mon = from.tm_mon; |
86
|
|
|
|
|
|
|
to.year = from.tm_year+1900; |
87
|
|
|
|
|
|
|
to.isdst = from.tm_isdst; |
88
|
|
|
|
|
|
|
to.wday = from.tm_wday; |
89
|
|
|
|
|
|
|
to.yday = from.tm_yday; |
90
|
|
|
|
|
|
|
#ifdef _WIN32 |
91
|
|
|
|
|
|
|
to.gmtoff = 0; |
92
|
|
|
|
|
|
|
to.n_zone = 0; |
93
|
|
|
|
|
|
|
#else |
94
|
|
|
|
|
|
|
to.gmtoff = from.tm_gmtoff; |
95
|
|
|
|
|
|
|
std::strncpy(to.zone, from.tm_zone, sizeof(to.zone)); |
96
|
|
|
|
|
|
|
to.zone[sizeof(to.zone)-1] = 0; |
97
|
|
|
|
|
|
|
#endif |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
}} |