extmod/modtime: Move tuple creation to common localtime implementation.

This factors code out of the ports and into the common `time.localtime`
implementation in `extmod/modtime.c`.  That helps to reduce code
duplication, prevent errors in implementation, and reduce code size on
some ports (mimxrt and stm32 at least).

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2025-08-12 11:47:52 +10:00
parent 40cc4e4f74
commit b3cd1a355e
10 changed files with 63 additions and 145 deletions

View file

@ -53,26 +53,26 @@
// - weekday is 0-6 for Mon-Sun // - weekday is 0-6 for Mon-Sun
// - yearday is 1-366 // - yearday is 1-366
static mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { static mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
timeutils_struct_time_t tm;
if (n_args == 0 || args[0] == mp_const_none) { if (n_args == 0 || args[0] == mp_const_none) {
// Get current date and time. // Get current date and time.
return mp_time_localtime_get(); mp_time_localtime_get(&tm);
} else { } else {
// Convert given seconds to tuple. // Convert given seconds to tuple.
mp_timestamp_t seconds = timeutils_obj_get_timestamp(args[0]); mp_timestamp_t seconds = timeutils_obj_get_timestamp(args[0]);
timeutils_struct_time_t tm;
timeutils_seconds_since_epoch_to_struct_time(seconds, &tm); timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
mp_obj_t tuple[8] = {
tuple[0] = mp_obj_new_int(tm.tm_year),
tuple[1] = mp_obj_new_int(tm.tm_mon),
tuple[2] = mp_obj_new_int(tm.tm_mday),
tuple[3] = mp_obj_new_int(tm.tm_hour),
tuple[4] = mp_obj_new_int(tm.tm_min),
tuple[5] = mp_obj_new_int(tm.tm_sec),
tuple[6] = mp_obj_new_int(tm.tm_wday),
tuple[7] = mp_obj_new_int(tm.tm_yday),
};
return mp_obj_new_tuple(8, tuple);
} }
mp_obj_t tuple[8] = {
mp_obj_new_int(tm.tm_year),
mp_obj_new_int(tm.tm_mon),
mp_obj_new_int(tm.tm_mday),
mp_obj_new_int(tm.tm_hour),
mp_obj_new_int(tm.tm_min),
mp_obj_new_int(tm.tm_sec),
mp_obj_new_int(tm.tm_wday),
mp_obj_new_int(tm.tm_yday),
};
return mp_obj_new_tuple(8, tuple);
} }
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_time_localtime_obj, 0, 1, time_localtime); MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_time_localtime_obj, 0, 1, time_localtime);

View file

@ -29,23 +29,10 @@
#include "shared/timeutils/timeutils.h" #include "shared/timeutils/timeutils.h"
#include "pybrtc.h" #include "pybrtc.h"
// Return the localtime as an 8-tuple. // Get the localtime.
static mp_obj_t mp_time_localtime_get(void) { static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
timeutils_struct_time_t tm;
// get the seconds from the RTC // get the seconds from the RTC
timeutils_seconds_since_2000_to_struct_time(pyb_rtc_get_seconds(), &tm); timeutils_seconds_since_2000_to_struct_time(pyb_rtc_get_seconds(), tm);
mp_obj_t tuple[8] = {
mp_obj_new_int(tm.tm_year),
mp_obj_new_int(tm.tm_mon),
mp_obj_new_int(tm.tm_mday),
mp_obj_new_int(tm.tm_hour),
mp_obj_new_int(tm.tm_min),
mp_obj_new_int(tm.tm_sec),
mp_obj_new_int(tm.tm_wday),
mp_obj_new_int(tm.tm_yday)
};
return mp_obj_new_tuple(8, tuple);
} }
// Returns the number of seconds, as an integer, since the Epoch. // Returns the number of seconds, as an integer, since the Epoch.

View file

@ -31,23 +31,11 @@
#include "py/obj.h" #include "py/obj.h"
#include "shared/timeutils/timeutils.h" #include "shared/timeutils/timeutils.h"
// Return the localtime as an 8-tuple. // Get the localtime.
static mp_obj_t mp_time_localtime_get(void) { static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
timeutils_struct_time_t tm; timeutils_seconds_since_epoch_to_struct_time(tv.tv_sec, tm);
timeutils_seconds_since_epoch_to_struct_time(tv.tv_sec, &tm);
mp_obj_t tuple[8] = {
tuple[0] = mp_obj_new_int(tm.tm_year),
tuple[1] = mp_obj_new_int(tm.tm_mon),
tuple[2] = mp_obj_new_int(tm.tm_mday),
tuple[3] = mp_obj_new_int(tm.tm_hour),
tuple[4] = mp_obj_new_int(tm.tm_min),
tuple[5] = mp_obj_new_int(tm.tm_sec),
tuple[6] = mp_obj_new_int(tm.tm_wday),
tuple[7] = mp_obj_new_int(tm.tm_yday),
};
return mp_obj_new_tuple(8, tuple);
} }
// Return the number of seconds since the Epoch. // Return the number of seconds since the Epoch.

View file

@ -29,22 +29,10 @@
#include "shared/timeutils/timeutils.h" #include "shared/timeutils/timeutils.h"
#include "modmachine.h" #include "modmachine.h"
// Return the localtime as an 8-tuple. // Get the localtime.
static mp_obj_t mp_time_localtime_get(void) { static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
mp_uint_t seconds = pyb_rtc_get_us_since_epoch() / 1000u / 1000u; mp_uint_t seconds = pyb_rtc_get_us_since_epoch() / 1000u / 1000u;
timeutils_struct_time_t tm; timeutils_seconds_since_epoch_to_struct_time(seconds, tm);
timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
mp_obj_t tuple[8] = {
tuple[0] = mp_obj_new_int(tm.tm_year),
tuple[1] = mp_obj_new_int(tm.tm_mon),
tuple[2] = mp_obj_new_int(tm.tm_mday),
tuple[3] = mp_obj_new_int(tm.tm_hour),
tuple[4] = mp_obj_new_int(tm.tm_min),
tuple[5] = mp_obj_new_int(tm.tm_sec),
tuple[6] = mp_obj_new_int(tm.tm_wday),
tuple[7] = mp_obj_new_int(tm.tm_yday),
};
return mp_obj_new_tuple(8, tuple);
} }
// Returns the number of seconds, as an integer, since the Epoch. // Returns the number of seconds, as an integer, since the Epoch.

View file

@ -29,22 +29,19 @@
#include "shared/timeutils/timeutils.h" #include "shared/timeutils/timeutils.h"
#include "fsl_snvs_lp.h" #include "fsl_snvs_lp.h"
// Return the localtime as an 8-tuple. // Get the localtime.
static mp_obj_t mp_time_localtime_get(void) { static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
// Get current date and time. // Get current date and time.
snvs_lp_srtc_datetime_t t; snvs_lp_srtc_datetime_t t;
SNVS_LP_SRTC_GetDatetime(SNVS, &t); SNVS_LP_SRTC_GetDatetime(SNVS, &t);
mp_obj_t tuple[8] = { tm->tm_year = t.year;
mp_obj_new_int(t.year), tm->tm_mon = t.month;
mp_obj_new_int(t.month), tm->tm_mday = t.day;
mp_obj_new_int(t.day), tm->tm_hour = t.hour;
mp_obj_new_int(t.hour), tm->tm_min = t.minute;
mp_obj_new_int(t.minute), tm->tm_sec = t.second;
mp_obj_new_int(t.second), tm->tm_wday = timeutils_calc_weekday(t.year, t.month, t.day);
mp_obj_new_int(timeutils_calc_weekday(t.year, t.month, t.day)), tm->tm_yday = timeutils_year_day(t.year, t.month, t.day);
mp_obj_new_int(timeutils_year_day(t.year, t.month, t.day)),
};
return mp_obj_new_tuple(8, tuple);
} }
// Return the number of seconds since the Epoch. // Return the number of seconds since the Epoch.

View file

@ -28,23 +28,20 @@
#include "shared/timeutils/timeutils.h" #include "shared/timeutils/timeutils.h"
#include "rtc.h" #include "rtc.h"
// Return the localtime as an 8-tuple. // Get the localtime.
static mp_obj_t mp_time_localtime_get(void) { static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
// get current date and time // get current date and time
rtc_init_finalise(); rtc_init_finalise();
ra_rtc_t time; ra_rtc_t time;
ra_rtc_get_time(&time); ra_rtc_get_time(&time);
mp_obj_t tuple[8] = { tm->tm_year = time.year;
mp_obj_new_int(time.year), tm->tm_mon = time.month;
mp_obj_new_int(time.month), tm->tm_mday = time.date;
mp_obj_new_int(time.date), tm->tm_hour = time.hour;
mp_obj_new_int(time.hour), tm->tm_min = time.minute;
mp_obj_new_int(time.minute), tm->tm_sec = time.second;
mp_obj_new_int(time.second), tm->tm_wday = time.weekday - 1;
mp_obj_new_int(time.weekday - 1), tm->tm_yday = timeutils_year_day(time.year, time.month, time.date);
mp_obj_new_int(timeutils_year_day(time.year, time.month, time.date)),
};
return mp_obj_new_tuple(8, tuple);
} }
// Returns the number of seconds, as an integer, since the Epoch. // Returns the number of seconds, as an integer, since the Epoch.

View file

@ -28,23 +28,11 @@
#include "shared/timeutils/timeutils.h" #include "shared/timeutils/timeutils.h"
#include "pico/aon_timer.h" #include "pico/aon_timer.h"
// Return the localtime as an 8-tuple. // Get the localtime.
static mp_obj_t mp_time_localtime_get(void) { static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
struct timespec ts; struct timespec ts;
aon_timer_get_time(&ts); aon_timer_get_time(&ts);
timeutils_struct_time_t tm; timeutils_seconds_since_epoch_to_struct_time(ts.tv_sec, tm);
timeutils_seconds_since_epoch_to_struct_time(ts.tv_sec, &tm);
mp_obj_t tuple[8] = {
mp_obj_new_int(tm.tm_year),
mp_obj_new_int(tm.tm_mon),
mp_obj_new_int(tm.tm_mday),
mp_obj_new_int(tm.tm_hour),
mp_obj_new_int(tm.tm_min),
mp_obj_new_int(tm.tm_sec),
mp_obj_new_int(tm.tm_wday),
mp_obj_new_int(tm.tm_yday),
};
return mp_obj_new_tuple(8, tuple);
} }
// Return the number of seconds since the Epoch. // Return the number of seconds since the Epoch.

View file

@ -30,23 +30,11 @@
static uint64_t time_us_64_offset_from_epoch; static uint64_t time_us_64_offset_from_epoch;
// Return the localtime as an 8-tuple. // Get the localtime.
static mp_obj_t mp_time_localtime_get(void) { static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
timeutils_struct_time_t tm; rtc_gettime(tm);
rtc_gettime(&tm); tm->tm_wday = timeutils_calc_weekday(tm->tm_year, tm->tm_mon, tm->tm_mday);
tm.tm_wday = timeutils_calc_weekday(tm.tm_year, tm.tm_mon, tm.tm_mday); tm->tm_yday = timeutils_year_day(tm->tm_year, tm->tm_mon, tm->tm_mday);
tm.tm_yday = timeutils_year_day(tm.tm_year, tm.tm_mon, tm.tm_mday);
mp_obj_t tuple[8] = {
tuple[0] = mp_obj_new_int(tm.tm_year),
tuple[1] = mp_obj_new_int(tm.tm_mon),
tuple[2] = mp_obj_new_int(tm.tm_mday),
tuple[3] = mp_obj_new_int(tm.tm_hour),
tuple[4] = mp_obj_new_int(tm.tm_min),
tuple[5] = mp_obj_new_int(tm.tm_sec),
tuple[6] = mp_obj_new_int(tm.tm_wday),
tuple[7] = mp_obj_new_int(tm.tm_yday),
};
return mp_obj_new_tuple(8, tuple);
} }
// Returns the number of seconds, as an integer, since the Epoch. // Returns the number of seconds, as an integer, since the Epoch.

View file

@ -28,8 +28,8 @@
#include "shared/timeutils/timeutils.h" #include "shared/timeutils/timeutils.h"
#include "rtc.h" #include "rtc.h"
// Return the localtime as an 8-tuple. // Get the localtime.
static mp_obj_t mp_time_localtime_get(void) { static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
// get current date and time // get current date and time
// note: need to call get time then get date to correctly access the registers // note: need to call get time then get date to correctly access the registers
rtc_init_finalise(); rtc_init_finalise();
@ -37,17 +37,14 @@ static mp_obj_t mp_time_localtime_get(void) {
RTC_TimeTypeDef time; RTC_TimeTypeDef time;
HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN); HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN); HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN);
mp_obj_t tuple[8] = { tm->tm_year = 2000 + date.Year;
mp_obj_new_int(2000 + date.Year), tm->tm_mon = date.Month;
mp_obj_new_int(date.Month), tm->tm_mday = date.Date;
mp_obj_new_int(date.Date), tm->tm_hour = time.Hours;
mp_obj_new_int(time.Hours), tm->tm_min = time.Minutes;
mp_obj_new_int(time.Minutes), tm->tm_sec = time.Seconds;
mp_obj_new_int(time.Seconds), tm->tm_wday = date.WeekDay - 1;
mp_obj_new_int(date.WeekDay - 1), tm->tm_yday = timeutils_year_day(tm->tm_year, date.Month, date.Date);
mp_obj_new_int(timeutils_year_day(2000 + date.Year, date.Month, date.Date)),
};
return mp_obj_new_tuple(8, tuple);
} }
// Returns the number of seconds, as an integer, since 1/1/2000. // Returns the number of seconds, as an integer, since 1/1/2000.

View file

@ -28,21 +28,9 @@
#include "shared/timeutils/timeutils.h" #include "shared/timeutils/timeutils.h"
#include "library.h" #include "library.h"
// Return the localtime as an 8-tuple. // Get the localtime.
static mp_obj_t mp_time_localtime_get(void) { static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
timeutils_struct_time_t tm; timeutils_seconds_since_epoch_to_struct_time(mp_hal_time_ms() / 1000, tm);
timeutils_seconds_since_epoch_to_struct_time(mp_hal_time_ms() / 1000, &tm);
mp_obj_t tuple[8] = {
mp_obj_new_int(tm.tm_year),
mp_obj_new_int(tm.tm_mon),
mp_obj_new_int(tm.tm_mday),
mp_obj_new_int(tm.tm_hour),
mp_obj_new_int(tm.tm_min),
mp_obj_new_int(tm.tm_sec),
mp_obj_new_int(tm.tm_wday),
mp_obj_new_int(tm.tm_yday),
};
return mp_obj_new_tuple(8, tuple);
} }
// Returns the number of seconds, as a float, since the Epoch. // Returns the number of seconds, as a float, since the Epoch.