| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
#ifndef TSTR_CALENDAR_H |
|
2
|
|
|
|
|
|
|
#define TSTR_CALENDAR_H |
|
3
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
#include |
|
5
|
|
|
|
|
|
|
#include |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
#define TSTR_CALENDAR_RDN_MIN 1 /* 0001-01-01 */ |
|
8
|
|
|
|
|
|
|
#define TSTR_CALENDAR_RDN_MAX 3652059 /* 9999-12-31 */ |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#define TSTR_CALENDAR_RDN_UNIX_EPOCH 719163 // 1970-01-01 |
|
11
|
|
|
|
|
|
|
|
|
12
|
164
|
|
|
|
|
|
static inline bool tstr_calendar_leap_year(int y) { |
|
13
|
164
|
100
|
|
|
|
|
return ((y & 3) == 0 && (y % 100 != 0 || y % 400 == 0)); |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
} |
|
15
|
|
|
|
|
|
|
|
|
16
|
534
|
|
|
|
|
|
static inline int tstr_calendar_month_days(int y, int m) { |
|
17
|
|
|
|
|
|
|
static const int kDays[] = {0, 31, 28, 31, 30, 31, 30, |
|
18
|
|
|
|
|
|
|
31, 31, 30, 31, 30, 31}; |
|
19
|
534
|
100
|
|
|
|
|
if (m == 2 && tstr_calendar_leap_year(y)) |
|
|
|
100
|
|
|
|
|
|
|
20
|
15
|
|
|
|
|
|
return 29; |
|
21
|
519
|
|
|
|
|
|
return kDays[m]; |
|
22
|
|
|
|
|
|
|
} |
|
23
|
|
|
|
|
|
|
|
|
24
|
43
|
|
|
|
|
|
static inline bool tstr_calendar_valid_ymd(int y, int m, int d) { |
|
25
|
42
|
100
|
|
|
|
|
return (y >= 1 && y <= 9999) |
|
26
|
41
|
100
|
|
|
|
|
&& (m >= 1 && m <= 12) |
|
|
|
100
|
|
|
|
|
|
|
27
|
85
|
100
|
|
|
|
|
&& (d >= 1 && (d <= 28 || d <= tstr_calendar_month_days(y, m))); |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
} |
|
29
|
|
|
|
|
|
|
|
|
30
|
612
|
|
|
|
|
|
static inline uint32_t tstr_calendar_ymd_to_rdn(int y, int m, int d) { |
|
31
|
612
|
100
|
|
|
|
|
if (m < 3) |
|
32
|
159
|
|
|
|
|
|
y--, m += 12; |
|
33
|
612
|
|
|
|
|
|
return (uint32_t)(1461 * y) / 4 - y / 100 + y / 400 |
|
34
|
612
|
|
|
|
|
|
+ d + ((979 * m - 2918) >> 5) - 306; |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
240
|
|
|
|
|
|
static inline void tstr_calendar_rdn_to_ymd(uint32_t rdn, int* yp, int* mp, int* dp) { |
|
38
|
|
|
|
|
|
|
uint32_t Z, H, A, B, y, C, m, d; |
|
39
|
|
|
|
|
|
|
|
|
40
|
240
|
|
|
|
|
|
Z = rdn + 306; |
|
41
|
240
|
|
|
|
|
|
H = 100 * Z - 25; |
|
42
|
240
|
|
|
|
|
|
A = H / 3652425; |
|
43
|
240
|
|
|
|
|
|
B = A - A / 4; |
|
44
|
240
|
|
|
|
|
|
y = (100 * B + H) / 36525; |
|
45
|
240
|
|
|
|
|
|
C = B + Z - (1461 * y) / 4; |
|
46
|
240
|
|
|
|
|
|
m = (535 * C + 48950) >> 14; |
|
47
|
240
|
|
|
|
|
|
d = C - ((979 * m - 2918) >> 5); |
|
48
|
|
|
|
|
|
|
|
|
49
|
240
|
100
|
|
|
|
|
if (m > 12) |
|
50
|
61
|
|
|
|
|
|
y++, m -= 12; |
|
51
|
|
|
|
|
|
|
|
|
52
|
240
|
50
|
|
|
|
|
if (yp) |
|
53
|
240
|
|
|
|
|
|
*yp = (int)y; |
|
54
|
240
|
50
|
|
|
|
|
if (mp) |
|
55
|
240
|
|
|
|
|
|
*mp = (int)m; |
|
56
|
240
|
50
|
|
|
|
|
if (dp) |
|
57
|
240
|
|
|
|
|
|
*dp = (int)d; |
|
58
|
240
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
43
|
|
|
|
|
|
static inline int tstr_calendar_ymd_to_doy(int y, int m, int d) { |
|
61
|
|
|
|
|
|
|
static const int kCumDays[] = {0, 0, 31, 59, 90, 120, 151, |
|
62
|
|
|
|
|
|
|
181, 212, 243, 273, 304, 334}; |
|
63
|
43
|
100
|
|
|
|
|
return kCumDays[m] + d + (m > 2 && tstr_calendar_leap_year(y)); |
|
|
|
100
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
|
|
66
|
90
|
|
|
|
|
|
static inline void tstr_calendar_yd_to_md(int y, int doy, int *mp, int *dp) { |
|
67
|
90
|
|
|
|
|
|
int m, d, jan_feb = 59 + tstr_calendar_leap_year(y); |
|
68
|
90
|
100
|
|
|
|
|
if (doy <= 31) { |
|
69
|
8
|
|
|
|
|
|
m = 1; |
|
70
|
8
|
|
|
|
|
|
d = doy; |
|
71
|
82
|
100
|
|
|
|
|
} else if (doy <= jan_feb) { |
|
72
|
9
|
|
|
|
|
|
m = 2; |
|
73
|
9
|
|
|
|
|
|
d = doy - 31; |
|
74
|
|
|
|
|
|
|
} else { |
|
75
|
73
|
|
|
|
|
|
int C = doy - jan_feb; |
|
76
|
73
|
|
|
|
|
|
m = (535 * C + 48950) >> 14; |
|
77
|
73
|
|
|
|
|
|
d = C - ((979 * m - 2918) >> 5); |
|
78
|
|
|
|
|
|
|
} |
|
79
|
90
|
50
|
|
|
|
|
if (mp) |
|
80
|
90
|
|
|
|
|
|
*mp = m; |
|
81
|
90
|
50
|
|
|
|
|
if (dp) |
|
82
|
90
|
|
|
|
|
|
*dp = d; |
|
83
|
90
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
49
|
|
|
|
|
|
static inline int tstr_calendar_rdn_to_dow(uint32_t rdn) { |
|
86
|
49
|
|
|
|
|
|
return 1 + (rdn + 6) % 7; |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
348
|
|
|
|
|
|
static inline int tstr_calendar_ymd_to_dow(int y, int m, int d) { |
|
90
|
|
|
|
|
|
|
static const int kDayOffset[] = {0, 6, 2, 1, 4, 6, 2, 4, 0, 3, 5, 1, 3}; |
|
91
|
348
|
100
|
|
|
|
|
if (m < 3) |
|
92
|
32
|
|
|
|
|
|
y--; |
|
93
|
348
|
|
|
|
|
|
return 1 + (y + y / 4 - y / 100 + y / 400 + kDayOffset[m] + d) % 7; |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
|
|
96
|
296
|
|
|
|
|
|
static inline int tstr_calendar_nth_dow_in_month(int y, int m, int ord, int dow) { |
|
97
|
296
|
100
|
|
|
|
|
if (ord > 0) { |
|
98
|
201
|
|
|
|
|
|
int first_dow = tstr_calendar_ymd_to_dow(y, m, 1); |
|
99
|
201
|
|
|
|
|
|
return 1 + (dow - first_dow + 7) % 7 + (ord - 1) * 7; |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
else { |
|
102
|
95
|
|
|
|
|
|
int mdays = tstr_calendar_month_days(y, m); |
|
103
|
95
|
|
|
|
|
|
int last_dow = tstr_calendar_ymd_to_dow(y, m, mdays); |
|
104
|
95
|
|
|
|
|
|
return mdays - (last_dow - dow + 7) % 7 + (ord + 1) * 7; |
|
105
|
|
|
|
|
|
|
} |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
|
|
108
|
17
|
|
|
|
|
|
static inline int tstr_calendar_resolve_century(int year, int pivot_year) { |
|
109
|
17
|
|
|
|
|
|
int century = pivot_year / 100; |
|
110
|
17
|
|
|
|
|
|
int base = century * 100; |
|
111
|
17
|
|
|
|
|
|
int pivot_offset = pivot_year - base; |
|
112
|
17
|
|
|
|
|
|
int resolved = base + year; |
|
113
|
17
|
100
|
|
|
|
|
if (year < pivot_offset) |
|
114
|
7
|
|
|
|
|
|
resolved += 100; |
|
115
|
17
|
|
|
|
|
|
return resolved; |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
#endif /* TSTR_CALENDAR_H */ |