blob: e99d920fde80baafe9f40c0f47de8de0ab19ce50 [file] [log] [blame]
Josef Gajdusek103d12a2015-05-11 21:11:37 +02001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Josef Gajdusek103d12a2015-05-11 21:11:37 +02003 *
4 * The MIT License (MIT)
5 *
Damien George4a4046d2023-03-10 12:20:45 +11006 * Copyright (c) 2013-2023 Damien P. George
Josef Gajdusek103d12a2015-05-11 21:11:37 +02007 * 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 George4a4046d2023-03-10 12:20:45 +110028#include "py/obj.h"
Damien George136369d2021-07-09 14:19:15 +100029#include "shared/timeutils/timeutils.h"
Paul Sokolovsky8bc3fc22016-11-06 01:30:19 +030030#include "modmachine.h"
Josef Gajdusek103d12a2015-05-11 21:11:37 +020031
Damien George4a4046d2023-03-10 12:20:45 +110032// Return the localtime as an 8-tuple.
Angus Grattondecf8e62024-02-27 15:32:29 +110033static mp_obj_t mp_time_localtime_get(void) {
Yoctopuce devdf05cae2025-07-01 13:16:20 +020034 mp_uint_t seconds = pyb_rtc_get_us_since_epoch() / 1000u / 1000u;
Josef Gajdusek103d12a2015-05-11 21:11:37 +020035 timeutils_struct_time_t tm;
Damien George8f20cdc2020-09-14 12:15:03 +100036 timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
Josef Gajdusek103d12a2015-05-11 21:11:37 +020037 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 Gajdusek103d12a2015-05-11 21:11:37 +020049
Damien George4a4046d2023-03-10 12:20:45 +110050// Returns the number of seconds, as an integer, since the Epoch.
Angus Grattondecf8e62024-02-27 15:32:29 +110051static mp_obj_t mp_time_time_get(void) {
Josef Gajdusek103d12a2015-05-11 21:11:37 +020052 // get date and time
Yoctopuce devdf05cae2025-07-01 13:16:20 +020053 return timeutils_obj_from_timestamp(pyb_rtc_get_us_since_epoch() / 1000 / 1000);
Josef Gajdusek103d12a2015-05-11 21:11:37 +020054}