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// The number of seconds between 1970/1/1 and 2000/1/1 is calculated using:
31// time.mktime((2000,1,1,0,0,0,0,0,0)) - time.mktime((1970,1,1,0,0,0,0,0,0))
32#define TIMEUTILS_SECONDS_1970_TO_2000 (946684800ULL)
33
34typedef struct _timeutils_struct_time_t {
35 uint16_t tm_year; // i.e. 2014
36 uint8_t tm_mon; // 1..12
37 uint8_t tm_mday; // 1..31
38 uint8_t tm_hour; // 0..23
39 uint8_t tm_min; // 0..59
40 uint8_t tm_sec; // 0..59
41 uint8_t tm_wday; // 0..6 0 = Monday
42 uint16_t tm_yday; // 1..366
43} timeutils_struct_time_t;
44
45bool timeutils_is_leap_year(mp_uint_t year);
46mp_uint_t timeutils_days_in_month(mp_uint_t year, mp_uint_t month);
47mp_uint_t timeutils_year_day(mp_uint_t year, mp_uint_t month, mp_uint_t date);
48
49void timeutils_seconds_since_2000_to_struct_time(mp_uint_t t,
50 timeutils_struct_time_t *tm);
51
52mp_uint_t timeutils_seconds_since_2000(mp_uint_t year, mp_uint_t month,
53 mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second);
54
55mp_uint_t timeutils_mktime_2000(mp_uint_t year, mp_int_t month, mp_int_t mday,
56 mp_int_t hours, mp_int_t minutes, mp_int_t seconds);
57
58// Select the Epoch used by the port.
59#if MICROPY_EPOCH_IS_1970
60
61static inline void timeutils_seconds_since_epoch_to_struct_time(uint64_t t, timeutils_struct_time_t *tm) {
62 // TODO this will give incorrect results for dates before 2000/1/1
63 return timeutils_seconds_since_2000_to_struct_time(t - TIMEUTILS_SECONDS_1970_TO_2000, tm);
64}
65
66static 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) {
67 return timeutils_mktime_2000(year, month, mday, hours, minutes, seconds) + TIMEUTILS_SECONDS_1970_TO_2000;
68}
69
70static inline uint64_t timeutils_seconds_since_epoch(mp_uint_t year, mp_uint_t month,
71 mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second) {
72 return timeutils_seconds_since_2000(year, month, date, hour, minute, second) + TIMEUTILS_SECONDS_1970_TO_2000;
73}
74
75static inline mp_uint_t timeutils_seconds_since_epoch_from_nanoseconds_since_1970(uint64_t ns) {
76 return ns / 1000000000ULL;
77}
78
79static inline uint64_t timeutils_nanoseconds_since_epoch_to_nanoseconds_since_1970(uint64_t ns) {
80 return ns;
81}
82
83#else // Epoch is 2000
84
85#define timeutils_seconds_since_epoch_to_struct_time timeutils_seconds_since_2000_to_struct_time
86#define timeutils_seconds_since_epoch timeutils_seconds_since_2000
87#define timeutils_mktime timeutils_mktime_2000
88
89static inline uint64_t timeutils_seconds_since_epoch_to_nanoseconds_since_1970(mp_uint_t s) {
90 return ((uint64_t)s + TIMEUTILS_SECONDS_1970_TO_2000) * 1000000000ULL;
91}
92
93static inline mp_uint_t timeutils_seconds_since_epoch_from_nanoseconds_since_1970(uint64_t ns) {
94 return ns / 1000000000ULL - TIMEUTILS_SECONDS_1970_TO_2000;
95}
96
97static inline int64_t timeutils_nanoseconds_since_epoch_to_nanoseconds_since_1970(int64_t ns) {
98 return ns + TIMEUTILS_SECONDS_1970_TO_2000 * 1000000000ULL;
99}
100
101#endif
102
103#endif // MICROPY_INCLUDED_LIB_TIMEUTILS_TIMEUTILS_H
104