| 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
|
28
|
|
|
|
|
|
static inline bool tstr_calendar_leap_year(int y) { |
|
13
|
28
|
100
|
|
|
|
|
return ((y & 3) == 0 && (y % 100 != 0 || y % 400 == 0)); |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
} |
|
15
|
|
|
|
|
|
|
|
|
16
|
46
|
|
|
|
|
|
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
|
46
|
100
|
|
|
|
|
if (m == 2 && tstr_calendar_leap_year(y)) |
|
|
|
100
|
|
|
|
|
|
|
20
|
5
|
|
|
|
|
|
return 29; |
|
21
|
41
|
|
|
|
|
|
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
|
216
|
|
|
|
|
|
static inline uint32_t tstr_calendar_ymd_to_rdn(int y, int m, int d) { |
|
31
|
216
|
100
|
|
|
|
|
if (m < 3) |
|
32
|
95
|
|
|
|
|
|
y--, m += 12; |
|
33
|
216
|
|
|
|
|
|
return (uint32_t)(1461 * y) / 4 - y / 100 + y / 400 |
|
34
|
216
|
|
|
|
|
|
+ d + ((979 * m - 2918) >> 5) - 306; |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
137
|
|
|
|
|
|
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
|
137
|
|
|
|
|
|
Z = rdn + 306; |
|
41
|
137
|
|
|
|
|
|
H = 100 * Z - 25; |
|
42
|
137
|
|
|
|
|
|
A = H / 3652425; |
|
43
|
137
|
|
|
|
|
|
B = A - A / 4; |
|
44
|
137
|
|
|
|
|
|
y = (100 * B + H) / 36525; |
|
45
|
137
|
|
|
|
|
|
C = B + Z - (1461 * y) / 4; |
|
46
|
137
|
|
|
|
|
|
m = (535 * C + 48950) >> 14; |
|
47
|
137
|
|
|
|
|
|
d = C - ((979 * m - 2918) >> 5); |
|
48
|
|
|
|
|
|
|
|
|
49
|
137
|
100
|
|
|
|
|
if (m > 12) |
|
50
|
24
|
|
|
|
|
|
y++, m -= 12; |
|
51
|
|
|
|
|
|
|
|
|
52
|
137
|
50
|
|
|
|
|
if (yp) |
|
53
|
137
|
|
|
|
|
|
*yp = (int)y; |
|
54
|
137
|
50
|
|
|
|
|
if (mp) |
|
55
|
137
|
|
|
|
|
|
*mp = (int)m; |
|
56
|
137
|
50
|
|
|
|
|
if (dp) |
|
57
|
137
|
|
|
|
|
|
*dp = (int)d; |
|
58
|
137
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
30
|
|
|
|
|
|
static inline int tstr_calendar_rdn_to_dow(uint32_t rdn) { |
|
61
|
30
|
|
|
|
|
|
return 1 + (rdn + 6) % 7; |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
|
|
64
|
24
|
|
|
|
|
|
static inline int tstr_calendar_ymd_to_dow(int y, int m, int d) { |
|
65
|
|
|
|
|
|
|
static const int kDayOffset[] = {0, 6, 2, 1, 4, 6, 2, 4, 0, 3, 5, 1, 3}; |
|
66
|
24
|
100
|
|
|
|
|
if (m < 3) |
|
67
|
10
|
|
|
|
|
|
y--; |
|
68
|
24
|
|
|
|
|
|
return 1 + (y + y / 4 - y / 100 + y / 400 + kDayOffset[m] + d) % 7; |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
17
|
|
|
|
|
|
static inline int tstr_calendar_resolve_century(int year, int pivot_year) { |
|
72
|
17
|
|
|
|
|
|
int century = pivot_year / 100; |
|
73
|
17
|
|
|
|
|
|
int base = century * 100; |
|
74
|
17
|
|
|
|
|
|
int pivot_offset = pivot_year - base; |
|
75
|
17
|
|
|
|
|
|
int resolved = base + year; |
|
76
|
17
|
100
|
|
|
|
|
if (year < pivot_offset) |
|
77
|
7
|
|
|
|
|
|
resolved += 100; |
|
78
|
17
|
|
|
|
|
|
return resolved; |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
#endif /* TSTR_CALENDAR_H */ |