1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2013, 2014 Damien P. George
7  * Copyright (c) 2015 Daniel Campora
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25  * THE SOFTWARE.
26  */
27 #ifndef MICROPY_INCLUDED_LIB_TIMEUTILS_TIMEUTILS_H
28 #define MICROPY_INCLUDED_LIB_TIMEUTILS_TIMEUTILS_H
29 
30 // Modified bt HaaS begin
31 #include <stdbool.h>
32 
33 #include "py/mpconfig.h"
34 
35 // Modified bt HaaS end
36 
37 // The number of seconds between 1970/1/1 and 2000/1/1 is calculated using:
38 // time.mktime((2000,1,1,0,0,0,0,0,0)) - time.mktime((1970,1,1,0,0,0,0,0,0))
39 #define TIMEUTILS_SECONDS_1970_TO_2000 (946684800ULL)
40 
41 typedef struct _timeutils_struct_time_t {
42     uint16_t tm_year;       // i.e. 2014
43     uint8_t tm_mon;         // 1..12
44     uint8_t tm_mday;        // 1..31
45     uint8_t tm_hour;        // 0..23
46     uint8_t tm_min;         // 0..59
47     uint8_t tm_sec;         // 0..59
48     uint8_t tm_wday;        // 0..6  0 = Monday
49     uint16_t tm_yday;       // 1..366
50 } timeutils_struct_time_t;
51 
52 bool timeutils_is_leap_year(mp_uint_t year);
53 mp_uint_t timeutils_days_in_month(mp_uint_t year, mp_uint_t month);
54 mp_uint_t timeutils_year_day(mp_uint_t year, mp_uint_t month, mp_uint_t date);
55 
56 void timeutils_seconds_since_2000_to_struct_time(mp_uint_t t,
57     timeutils_struct_time_t *tm);
58 
59 mp_uint_t timeutils_seconds_since_2000(mp_uint_t year, mp_uint_t month,
60     mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second);
61 
62 mp_uint_t timeutils_mktime_2000(mp_uint_t year, mp_int_t month, mp_int_t mday,
63     mp_int_t hours, mp_int_t minutes, mp_int_t seconds);
64 
65 // Select the Epoch used by the port.
66 #if MICROPY_EPOCH_IS_1970
67 
timeutils_seconds_since_epoch_to_struct_time(uint64_t t,timeutils_struct_time_t * tm)68 static inline void timeutils_seconds_since_epoch_to_struct_time(uint64_t t, timeutils_struct_time_t *tm) {
69     // TODO this will give incorrect results for dates before 2000/1/1
70     return timeutils_seconds_since_2000_to_struct_time(t - TIMEUTILS_SECONDS_1970_TO_2000, tm);
71 }
72 
timeutils_mktime(mp_uint_t year,mp_int_t month,mp_int_t mday,mp_int_t hours,mp_int_t minutes,mp_int_t seconds)73 static inline uint64_t timeutils_mktime(mp_uint_t year, mp_int_t month, mp_int_t mday, mp_int_t hours, mp_int_t minutes, mp_int_t seconds) {
74     return timeutils_mktime_2000(year, month, mday, hours, minutes, seconds) + TIMEUTILS_SECONDS_1970_TO_2000;
75 }
76 
timeutils_seconds_since_epoch(mp_uint_t year,mp_uint_t month,mp_uint_t date,mp_uint_t hour,mp_uint_t minute,mp_uint_t second)77 static inline uint64_t timeutils_seconds_since_epoch(mp_uint_t year, mp_uint_t month,
78     mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second) {
79     return timeutils_seconds_since_2000(year, month, date, hour, minute, second) + TIMEUTILS_SECONDS_1970_TO_2000;
80 }
81 
timeutils_seconds_since_epoch_from_nanoseconds_since_1970(uint64_t ns)82 static inline mp_uint_t timeutils_seconds_since_epoch_from_nanoseconds_since_1970(uint64_t ns) {
83     return ns / 1000000000ULL;
84 }
85 
timeutils_nanoseconds_since_epoch_to_nanoseconds_since_1970(uint64_t ns)86 static inline uint64_t timeutils_nanoseconds_since_epoch_to_nanoseconds_since_1970(uint64_t ns) {
87     return ns;
88 }
89 
90 #else // Epoch is 2000
91 
92 #define timeutils_seconds_since_epoch_to_struct_time timeutils_seconds_since_2000_to_struct_time
93 #define timeutils_seconds_since_epoch timeutils_seconds_since_2000
94 #define timeutils_mktime timeutils_mktime_2000
95 
timeutils_seconds_since_epoch_to_nanoseconds_since_1970(mp_uint_t s)96 static inline uint64_t timeutils_seconds_since_epoch_to_nanoseconds_since_1970(mp_uint_t s) {
97     return ((uint64_t)s + TIMEUTILS_SECONDS_1970_TO_2000) * 1000000000ULL;
98 }
99 
timeutils_seconds_since_epoch_from_nanoseconds_since_1970(uint64_t ns)100 static inline mp_uint_t timeutils_seconds_since_epoch_from_nanoseconds_since_1970(uint64_t ns) {
101     return ns / 1000000000ULL - TIMEUTILS_SECONDS_1970_TO_2000;
102 }
103 
timeutils_nanoseconds_since_epoch_to_nanoseconds_since_1970(int64_t ns)104 static inline int64_t timeutils_nanoseconds_since_epoch_to_nanoseconds_since_1970(int64_t ns) {
105     return ns + TIMEUTILS_SECONDS_1970_TO_2000 * 1000000000ULL;
106 }
107 
108 #endif
109 
110 int timeutils_calc_weekday(int y, int m, int d);
111 
112 #endif // MICROPY_INCLUDED_LIB_TIMEUTILS_TIMEUTILS_H
113