File Coverage

tstr_time.h
Criterion Covered Total %
statement 47 49 95.9
branch 52 56 92.8
condition n/a
subroutine n/a
pod n/a
total 99 105 94.2


line stmt bran cond sub pod time code
1             #ifndef TSTR_TIME_H
2             #define TSTR_TIME_H
3              
4             #include
5             #include
6             #include "tstr_calendar.h"
7              
8             #define TSTR_TIME_EPOCH_MIN INT64_C(-62135596800)
9             #define TSTR_TIME_EPOCH_MAX INT64_C(253402300799)
10              
11 11           static inline bool tstr_time_valid_hms(int h, int m, int s) {
12 10 100         return (h >= 0 && h <= 23)
13 9 100         && (m >= 0 && m <= 59)
    100          
14 21 100         && (s >= 0 && s <= 59);
    100          
    100          
15             }
16              
17 9           static inline bool tstr_time_valid_hms60(int h, int m, int s) {
18 8 100         return (h >= 0 && h <= 23)
19 7 100         && (m >= 0 && m <= 59)
    100          
20 17 100         && (s >= 0 && s <= 60);
    100          
    100          
21             }
22              
23             // Validates that a folded leap-second epoch (the 23:59:59 that a
24             // 23:59:60 leap second folds onto) lands on a real leap-second slot:
25             // exactly 23:59:60 UTC on June 30 or December 31, year >= 1972. The
26             // check is table-free: every such date is accepted, whether or not a
27             // leap second was actually inserted. Returns 0 on success, 1 if the
28             // instant is not 23:59:60 UTC, or 2 if the UTC date is not valid.
29 18           static inline int tstr_time_leap_check(int64_t epoch) {
30 18           int64_t days = epoch / 86400;
31 18           int64_t sod = epoch - days * 86400;
32 18 50         if (sod < 0) {
33 0           sod += 86400;
34 0           days--;
35             }
36 18 100         if (sod != 86399)
37 3           return 1;
38             int y, m, d;
39 15           tstr_calendar_rdn_to_ymd((uint32_t)(days + TSTR_CALENDAR_RDN_UNIX_EPOCH),
40             &y, &m, &d);
41 15 100         if (!(y >= 1972 && ((m == 6 && d == 30) || (m == 12 && d == 31))))
    100          
    50          
    100          
    50          
42 3           return 2;
43 12           return 0;
44             }
45              
46 296           static inline int64_t tstr_time_timegm(int y,
47             int m,
48             int d,
49             int H,
50             int M,
51             int S) {
52 296           uint32_t rdn = tstr_calendar_ymd_to_rdn(y, m, d);
53 296           int64_t sod = (int64_t)H * 3600 + (int64_t)M * 60 + S;
54 296           return ((int64_t)rdn - TSTR_CALENDAR_RDN_UNIX_EPOCH) * 86400 + sod;
55             }
56              
57 133           static inline void tstr_time_gmtime(int64_t epoch,
58             int* yp,
59             int* mp,
60             int* dp,
61             int* Hp,
62             int* Mp,
63             int* Sp,
64             int* wdayp,
65             int* ydayp) {
66             int y, m, d;
67 133           int64_t days = epoch / 86400;
68 133           int sod = (int)(epoch - days * 86400);
69 133 100         if (sod < 0) {
70 2           sod += 86400;
71 2           days--;
72             }
73 133           uint32_t rdn = (uint32_t)(days + TSTR_CALENDAR_RDN_UNIX_EPOCH);
74 133           tstr_calendar_rdn_to_ymd(rdn, &y, &m, &d);
75 133 50         if (yp)
76 133           *yp = y;
77 133 100         if (mp)
78 19           *mp = m;
79 133 100         if (dp)
80 19           *dp = d;
81 133 100         if (Hp)
82 19           *Hp = sod / 3600;
83 133 100         if (Mp)
84 19           *Mp = (sod % 3600) / 60;
85 133 100         if (Sp)
86 19           *Sp = sod % 60;
87 133 100         if (wdayp)
88 19           *wdayp = tstr_calendar_rdn_to_dow(rdn) % 7;
89 133 100         if (ydayp)
90 19           *ydayp = tstr_calendar_ymd_to_doy(y, m, d) - 1;
91 133           }
92              
93             #endif /* TSTR_TIME_H */