Skip to content

Commit b3cd1a3

Browse files
committed
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>
1 parent 40cc4e4 commit b3cd1a3

File tree

10 files changed

+63
-145
lines changed

10 files changed

+63
-145
lines changed

extmod/modtime.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,26 @@
5353
// - weekday is 0-6 for Mon-Sun
5454
// - yearday is 1-366
5555
static mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
56+
timeutils_struct_time_t tm;
5657
if (n_args == 0 || args[0] == mp_const_none) {
5758
// Get current date and time.
58-
return mp_time_localtime_get();
59+
mp_time_localtime_get(&tm);
5960
} else {
6061
// Convert given seconds to tuple.
6162
mp_timestamp_t seconds = timeutils_obj_get_timestamp(args[0]);
62-
timeutils_struct_time_t tm;
6363
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);
7564
}
65+
mp_obj_t tuple[8] = {
66+
mp_obj_new_int(tm.tm_year),
67+
mp_obj_new_int(tm.tm_mon),
68+
mp_obj_new_int(tm.tm_mday),
69+
mp_obj_new_int(tm.tm_hour),
70+
mp_obj_new_int(tm.tm_min),
71+
mp_obj_new_int(tm.tm_sec),
72+
mp_obj_new_int(tm.tm_wday),
73+
mp_obj_new_int(tm.tm_yday),
74+
};
75+
return mp_obj_new_tuple(8, tuple);
7676
}
7777
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_time_localtime_obj, 0, 1, time_localtime);
7878

ports/cc3200/mods/modtime.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,10 @@
2929
#include "shared/timeutils/timeutils.h"
3030
#include "pybrtc.h"
3131

32-
// Return the localtime as an 8-tuple.
33-
static mp_obj_t mp_time_localtime_get(void) {
34-
timeutils_struct_time_t tm;
35-
32+
// Get the localtime.
33+
static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
3634
// get the seconds from the RTC
37-
timeutils_seconds_since_2000_to_struct_time(pyb_rtc_get_seconds(), &tm);
38-
mp_obj_t tuple[8] = {
39-
mp_obj_new_int(tm.tm_year),
40-
mp_obj_new_int(tm.tm_mon),
41-
mp_obj_new_int(tm.tm_mday),
42-
mp_obj_new_int(tm.tm_hour),
43-
mp_obj_new_int(tm.tm_min),
44-
mp_obj_new_int(tm.tm_sec),
45-
mp_obj_new_int(tm.tm_wday),
46-
mp_obj_new_int(tm.tm_yday)
47-
};
48-
return mp_obj_new_tuple(8, tuple);
35+
timeutils_seconds_since_2000_to_struct_time(pyb_rtc_get_seconds(), tm);
4936
}
5037

5138
// Returns the number of seconds, as an integer, since the Epoch.

ports/esp32/modtime.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,11 @@
3131
#include "py/obj.h"
3232
#include "shared/timeutils/timeutils.h"
3333

34-
// Return the localtime as an 8-tuple.
35-
static mp_obj_t mp_time_localtime_get(void) {
34+
// Get the localtime.
35+
static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
3636
struct timeval tv;
3737
gettimeofday(&tv, NULL);
38-
timeutils_struct_time_t tm;
39-
timeutils_seconds_since_epoch_to_struct_time(tv.tv_sec, &tm);
40-
mp_obj_t tuple[8] = {
41-
tuple[0] = mp_obj_new_int(tm.tm_year),
42-
tuple[1] = mp_obj_new_int(tm.tm_mon),
43-
tuple[2] = mp_obj_new_int(tm.tm_mday),
44-
tuple[3] = mp_obj_new_int(tm.tm_hour),
45-
tuple[4] = mp_obj_new_int(tm.tm_min),
46-
tuple[5] = mp_obj_new_int(tm.tm_sec),
47-
tuple[6] = mp_obj_new_int(tm.tm_wday),
48-
tuple[7] = mp_obj_new_int(tm.tm_yday),
49-
};
50-
return mp_obj_new_tuple(8, tuple);
38+
timeutils_seconds_since_epoch_to_struct_time(tv.tv_sec, tm);
5139
}
5240

5341
// Return the number of seconds since the Epoch.

ports/esp8266/modtime.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,10 @@
2929
#include "shared/timeutils/timeutils.h"
3030
#include "modmachine.h"
3131

32-
// Return the localtime as an 8-tuple.
33-
static mp_obj_t mp_time_localtime_get(void) {
32+
// Get the localtime.
33+
static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
3434
mp_uint_t seconds = pyb_rtc_get_us_since_epoch() / 1000u / 1000u;
35-
timeutils_struct_time_t tm;
36-
timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
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);
35+
timeutils_seconds_since_epoch_to_struct_time(seconds, tm);
4836
}
4937

5038
// Returns the number of seconds, as an integer, since the Epoch.

ports/mimxrt/modtime.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,19 @@
2929
#include "shared/timeutils/timeutils.h"
3030
#include "fsl_snvs_lp.h"
3131

32-
// Return the localtime as an 8-tuple.
33-
static mp_obj_t mp_time_localtime_get(void) {
32+
// Get the localtime.
33+
static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
3434
// Get current date and time.
3535
snvs_lp_srtc_datetime_t t;
3636
SNVS_LP_SRTC_GetDatetime(SNVS, &t);
37-
mp_obj_t tuple[8] = {
38-
mp_obj_new_int(t.year),
39-
mp_obj_new_int(t.month),
40-
mp_obj_new_int(t.day),
41-
mp_obj_new_int(t.hour),
42-
mp_obj_new_int(t.minute),
43-
mp_obj_new_int(t.second),
44-
mp_obj_new_int(timeutils_calc_weekday(t.year, t.month, t.day)),
45-
mp_obj_new_int(timeutils_year_day(t.year, t.month, t.day)),
46-
};
47-
return mp_obj_new_tuple(8, tuple);
37+
tm->tm_year = t.year;
38+
tm->tm_mon = t.month;
39+
tm->tm_mday = t.day;
40+
tm->tm_hour = t.hour;
41+
tm->tm_min = t.minute;
42+
tm->tm_sec = t.second;
43+
tm->tm_wday = timeutils_calc_weekday(t.year, t.month, t.day);
44+
tm->tm_yday = timeutils_year_day(t.year, t.month, t.day);
4845
}
4946

5047
// Return the number of seconds since the Epoch.

ports/renesas-ra/modtime.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,20 @@
2828
#include "shared/timeutils/timeutils.h"
2929
#include "rtc.h"
3030

31-
// Return the localtime as an 8-tuple.
32-
static mp_obj_t mp_time_localtime_get(void) {
31+
// Get the localtime.
32+
static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
3333
// get current date and time
3434
rtc_init_finalise();
3535
ra_rtc_t time;
3636
ra_rtc_get_time(&time);
37-
mp_obj_t tuple[8] = {
38-
mp_obj_new_int(time.year),
39-
mp_obj_new_int(time.month),
40-
mp_obj_new_int(time.date),
41-
mp_obj_new_int(time.hour),
42-
mp_obj_new_int(time.minute),
43-
mp_obj_new_int(time.second),
44-
mp_obj_new_int(time.weekday - 1),
45-
mp_obj_new_int(timeutils_year_day(time.year, time.month, time.date)),
46-
};
47-
return mp_obj_new_tuple(8, tuple);
37+
tm->tm_year = time.year;
38+
tm->tm_mon = time.month;
39+
tm->tm_mday = time.date;
40+
tm->tm_hour = time.hour;
41+
tm->tm_min = time.minute;
42+
tm->tm_sec = time.second;
43+
tm->tm_wday = time.weekday - 1;
44+
tm->tm_yday = timeutils_year_day(time.year, time.month, time.date);
4845
}
4946

5047
// Returns the number of seconds, as an integer, since the Epoch.

ports/rp2/modtime.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,11 @@
2828
#include "shared/timeutils/timeutils.h"
2929
#include "pico/aon_timer.h"
3030

31-
// Return the localtime as an 8-tuple.
32-
static mp_obj_t mp_time_localtime_get(void) {
31+
// Get the localtime.
32+
static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
3333
struct timespec ts;
3434
aon_timer_get_time(&ts);
35-
timeutils_struct_time_t tm;
36-
timeutils_seconds_since_epoch_to_struct_time(ts.tv_sec, &tm);
37-
mp_obj_t tuple[8] = {
38-
mp_obj_new_int(tm.tm_year),
39-
mp_obj_new_int(tm.tm_mon),
40-
mp_obj_new_int(tm.tm_mday),
41-
mp_obj_new_int(tm.tm_hour),
42-
mp_obj_new_int(tm.tm_min),
43-
mp_obj_new_int(tm.tm_sec),
44-
mp_obj_new_int(tm.tm_wday),
45-
mp_obj_new_int(tm.tm_yday),
46-
};
47-
return mp_obj_new_tuple(8, tuple);
35+
timeutils_seconds_since_epoch_to_struct_time(ts.tv_sec, tm);
4836
}
4937

5038
// Return the number of seconds since the Epoch.

ports/samd/modtime.c

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,11 @@
3030

3131
static uint64_t time_us_64_offset_from_epoch;
3232

33-
// Return the localtime as an 8-tuple.
34-
static mp_obj_t mp_time_localtime_get(void) {
35-
timeutils_struct_time_t tm;
36-
rtc_gettime(&tm);
37-
tm.tm_wday = timeutils_calc_weekday(tm.tm_year, tm.tm_mon, tm.tm_mday);
38-
tm.tm_yday = timeutils_year_day(tm.tm_year, tm.tm_mon, tm.tm_mday);
39-
mp_obj_t tuple[8] = {
40-
tuple[0] = mp_obj_new_int(tm.tm_year),
41-
tuple[1] = mp_obj_new_int(tm.tm_mon),
42-
tuple[2] = mp_obj_new_int(tm.tm_mday),
43-
tuple[3] = mp_obj_new_int(tm.tm_hour),
44-
tuple[4] = mp_obj_new_int(tm.tm_min),
45-
tuple[5] = mp_obj_new_int(tm.tm_sec),
46-
tuple[6] = mp_obj_new_int(tm.tm_wday),
47-
tuple[7] = mp_obj_new_int(tm.tm_yday),
48-
};
49-
return mp_obj_new_tuple(8, tuple);
33+
// Get the localtime.
34+
static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
35+
rtc_gettime(tm);
36+
tm->tm_wday = timeutils_calc_weekday(tm->tm_year, tm->tm_mon, tm->tm_mday);
37+
tm->tm_yday = timeutils_year_day(tm->tm_year, tm->tm_mon, tm->tm_mday);
5038
}
5139

5240
// Returns the number of seconds, as an integer, since the Epoch.

ports/stm32/modtime.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,23 @@
2828
#include "shared/timeutils/timeutils.h"
2929
#include "rtc.h"
3030

31-
// Return the localtime as an 8-tuple.
32-
static mp_obj_t mp_time_localtime_get(void) {
31+
// Get the localtime.
32+
static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
3333
// get current date and time
3434
// note: need to call get time then get date to correctly access the registers
3535
rtc_init_finalise();
3636
RTC_DateTypeDef date;
3737
RTC_TimeTypeDef time;
3838
HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN);
3939
HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN);
40-
mp_obj_t tuple[8] = {
41-
mp_obj_new_int(2000 + date.Year),
42-
mp_obj_new_int(date.Month),
43-
mp_obj_new_int(date.Date),
44-
mp_obj_new_int(time.Hours),
45-
mp_obj_new_int(time.Minutes),
46-
mp_obj_new_int(time.Seconds),
47-
mp_obj_new_int(date.WeekDay - 1),
48-
mp_obj_new_int(timeutils_year_day(2000 + date.Year, date.Month, date.Date)),
49-
};
50-
return mp_obj_new_tuple(8, tuple);
40+
tm->tm_year = 2000 + date.Year;
41+
tm->tm_mon = date.Month;
42+
tm->tm_mday = date.Date;
43+
tm->tm_hour = time.Hours;
44+
tm->tm_min = time.Minutes;
45+
tm->tm_sec = time.Seconds;
46+
tm->tm_wday = date.WeekDay - 1;
47+
tm->tm_yday = timeutils_year_day(tm->tm_year, date.Month, date.Date);
5148
}
5249

5350
// Returns the number of seconds, as an integer, since 1/1/2000.

ports/webassembly/modtime.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,9 @@
2828
#include "shared/timeutils/timeutils.h"
2929
#include "library.h"
3030

31-
// Return the localtime as an 8-tuple.
32-
static mp_obj_t mp_time_localtime_get(void) {
33-
timeutils_struct_time_t tm;
34-
timeutils_seconds_since_epoch_to_struct_time(mp_hal_time_ms() / 1000, &tm);
35-
mp_obj_t tuple[8] = {
36-
mp_obj_new_int(tm.tm_year),
37-
mp_obj_new_int(tm.tm_mon),
38-
mp_obj_new_int(tm.tm_mday),
39-
mp_obj_new_int(tm.tm_hour),
40-
mp_obj_new_int(tm.tm_min),
41-
mp_obj_new_int(tm.tm_sec),
42-
mp_obj_new_int(tm.tm_wday),
43-
mp_obj_new_int(tm.tm_yday),
44-
};
45-
return mp_obj_new_tuple(8, tuple);
31+
// Get the localtime.
32+
static void mp_time_localtime_get(timeutils_struct_time_t *tm) {
33+
timeutils_seconds_since_epoch_to_struct_time(mp_hal_time_ms() / 1000, tm);
4634
}
4735

4836
// Returns the number of seconds, as a float, since the Epoch.

0 commit comments

Comments
 (0)