LCOV - code coverage report
Current view: top level - extmod - modtime.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.23.0-37-gd7d77d91b.info Lines: 35 35 100.0 %
Date: 2024-06-12 04:17:08 Functions: 10 10 100.0 %
Branches: 4 6 66.7 %

           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-2023 Damien P. George
       7                 :            :  * Copyright (c) 2016 Paul Sokolovsky
       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                 :            : 
      28                 :            : #include "py/mphal.h"
      29                 :            : #include "py/runtime.h"
      30                 :            : #include "py/smallint.h"
      31                 :            : #include "extmod/modtime.h"
      32                 :            : 
      33                 :            : #if MICROPY_PY_TIME
      34                 :            : 
      35                 :            : #ifdef MICROPY_PY_TIME_INCLUDEFILE
      36                 :            : #include MICROPY_PY_TIME_INCLUDEFILE
      37                 :            : #endif
      38                 :            : 
      39                 :            : #if MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME
      40                 :            : 
      41                 :            : #include "shared/timeutils/timeutils.h"
      42                 :            : 
      43                 :            : // localtime([secs])
      44                 :            : // Convert a time expressed in seconds since the Epoch into an 8-tuple which
      45                 :            : // contains: (year, month, mday, hour, minute, second, weekday, yearday)
      46                 :            : // If secs is not provided or None, then the current time is used.
      47                 :            : // - year    is the full year, eg 2000
      48                 :            : // - month   is 1-12
      49                 :            : // - mday    is 1-31
      50                 :            : // - hour    is 0-23
      51                 :            : // - minute  is 0-59
      52                 :            : // - second  is 0-59
      53                 :            : // - weekday is 0-6 for Mon-Sun
      54                 :            : // - yearday is 1-366
      55                 :            : static mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
      56                 :            :     if (n_args == 0 || args[0] == mp_const_none) {
      57                 :            :         // Get current date and time.
      58                 :            :         return mp_time_localtime_get();
      59                 :            :     } else {
      60                 :            :         // Convert given seconds to tuple.
      61                 :            :         mp_int_t seconds = mp_obj_get_int(args[0]);
      62                 :            :         timeutils_struct_time_t tm;
      63                 :            :         timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
      64                 :            :         mp_obj_t tuple[8] = {
      65                 :            :             tuple[0] = mp_obj_new_int(tm.tm_year),
      66                 :            :             tuple[1] = mp_obj_new_int(tm.tm_mon),
      67                 :            :             tuple[2] = mp_obj_new_int(tm.tm_mday),
      68                 :            :             tuple[3] = mp_obj_new_int(tm.tm_hour),
      69                 :            :             tuple[4] = mp_obj_new_int(tm.tm_min),
      70                 :            :             tuple[5] = mp_obj_new_int(tm.tm_sec),
      71                 :            :             tuple[6] = mp_obj_new_int(tm.tm_wday),
      72                 :            :             tuple[7] = mp_obj_new_int(tm.tm_yday),
      73                 :            :         };
      74                 :            :         return mp_obj_new_tuple(8, tuple);
      75                 :            :     }
      76                 :            : }
      77                 :            : MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_time_localtime_obj, 0, 1, time_localtime);
      78                 :            : 
      79                 :            : // mktime()
      80                 :            : // This is the inverse function of localtime. Its argument is a full 8-tuple
      81                 :            : // which expresses a time as per localtime. It returns an integer which is
      82                 :            : // the number of seconds since the Epoch (eg 1st Jan 1970, or 1st Jan 2000).
      83                 :            : static mp_obj_t time_mktime(mp_obj_t tuple) {
      84                 :            :     size_t len;
      85                 :            :     mp_obj_t *elem;
      86                 :            :     mp_obj_get_array(tuple, &len, &elem);
      87                 :            : 
      88                 :            :     // localtime generates a tuple of len 8. CPython uses 9, so we accept both.
      89                 :            :     if (len < 8 || len > 9) {
      90                 :            :         mp_raise_TypeError(MP_ERROR_TEXT("mktime needs a tuple of length 8 or 9"));
      91                 :            :     }
      92                 :            : 
      93                 :            :     return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]),
      94                 :            :         mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]),
      95                 :            :         mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])));
      96                 :            : }
      97                 :            : MP_DEFINE_CONST_FUN_OBJ_1(mp_time_mktime_obj, time_mktime);
      98                 :            : 
      99                 :            : #endif // MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME
     100                 :            : 
     101                 :            : #if MICROPY_PY_TIME_TIME_TIME_NS
     102                 :            : 
     103                 :            : // time()
     104                 :            : // Return the number of seconds since the Epoch.
     105                 :        154 : static mp_obj_t time_time(void) {
     106                 :        154 :     return mp_time_time_get();
     107                 :            : }
     108                 :            : static MP_DEFINE_CONST_FUN_OBJ_0(mp_time_time_obj, time_time);
     109                 :            : 
     110                 :            : // time_ns()
     111                 :            : // Returns the number of nanoseconds since the Epoch, as an integer.
     112                 :         12 : static mp_obj_t time_time_ns(void) {
     113                 :         12 :     return mp_obj_new_int_from_ull(mp_hal_time_ns());
     114                 :            : }
     115                 :            : MP_DEFINE_CONST_FUN_OBJ_0(mp_time_time_ns_obj, time_time_ns);
     116                 :            : 
     117                 :            : #endif // MICROPY_PY_TIME_TIME_TIME_NS
     118                 :            : 
     119                 :       5755 : static mp_obj_t time_sleep(mp_obj_t seconds_o) {
     120                 :            :     #ifdef MICROPY_PY_TIME_CUSTOM_SLEEP
     121                 :       5755 :     mp_time_sleep(seconds_o);
     122                 :            :     #else
     123                 :            :     #if MICROPY_PY_BUILTINS_FLOAT
     124                 :            :     mp_hal_delay_ms((mp_uint_t)(1000 * mp_obj_get_float(seconds_o)));
     125                 :            :     #else
     126                 :            :     mp_hal_delay_ms(1000 * mp_obj_get_int(seconds_o));
     127                 :            :     #endif
     128                 :            :     #endif
     129                 :       5755 :     return mp_const_none;
     130                 :            : }
     131                 :            : MP_DEFINE_CONST_FUN_OBJ_1(mp_time_sleep_obj, time_sleep);
     132                 :            : 
     133                 :         74 : static mp_obj_t time_sleep_ms(mp_obj_t arg) {
     134                 :         74 :     mp_int_t ms = mp_obj_get_int(arg);
     135         [ +  - ]:         74 :     if (ms >= 0) {
     136                 :         74 :         mp_hal_delay_ms(ms);
     137                 :            :     }
     138                 :         74 :     return mp_const_none;
     139                 :            : }
     140                 :            : MP_DEFINE_CONST_FUN_OBJ_1(mp_time_sleep_ms_obj, time_sleep_ms);
     141                 :            : 
     142                 :          4 : static mp_obj_t time_sleep_us(mp_obj_t arg) {
     143                 :          4 :     mp_int_t us = mp_obj_get_int(arg);
     144         [ +  - ]:          4 :     if (us > 0) {
     145                 :          4 :         mp_hal_delay_us(us);
     146                 :            :     }
     147                 :          4 :     return mp_const_none;
     148                 :            : }
     149                 :            : MP_DEFINE_CONST_FUN_OBJ_1(mp_time_sleep_us_obj, time_sleep_us);
     150                 :            : 
     151                 :     294301 : static mp_obj_t time_ticks_ms(void) {
     152                 :     294301 :     return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & (MICROPY_PY_TIME_TICKS_PERIOD - 1));
     153                 :            : }
     154                 :            : MP_DEFINE_CONST_FUN_OBJ_0(mp_time_ticks_ms_obj, time_ticks_ms);
     155                 :            : 
     156                 :         54 : static mp_obj_t time_ticks_us(void) {
     157                 :         54 :     return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_us() & (MICROPY_PY_TIME_TICKS_PERIOD - 1));
     158                 :            : }
     159                 :            : MP_DEFINE_CONST_FUN_OBJ_0(mp_time_ticks_us_obj, time_ticks_us);
     160                 :            : 
     161                 :         54 : static mp_obj_t time_ticks_cpu(void) {
     162                 :         54 :     return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_cpu() & (MICROPY_PY_TIME_TICKS_PERIOD - 1));
     163                 :            : }
     164                 :            : MP_DEFINE_CONST_FUN_OBJ_0(mp_time_ticks_cpu_obj, time_ticks_cpu);
     165                 :            : 
     166                 :     153996 : static mp_obj_t time_ticks_diff(mp_obj_t end_in, mp_obj_t start_in) {
     167                 :            :     // we assume that the arguments come from ticks_xx so are small ints
     168                 :     153996 :     mp_uint_t start = MP_OBJ_SMALL_INT_VALUE(start_in);
     169                 :     153996 :     mp_uint_t end = MP_OBJ_SMALL_INT_VALUE(end_in);
     170                 :            :     // Optimized formula avoiding if conditions. We adjust difference "forward",
     171                 :            :     // wrap it around and adjust back.
     172                 :     153996 :     mp_int_t diff = ((end - start + MICROPY_PY_TIME_TICKS_PERIOD / 2) & (MICROPY_PY_TIME_TICKS_PERIOD - 1))
     173                 :     153996 :         - MICROPY_PY_TIME_TICKS_PERIOD / 2;
     174                 :     153996 :     return MP_OBJ_NEW_SMALL_INT(diff);
     175                 :            : }
     176                 :            : MP_DEFINE_CONST_FUN_OBJ_2(mp_time_ticks_diff_obj, time_ticks_diff);
     177                 :            : 
     178                 :     140396 : static mp_obj_t time_ticks_add(mp_obj_t ticks_in, mp_obj_t delta_in) {
     179                 :            :     // we assume that first argument come from ticks_xx so is small int
     180                 :     140396 :     mp_uint_t ticks = MP_OBJ_SMALL_INT_VALUE(ticks_in);
     181                 :     140396 :     mp_uint_t delta = mp_obj_get_int(delta_in);
     182                 :            : 
     183                 :            :     // Check that delta does not overflow the range that ticks_diff can handle.
     184                 :            :     // This ensures the following:
     185                 :            :     //  - ticks_diff(ticks_add(T, delta), T) == delta
     186                 :            :     //  - ticks_diff(T, ticks_add(T, delta)) == -delta
     187                 :            :     // The latter requires excluding delta=-TICKS_PERIOD/2.
     188                 :            :     //
     189                 :            :     // This unsigned comparison is equivalent to a signed comparison of:
     190                 :            :     //   delta <= -TICKS_PERIOD/2 || delta >= TICKS_PERIOD/2
     191         [ +  + ]:     140396 :     if (delta + MICROPY_PY_TIME_TICKS_PERIOD / 2 - 1 >= MICROPY_PY_TIME_TICKS_PERIOD - 1) {
     192                 :         25 :         mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("ticks interval overflow"));
     193                 :            :     }
     194                 :            : 
     195                 :     140371 :     return MP_OBJ_NEW_SMALL_INT((ticks + delta) & (MICROPY_PY_TIME_TICKS_PERIOD - 1));
     196                 :            : }
     197                 :            : MP_DEFINE_CONST_FUN_OBJ_2(mp_time_ticks_add_obj, time_ticks_add);
     198                 :            : 
     199                 :            : static const mp_rom_map_elem_t mp_module_time_globals_table[] = {
     200                 :            :     { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) },
     201                 :            : 
     202                 :            :     #if MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME
     203                 :            :     { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&mp_time_localtime_obj) },
     204                 :            :     { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&mp_time_localtime_obj) },
     205                 :            :     { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&mp_time_mktime_obj) },
     206                 :            :     #endif
     207                 :            : 
     208                 :            :     #if MICROPY_PY_TIME_TIME_TIME_NS
     209                 :            :     { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&mp_time_time_obj) },
     210                 :            :     { MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_time_time_ns_obj) },
     211                 :            :     #endif
     212                 :            : 
     213                 :            :     { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_time_sleep_obj) },
     214                 :            :     { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_time_sleep_ms_obj) },
     215                 :            :     { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_time_sleep_us_obj) },
     216                 :            : 
     217                 :            :     { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_time_ticks_ms_obj) },
     218                 :            :     { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_time_ticks_us_obj) },
     219                 :            :     { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_time_ticks_cpu_obj) },
     220                 :            :     { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_time_ticks_add_obj) },
     221                 :            :     { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_time_ticks_diff_obj) },
     222                 :            : 
     223                 :            :     #ifdef MICROPY_PY_TIME_EXTRA_GLOBALS
     224                 :            :     MICROPY_PY_TIME_EXTRA_GLOBALS
     225                 :            :     #endif
     226                 :            : };
     227                 :            : static MP_DEFINE_CONST_DICT(mp_module_time_globals, mp_module_time_globals_table);
     228                 :            : 
     229                 :            : const mp_obj_module_t mp_module_time = {
     230                 :            :     .base = { &mp_type_module },
     231                 :            :     .globals = (mp_obj_dict_t *)&mp_module_time_globals,
     232                 :            : };
     233                 :            : 
     234                 :            : MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_time, mp_module_time);
     235                 :            : 
     236                 :            : #endif // MICROPY_PY_TIME

Generated by: LCOV version 1.15-5-g462f71d