Skip to content

Commit df05cae

Browse files
yoctopucedpgeorge
authored andcommitted
shared/timeutils: Standardize supported date range on all platforms.
This is code makes sure that time functions work properly on a reasonable date range, on all platforms, regardless of the epoch. The suggested minimum range is 1970 to 2099. In order to reduce code footprint, code to support far away dates is only enabled specified by the port. New types are defined to identify timestamps. The implementation with the smallest code footprint is when support timerange is limited to 1970-2099 and Epoch is 1970. This makes it possible to use 32 bit unsigned integers for all timestamps. On ARM4F, adding support for dates up to year 3000 adds 460 bytes of code. Supporting dates back to 1600 adds another 44 bytes of code. Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
1 parent c4a88f2 commit df05cae

File tree

18 files changed

+387
-106
lines changed

18 files changed

+387
-106
lines changed

extmod/modtime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
5858
return mp_time_localtime_get();
5959
} else {
6060
// Convert given seconds to tuple.
61-
mp_int_t seconds = mp_obj_get_int(args[0]);
61+
mp_timestamp_t seconds = timeutils_obj_get_timestamp(args[0]);
6262
timeutils_struct_time_t tm;
6363
timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
6464
mp_obj_t tuple[8] = {
@@ -90,7 +90,7 @@ static mp_obj_t time_mktime(mp_obj_t tuple) {
9090
mp_raise_TypeError(MP_ERROR_TEXT("mktime needs a tuple of length 8 or 9"));
9191
}
9292

93-
return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]),
93+
return timeutils_obj_from_timestamp(timeutils_mktime(mp_obj_get_int(elem[0]),
9494
mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]),
9595
mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])));
9696
}

extmod/vfs_fat.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ static mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) {
326326
} else {
327327
mode |= MP_S_IFREG;
328328
}
329-
mp_int_t seconds = timeutils_seconds_since_epoch(
329+
mp_timestamp_t seconds = timeutils_seconds_since_epoch(
330330
1980 + ((fno.fdate >> 9) & 0x7f),
331331
(fno.fdate >> 5) & 0x0f,
332332
fno.fdate & 0x1f,
@@ -341,9 +341,9 @@ static mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) {
341341
t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
342342
t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
343343
t->items[6] = mp_obj_new_int_from_uint(fno.fsize); // st_size
344-
t->items[7] = mp_obj_new_int_from_uint(seconds); // st_atime
345-
t->items[8] = mp_obj_new_int_from_uint(seconds); // st_mtime
346-
t->items[9] = mp_obj_new_int_from_uint(seconds); // st_ctime
344+
t->items[7] = timeutils_obj_from_timestamp(seconds); // st_atime
345+
t->items[8] = timeutils_obj_from_timestamp(seconds); // st_mtime
346+
t->items[9] = timeutils_obj_from_timestamp(seconds); // st_ctime
347347

348348
return MP_OBJ_FROM_PTR(t);
349349
}

extmod/vfs_lfsx.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ static mp_obj_t MP_VFS_LFSx(stat)(mp_obj_t self_in, mp_obj_t path_in) {
378378
mp_raise_OSError(-ret);
379379
}
380380

381-
mp_uint_t mtime = 0;
381+
mp_timestamp_t mtime = 0;
382382
#if LFS_BUILD_VERSION == 2
383383
uint8_t mtime_buf[8];
384384
lfs2_ssize_t sz = lfs2_getattr(&self->lfs, path, LFS_ATTR_MTIME, &mtime_buf, sizeof(mtime_buf));
@@ -400,9 +400,9 @@ static mp_obj_t MP_VFS_LFSx(stat)(mp_obj_t self_in, mp_obj_t path_in) {
400400
t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
401401
t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
402402
t->items[6] = mp_obj_new_int_from_uint(info.size); // st_size
403-
t->items[7] = mp_obj_new_int_from_uint(mtime); // st_atime
404-
t->items[8] = mp_obj_new_int_from_uint(mtime); // st_mtime
405-
t->items[9] = mp_obj_new_int_from_uint(mtime); // st_ctime
403+
t->items[7] = timeutils_obj_from_timestamp(mtime); // st_atime
404+
t->items[8] = timeutils_obj_from_timestamp(mtime); // st_mtime
405+
t->items[9] = timeutils_obj_from_timestamp(mtime); // st_ctime
406406

407407
return MP_OBJ_FROM_PTR(t);
408408
}

ports/cc3200/mods/modtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@ static mp_obj_t mp_time_localtime_get(void) {
5050

5151
// Returns the number of seconds, as an integer, since the Epoch.
5252
static mp_obj_t mp_time_time_get(void) {
53-
return mp_obj_new_int(pyb_rtc_get_seconds());
53+
return timeutils_obj_from_timestamp(pyb_rtc_get_seconds());
5454
}

ports/esp32/modtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ static mp_obj_t mp_time_localtime_get(void) {
5454
static mp_obj_t mp_time_time_get(void) {
5555
struct timeval tv;
5656
gettimeofday(&tv, NULL);
57-
return mp_obj_new_int(tv.tv_sec);
57+
return timeutils_obj_from_timestamp(tv.tv_sec);
5858
}

ports/esp8266/modtime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
// Return the localtime as an 8-tuple.
3333
static mp_obj_t mp_time_localtime_get(void) {
34-
mp_int_t seconds = pyb_rtc_get_us_since_epoch() / 1000 / 1000;
34+
mp_uint_t seconds = pyb_rtc_get_us_since_epoch() / 1000u / 1000u;
3535
timeutils_struct_time_t tm;
3636
timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
3737
mp_obj_t tuple[8] = {
@@ -50,5 +50,5 @@ static mp_obj_t mp_time_localtime_get(void) {
5050
// Returns the number of seconds, as an integer, since the Epoch.
5151
static mp_obj_t mp_time_time_get(void) {
5252
// get date and time
53-
return mp_obj_new_int(pyb_rtc_get_us_since_epoch() / 1000 / 1000);
53+
return timeutils_obj_from_timestamp(pyb_rtc_get_us_since_epoch() / 1000 / 1000);
5454
}

ports/mimxrt/modtime.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,7 @@ static mp_obj_t mp_time_localtime_get(void) {
5151
static mp_obj_t mp_time_time_get(void) {
5252
snvs_lp_srtc_datetime_t t;
5353
SNVS_LP_SRTC_GetDatetime(SNVS, &t);
54-
// EPOCH is 1970 for this port, which leads to the following trouble:
55-
// timeutils_seconds_since_epoch() calls timeutils_seconds_since_2000(), and
56-
// timeutils_seconds_since_2000() subtracts 2000 from year, but uses
57-
// an unsigned number for seconds, That causes an underrun, which is not
58-
// fixed by adding the TIMEUTILS_SECONDS_1970_TO_2000.
59-
// Masking it to 32 bit for year < 2000 fixes it.
60-
return mp_obj_new_int_from_ull(
54+
return timeutils_obj_from_timestamp(
6155
timeutils_seconds_since_epoch(t.year, t.month, t.day, t.hour, t.minute, t.second)
62-
& (t.year < 2000 ? 0xffffffff : 0xffffffffffff)
6356
);
6457
}

ports/renesas-ra/modtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ static mp_obj_t mp_time_time_get(void) {
5353
rtc_init_finalise();
5454
ra_rtc_t time;
5555
ra_rtc_get_time(&time);
56-
return mp_obj_new_int(timeutils_seconds_since_epoch(time.year, time.month, time.date, time.hour, time.minute, time.second));
56+
return timeutils_obj_from_timestamp(timeutils_seconds_since_epoch(time.year, time.month, time.date, time.hour, time.minute, time.second));
5757
}

ports/stm32/modtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@ static mp_obj_t mp_time_time_get(void) {
5959
RTC_TimeTypeDef time;
6060
HAL_RTC_GetTime(&RTCHandle, &time, RTC_FORMAT_BIN);
6161
HAL_RTC_GetDate(&RTCHandle, &date, RTC_FORMAT_BIN);
62-
return mp_obj_new_int(timeutils_seconds_since_epoch(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds));
62+
return timeutils_obj_from_timestamp(timeutils_seconds_since_epoch(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds));
6363
}

ports/stm32/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#ifndef MICROPY_FLOAT_IMPL // can be configured by each board via mpconfigboard.mk
7777
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
7878
#endif
79+
#define MICROPY_TIME_SUPPORT_Y1969_AND_BEFORE (1)
7980
#define MICROPY_USE_INTERNAL_ERRNO (1)
8081
#define MICROPY_SCHEDULER_STATIC_NODES (1)
8182
#define MICROPY_SCHEDULER_DEPTH (8)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy