LCOV - code coverage report
Current view: top level - shared/timeutils - timeutils.h (source / functions) Hit Total Coverage
Test: unix_coverage_v1.19.1-740-gbf49a087b.info Lines: 4 4 100.0 %
Date: 2022-12-09 11:55:04 Functions: 0 0 -
Branches: 0 0 -

           Branch data     Line data    Source code
       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                 :            : 
      34                 :            : typedef 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                 :            : 
      45                 :            : bool timeutils_is_leap_year(mp_uint_t year);
      46                 :            : mp_uint_t timeutils_days_in_month(mp_uint_t year, mp_uint_t month);
      47                 :            : mp_uint_t timeutils_year_day(mp_uint_t year, mp_uint_t month, mp_uint_t date);
      48                 :            : 
      49                 :            : void timeutils_seconds_since_2000_to_struct_time(mp_uint_t t,
      50                 :            :     timeutils_struct_time_t *tm);
      51                 :            : 
      52                 :            : mp_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                 :            : 
      55                 :            : mp_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                 :            : 
      61                 :            : static 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                 :            : 
      66                 :            : 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) {
      67                 :            :     return timeutils_mktime_2000(year, month, mday, hours, minutes, seconds) + TIMEUTILS_SECONDS_1970_TO_2000;
      68                 :            : }
      69                 :            : 
      70                 :         14 : static 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                 :            :     // TODO this will give incorrect results for dates before 2000/1/1
      73                 :         14 :     return timeutils_seconds_since_2000(year, month, date, hour, minute, second) + TIMEUTILS_SECONDS_1970_TO_2000;
      74                 :            : }
      75                 :            : 
      76                 :         24 : static inline mp_uint_t timeutils_seconds_since_epoch_from_nanoseconds_since_1970(uint64_t ns) {
      77                 :         24 :     return ns / 1000000000ULL;
      78                 :            : }
      79                 :            : 
      80                 :            : static inline uint64_t timeutils_nanoseconds_since_epoch_to_nanoseconds_since_1970(uint64_t ns) {
      81                 :            :     return ns;
      82                 :            : }
      83                 :            : 
      84                 :            : #else // Epoch is 2000
      85                 :            : 
      86                 :            : #define timeutils_seconds_since_epoch_to_struct_time timeutils_seconds_since_2000_to_struct_time
      87                 :            : #define timeutils_seconds_since_epoch timeutils_seconds_since_2000
      88                 :            : #define timeutils_mktime timeutils_mktime_2000
      89                 :            : 
      90                 :            : static inline uint64_t timeutils_seconds_since_epoch_to_nanoseconds_since_1970(mp_uint_t s) {
      91                 :            :     return ((uint64_t)s + TIMEUTILS_SECONDS_1970_TO_2000) * 1000000000ULL;
      92                 :            : }
      93                 :            : 
      94                 :            : static inline mp_uint_t timeutils_seconds_since_epoch_from_nanoseconds_since_1970(uint64_t ns) {
      95                 :            :     return ns / 1000000000ULL - TIMEUTILS_SECONDS_1970_TO_2000;
      96                 :            : }
      97                 :            : 
      98                 :            : static inline int64_t timeutils_nanoseconds_since_epoch_to_nanoseconds_since_1970(int64_t ns) {
      99                 :            :     return ns + TIMEUTILS_SECONDS_1970_TO_2000 * 1000000000ULL;
     100                 :            : }
     101                 :            : 
     102                 :            : #endif
     103                 :            : 
     104                 :            : int timeutils_calc_weekday(int y, int m, int d);
     105                 :            : 
     106                 :            : #endif // MICROPY_INCLUDED_LIB_TIMEUTILS_TIMEUTILS_H

Generated by: LCOV version 1.15-5-g462f71d