LCOV - code coverage report
Current view: top level - shared/timeutils - timeutils.h (source / functions) Hit Total Coverage
Test: unix_coverage_v1.24.0-73-g2e796d6c3.info Lines: 4 4 100.0 %
Date: 2024-11-30 10:29:47 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                 :            : // Year is absolute, month/date are 1-based, hour/minute/second are 0-based.
      53                 :            : mp_uint_t timeutils_seconds_since_2000(mp_uint_t year, mp_uint_t month,
      54                 :            :     mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second);
      55                 :            : 
      56                 :            : // Year is absolute, month/mday are 1-based, hours/minutes/seconds are 0-based.
      57                 :            : mp_uint_t timeutils_mktime_2000(mp_uint_t year, mp_int_t month, mp_int_t mday,
      58                 :            :     mp_int_t hours, mp_int_t minutes, mp_int_t seconds);
      59                 :            : 
      60                 :            : // Select the Epoch used by the port.
      61                 :            : #if MICROPY_EPOCH_IS_1970
      62                 :            : 
      63                 :            : static inline void timeutils_seconds_since_epoch_to_struct_time(uint64_t t, timeutils_struct_time_t *tm) {
      64                 :            :     // TODO this will give incorrect results for dates before 2000/1/1
      65                 :            :     timeutils_seconds_since_2000_to_struct_time(t - TIMEUTILS_SECONDS_1970_TO_2000, tm);
      66                 :            : }
      67                 :            : 
      68                 :            : // Year is absolute, month/mday are 1-based, hours/minutes/seconds are 0-based.
      69                 :            : 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) {
      70                 :            :     return timeutils_mktime_2000(year, month, mday, hours, minutes, seconds) + TIMEUTILS_SECONDS_1970_TO_2000;
      71                 :            : }
      72                 :            : 
      73                 :            : // Year is absolute, month/date are 1-based, hour/minute/second are 0-based.
      74                 :         14 : static inline uint64_t timeutils_seconds_since_epoch(mp_uint_t year, mp_uint_t month,
      75                 :            :     mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second) {
      76                 :            :     // TODO this will give incorrect results for dates before 2000/1/1
      77                 :         14 :     return timeutils_seconds_since_2000(year, month, date, hour, minute, second) + TIMEUTILS_SECONDS_1970_TO_2000;
      78                 :            : }
      79                 :            : 
      80                 :         24 : static inline mp_uint_t timeutils_seconds_since_epoch_from_nanoseconds_since_1970(uint64_t ns) {
      81                 :         24 :     return ns / 1000000000ULL;
      82                 :            : }
      83                 :            : 
      84                 :            : static inline uint64_t timeutils_nanoseconds_since_epoch_to_nanoseconds_since_1970(uint64_t ns) {
      85                 :            :     return ns;
      86                 :            : }
      87                 :            : 
      88                 :            : #else // Epoch is 2000
      89                 :            : 
      90                 :            : #define timeutils_seconds_since_epoch_to_struct_time timeutils_seconds_since_2000_to_struct_time
      91                 :            : #define timeutils_seconds_since_epoch timeutils_seconds_since_2000
      92                 :            : #define timeutils_mktime timeutils_mktime_2000
      93                 :            : 
      94                 :            : static inline uint64_t timeutils_seconds_since_epoch_to_nanoseconds_since_1970(mp_uint_t s) {
      95                 :            :     return ((uint64_t)s + TIMEUTILS_SECONDS_1970_TO_2000) * 1000000000ULL;
      96                 :            : }
      97                 :            : 
      98                 :            : static inline mp_uint_t timeutils_seconds_since_epoch_from_nanoseconds_since_1970(uint64_t ns) {
      99                 :            :     return ns / 1000000000ULL - TIMEUTILS_SECONDS_1970_TO_2000;
     100                 :            : }
     101                 :            : 
     102                 :            : static inline int64_t timeutils_nanoseconds_since_epoch_to_nanoseconds_since_1970(int64_t ns) {
     103                 :            :     return ns + TIMEUTILS_SECONDS_1970_TO_2000 * 1000000000ULL;
     104                 :            : }
     105                 :            : 
     106                 :            : #endif
     107                 :            : 
     108                 :            : int timeutils_calc_weekday(int y, int m, int d);
     109                 :            : 
     110                 :            : #endif // MICROPY_INCLUDED_LIB_TIMEUTILS_TIMEUTILS_H

Generated by: LCOV version 1.15-5-g462f71d