Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 1 | /* |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame] | 2 | * This file is part of the MicroPython project, http://micropython.org/ |
Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
Damien George | 4a4046d | 2023-03-10 12:20:45 +1100 | [diff] [blame] | 6 | * Copyright (c) 2013-2023 Damien P. George |
Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 Josef Gajdusek |
| 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 | |
Damien George | 4a4046d | 2023-03-10 12:20:45 +1100 | [diff] [blame] | 28 | #include "py/obj.h" |
Damien George | 136369d | 2021-07-09 14:19:15 +1000 | [diff] [blame] | 29 | #include "shared/timeutils/timeutils.h" |
Paul Sokolovsky | 8bc3fc2 | 2016-11-06 01:30:19 +0300 | [diff] [blame] | 30 | #include "modmachine.h" |
Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 31 | |
Damien George | 4a4046d | 2023-03-10 12:20:45 +1100 | [diff] [blame] | 32 | // Return the localtime as an 8-tuple. |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 33 | static mp_obj_t mp_time_localtime_get(void) { |
Yoctopuce dev | df05cae | 2025-07-01 13:16:20 +0200 | [diff] [blame] | 34 | mp_uint_t seconds = pyb_rtc_get_us_since_epoch() / 1000u / 1000u; |
Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 35 | timeutils_struct_time_t tm; |
Damien George | 8f20cdc | 2020-09-14 12:15:03 +1000 | [diff] [blame] | 36 | timeutils_seconds_since_epoch_to_struct_time(seconds, &tm); |
Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 37 | mp_obj_t tuple[8] = { |
| 38 | tuple[0] = mp_obj_new_int(tm.tm_year), |
| 39 | tuple[1] = mp_obj_new_int(tm.tm_mon), |
| 40 | tuple[2] = mp_obj_new_int(tm.tm_mday), |
| 41 | tuple[3] = mp_obj_new_int(tm.tm_hour), |
| 42 | tuple[4] = mp_obj_new_int(tm.tm_min), |
| 43 | tuple[5] = mp_obj_new_int(tm.tm_sec), |
| 44 | tuple[6] = mp_obj_new_int(tm.tm_wday), |
| 45 | tuple[7] = mp_obj_new_int(tm.tm_yday), |
| 46 | }; |
| 47 | return mp_obj_new_tuple(8, tuple); |
| 48 | } |
Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 49 | |
Damien George | 4a4046d | 2023-03-10 12:20:45 +1100 | [diff] [blame] | 50 | // Returns the number of seconds, as an integer, since the Epoch. |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 51 | static mp_obj_t mp_time_time_get(void) { |
Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 52 | // get date and time |
Yoctopuce dev | df05cae | 2025-07-01 13:16:20 +0200 | [diff] [blame] | 53 | return timeutils_obj_from_timestamp(pyb_rtc_get_us_since_epoch() / 1000 / 1000); |
Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 54 | } |