File Coverage

src/panda/time/basic.h
Criterion Covered Total %
statement 23 23 100.0
branch 6 6 100.0
condition n/a
subroutine n/a
pod n/a
total 29 29 100.0


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             typedef int64_t ptime_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             struct datetime {
16             ptime_t sec;
17             ptime_t min;
18             ptime_t hour;
19             ptime_t mday;
20             ptime_t mon;
21             int32_t yday;
22             int32_t wday;
23             int32_t year;
24             int32_t isdst;
25             int32_t gmtoff;
26             union {
27             char zone[ZONE_ABBR_MAX+1];
28             int64_t n_zone;
29             };
30             };
31             typedef datetime dt;
32              
33             const int DAYS_IN_MONTH [][12] = {
34             {31,28,31,30,31,30,31,31,30,31,30,31},
35             {31,29,31,30,31,30,31,31,30,31,30,31},
36             };
37              
38 62208           inline int is_leap_year (int32_t year) {
39 62208 100         return (year % 4) == 0 && ((year % 25) != 0 || (year % 16) == 0);
    100          
    100          
40             }
41              
42             inline int days_in_month (int32_t year, uint8_t month) {
43             return DAYS_IN_MONTH[is_leap_year(year)][month];
44             }
45              
46             // DAYS PASSED SINCE 1 Jan 0001 00:00:00 TILL 1 Jan 00:00:00
47 31340           inline ptime_t christ_days (int32_t year) {
48 31340           ptime_t yearpos = (ptime_t)year + 2147483999U;
49 31340           ptime_t ret = yearpos*365;
50 31340           yearpos >>= 2;
51 31340           ret += yearpos;
52 31340           yearpos /= 25;
53 31340           ret -= yearpos - (yearpos >> 2) + (ptime_t)146097*5368710;
54 31340           return ret;
55             }
56              
57 9           inline void dt2tm (tm& to, const datetime& from) {
58 9           to.tm_sec = from.sec;
59 9           to.tm_min = from.min;
60 9           to.tm_hour = from.hour;
61 9           to.tm_mday = from.mday;
62 9           to.tm_mon = from.mon;
63 9           to.tm_year = from.year-1900;
64 9           to.tm_isdst = from.isdst;
65 9           to.tm_wday = from.wday;
66 9           to.tm_yday = from.yday;
67             #ifndef _WIN32
68 9           to.tm_gmtoff = from.gmtoff;
69 9           to.tm_zone = const_cast(from.zone);
70             #endif
71 9           }
72              
73             inline void tm2dt (datetime& to, const tm& from) {
74             to.sec = from.tm_sec;
75             to.min = from.tm_min;
76             to.hour = from.tm_hour;
77             to.mday = from.tm_mday;
78             to.mon = from.tm_mon;
79             to.year = from.tm_year+1900;
80             to.isdst = from.tm_isdst;
81             to.wday = from.tm_wday;
82             to.yday = from.tm_yday;
83             #ifdef _WIN32
84             to.gmtoff = 0;
85             to.n_zone = 0;
86             #else
87             to.gmtoff = from.tm_gmtoff;
88             std::strncpy(to.zone, from.tm_zone, sizeof(to.zone));
89             to.zone[sizeof(to.zone)-1] = 0;
90             #endif
91             }
92              
93             }}