Fix os.stat() to use 1970 epoch
Commit 95e70cd0ea
'time: Use 1970 epoch' changed epoch for the time
module, but not for other users. This patch does the same for the only
other core timeutils user: extmod/vfs_fat.c:fat_vfs_stat().
Other timeutils users: cc3200, esp8266 and stm32, are not changed.
Ports that don't use long ints, will still get wrong time values from
os.stat().
This commit is contained in:
parent
d1b588375b
commit
39ee12d1ac
@ -320,7 +320,7 @@ STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) {
|
|||||||
} else {
|
} else {
|
||||||
mode |= MP_S_IFREG;
|
mode |= MP_S_IFREG;
|
||||||
}
|
}
|
||||||
mp_int_t seconds = timeutils_seconds_since_2000(
|
mp_uint_t seconds = timeutils_seconds_since_epoch(
|
||||||
1980 + ((fno.fdate >> 9) & 0x7f),
|
1980 + ((fno.fdate >> 9) & 0x7f),
|
||||||
(fno.fdate >> 5) & 0x0f,
|
(fno.fdate >> 5) & 0x0f,
|
||||||
fno.fdate & 0x1f,
|
fno.fdate & 0x1f,
|
||||||
@ -335,9 +335,9 @@ STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) {
|
|||||||
t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
|
t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
|
||||||
t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
|
t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
|
||||||
t->items[6] = mp_obj_new_int_from_uint(fno.fsize); // st_size
|
t->items[6] = mp_obj_new_int_from_uint(fno.fsize); // st_size
|
||||||
t->items[7] = MP_OBJ_NEW_SMALL_INT(seconds); // st_atime
|
t->items[7] = mp_obj_new_int_from_uint(seconds); // st_atime
|
||||||
t->items[8] = MP_OBJ_NEW_SMALL_INT(seconds); // st_mtime
|
t->items[8] = mp_obj_new_int_from_uint(seconds); // st_mtime
|
||||||
t->items[9] = MP_OBJ_NEW_SMALL_INT(seconds); // st_ctime
|
t->items[9] = mp_obj_new_int_from_uint(seconds); // st_ctime
|
||||||
|
|
||||||
return MP_OBJ_FROM_PTR(t);
|
return MP_OBJ_FROM_PTR(t);
|
||||||
}
|
}
|
||||||
|
@ -158,6 +158,17 @@ mp_uint_t timeutils_seconds_since_2000(mp_uint_t year, mp_uint_t month,
|
|||||||
+ (year - 2000) * 31536000;
|
+ (year - 2000) * 31536000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void timeutils_seconds_since_epoch_to_struct_time(mp_uint_t t, timeutils_struct_time_t *tm) {
|
||||||
|
t -= EPOCH1970_EPOCH2000_DIFF_SECS;
|
||||||
|
timeutils_seconds_since_2000_to_struct_time(t, tm);
|
||||||
|
}
|
||||||
|
|
||||||
|
mp_uint_t timeutils_seconds_since_epoch(mp_uint_t year, mp_uint_t month, mp_uint_t date,
|
||||||
|
mp_uint_t hour, mp_uint_t minute, mp_uint_t second) {
|
||||||
|
mp_uint_t t = timeutils_seconds_since_2000(year, month, date, hour, minute, second);
|
||||||
|
return t + EPOCH1970_EPOCH2000_DIFF_SECS;
|
||||||
|
}
|
||||||
|
|
||||||
mp_uint_t timeutils_mktime(mp_uint_t year, mp_int_t month, mp_int_t mday,
|
mp_uint_t timeutils_mktime(mp_uint_t year, mp_int_t month, mp_int_t mday,
|
||||||
mp_int_t hours, mp_int_t minutes, mp_int_t seconds) {
|
mp_int_t hours, mp_int_t minutes, mp_int_t seconds) {
|
||||||
|
|
||||||
@ -211,5 +222,5 @@ mp_uint_t timeutils_mktime(mp_uint_t year, mp_int_t month, mp_int_t mday,
|
|||||||
year++;
|
year++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return timeutils_seconds_since_2000(year, month, mday, hours, minutes, seconds);
|
return timeutils_seconds_since_epoch(year, month, mday, hours, minutes, seconds);
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,8 @@
|
|||||||
#ifndef MICROPY_INCLUDED_LIB_TIMEUTILS_TIMEUTILS_H
|
#ifndef MICROPY_INCLUDED_LIB_TIMEUTILS_TIMEUTILS_H
|
||||||
#define MICROPY_INCLUDED_LIB_TIMEUTILS_TIMEUTILS_H
|
#define MICROPY_INCLUDED_LIB_TIMEUTILS_TIMEUTILS_H
|
||||||
|
|
||||||
|
#define EPOCH1970_EPOCH2000_DIFF_SECS 946684800
|
||||||
|
|
||||||
typedef struct _timeutils_struct_time_t {
|
typedef struct _timeutils_struct_time_t {
|
||||||
uint16_t tm_year; // i.e. 2014
|
uint16_t tm_year; // i.e. 2014
|
||||||
uint8_t tm_mon; // 1..12
|
uint8_t tm_mon; // 1..12
|
||||||
@ -48,6 +50,11 @@ void timeutils_seconds_since_2000_to_struct_time(mp_uint_t t,
|
|||||||
mp_uint_t timeutils_seconds_since_2000(mp_uint_t year, mp_uint_t month,
|
mp_uint_t timeutils_seconds_since_2000(mp_uint_t year, mp_uint_t month,
|
||||||
mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second);
|
mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second);
|
||||||
|
|
||||||
|
void timeutils_seconds_since_epoch_to_struct_time(mp_uint_t t, timeutils_struct_time_t *tm);
|
||||||
|
|
||||||
|
mp_uint_t timeutils_seconds_since_epoch(mp_uint_t year, mp_uint_t month, mp_uint_t date,
|
||||||
|
mp_uint_t hour, mp_uint_t minute, mp_uint_t second);
|
||||||
|
|
||||||
mp_uint_t timeutils_mktime(mp_uint_t year, mp_int_t month, mp_int_t mday,
|
mp_uint_t timeutils_mktime(mp_uint_t year, mp_int_t month, mp_int_t mday,
|
||||||
mp_int_t hours, mp_int_t minutes, mp_int_t seconds);
|
mp_int_t hours, mp_int_t minutes, mp_int_t seconds);
|
||||||
|
|
||||||
|
@ -36,8 +36,6 @@
|
|||||||
#include "shared-bindings/time/__init__.h"
|
#include "shared-bindings/time/__init__.h"
|
||||||
#include "supervisor/shared/translate.h"
|
#include "supervisor/shared/translate.h"
|
||||||
|
|
||||||
#define EPOCH1970_EPOCH2000_DIFF_SECS 946684800
|
|
||||||
|
|
||||||
//| :mod:`time` --- time and timing related functions
|
//| :mod:`time` --- time and timing related functions
|
||||||
//| ========================================================
|
//| ========================================================
|
||||||
//|
|
//|
|
||||||
@ -142,9 +140,9 @@ const mp_obj_namedtuple_type_t struct_time_type_obj = {
|
|||||||
|
|
||||||
mp_obj_t struct_time_from_tm(timeutils_struct_time_t *tm) {
|
mp_obj_t struct_time_from_tm(timeutils_struct_time_t *tm) {
|
||||||
timeutils_struct_time_t tmp;
|
timeutils_struct_time_t tmp;
|
||||||
mp_uint_t secs = timeutils_seconds_since_2000(tm->tm_year, tm->tm_mon, tm->tm_mday,
|
mp_uint_t secs = timeutils_seconds_since_epoch(tm->tm_year, tm->tm_mon, tm->tm_mday,
|
||||||
tm->tm_hour, tm->tm_min, tm->tm_sec);
|
tm->tm_hour, tm->tm_min, tm->tm_sec);
|
||||||
timeutils_seconds_since_2000_to_struct_time(secs, &tmp);
|
timeutils_seconds_since_epoch_to_struct_time(secs, &tmp);
|
||||||
tm->tm_wday = tmp.tm_wday;
|
tm->tm_wday = tmp.tm_wday;
|
||||||
tm->tm_yday = tmp.tm_yday;
|
tm->tm_yday = tmp.tm_yday;
|
||||||
|
|
||||||
@ -202,9 +200,9 @@ mp_obj_t MP_WEAK rtc_get_time_source_time(void) {
|
|||||||
STATIC mp_obj_t time_time(void) {
|
STATIC mp_obj_t time_time(void) {
|
||||||
timeutils_struct_time_t tm;
|
timeutils_struct_time_t tm;
|
||||||
struct_time_to_tm(rtc_get_time_source_time(), &tm);
|
struct_time_to_tm(rtc_get_time_source_time(), &tm);
|
||||||
mp_uint_t secs = timeutils_seconds_since_2000(tm.tm_year, tm.tm_mon, tm.tm_mday,
|
mp_uint_t secs = timeutils_seconds_since_epoch(tm.tm_year, tm.tm_mon, tm.tm_mday,
|
||||||
tm.tm_hour, tm.tm_min, tm.tm_sec);
|
tm.tm_hour, tm.tm_min, tm.tm_sec);
|
||||||
return mp_obj_new_int_from_uint(secs + EPOCH1970_EPOCH2000_DIFF_SECS);
|
return mp_obj_new_int_from_uint(secs);
|
||||||
}
|
}
|
||||||
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
|
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
|
||||||
|
|
||||||
@ -228,7 +226,7 @@ STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
|
|||||||
mp_raise_msg(&mp_type_OverflowError, translate("timestamp out of range for platform time_t"));
|
mp_raise_msg(&mp_type_OverflowError, translate("timestamp out of range for platform time_t"));
|
||||||
|
|
||||||
timeutils_struct_time_t tm;
|
timeutils_struct_time_t tm;
|
||||||
timeutils_seconds_since_2000_to_struct_time(secs - EPOCH1970_EPOCH2000_DIFF_SECS, &tm);
|
timeutils_seconds_since_epoch_to_struct_time(secs, &tm);
|
||||||
|
|
||||||
return struct_time_from_tm(&tm);
|
return struct_time_from_tm(&tm);
|
||||||
}
|
}
|
||||||
@ -262,7 +260,7 @@ STATIC mp_obj_t time_mktime(mp_obj_t t) {
|
|||||||
|
|
||||||
mp_uint_t secs = timeutils_mktime(mp_obj_get_int(elem[0]), mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]),
|
mp_uint_t secs = timeutils_mktime(mp_obj_get_int(elem[0]), mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]),
|
||||||
mp_obj_get_int(elem[3]), mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5]));
|
mp_obj_get_int(elem[3]), mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5]));
|
||||||
return mp_obj_new_int_from_uint(secs + EPOCH1970_EPOCH2000_DIFF_SECS);
|
return mp_obj_new_int_from_uint(secs);
|
||||||
}
|
}
|
||||||
MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
|
MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
|
||||||
#endif // MICROPY_LONGINT_IMPL
|
#endif // MICROPY_LONGINT_IMPL
|
||||||
|
@ -5,7 +5,7 @@ statvfs: (512, 512, 16, 16, 16, 0, 0, 0, 0, 255)
|
|||||||
getcwd: /
|
getcwd: /
|
||||||
True
|
True
|
||||||
[('foo_file.txt', 32768, 0, 6)]
|
[('foo_file.txt', 32768, 0, 6)]
|
||||||
stat root: (16384, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
stat root: (16384, 0, 0, 0, 0, 0, 0, 946684800, 946684800, 946684800)
|
||||||
stat file: (32768, 0, 0, 0, 0, 0, 6)
|
stat file: (32768, 0, 0, 0, 0, 0, 6)
|
||||||
True
|
True
|
||||||
True
|
True
|
||||||
|
Loading…
Reference in New Issue
Block a user