Skip to content

Commit

Permalink
lib/timeutils: Provide simple impl of extra funcs when Epoch is 1970.
Browse files Browse the repository at this point in the history
Dates/times must be post 2000/1/1 to work correctly with these simple
implementations.

Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed Jan 29, 2021
1 parent 75fea33 commit 33f1038
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/timeutils/timeutils.c
Expand Up @@ -158,7 +158,7 @@ mp_uint_t timeutils_seconds_since_2000(mp_uint_t year, mp_uint_t month,
+ (year - 2000) * 31536000;
}

mp_uint_t timeutils_mktime(mp_uint_t year, mp_int_t month, mp_int_t mday,
mp_uint_t timeutils_mktime_2000(mp_uint_t year, mp_int_t month, mp_int_t mday,
mp_int_t hours, mp_int_t minutes, mp_int_t seconds) {

// Normalize the tuple. This allows things like:
Expand Down
12 changes: 11 additions & 1 deletion lib/timeutils/timeutils.h
Expand Up @@ -52,12 +52,21 @@ 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 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_2000(mp_uint_t year, mp_int_t month, mp_int_t mday,
mp_int_t hours, mp_int_t minutes, mp_int_t seconds);

// Select the Epoch used by the port.
#if MICROPY_EPOCH_IS_1970

static inline void timeutils_seconds_since_epoch_to_struct_time(uint64_t t, timeutils_struct_time_t *tm) {
// TODO this will give incorrect results for dates before 2000/1/1
return timeutils_seconds_since_2000_to_struct_time(t - TIMEUTILS_SECONDS_1970_TO_2000, tm);

This comment has been minimized.

Copy link
@gvanem

gvanem Jun 7, 2021

This is a void function. Should it not be:

static inline void timeutils_seconds_since_epoch_to_struct_time(uint64_t t, timeutils_struct_time_t *tm) {
    // TODO this will give incorrect results for dates before 2000/1/1
    timeutils_seconds_since_2000_to_struct_time(t - TIMEUTILS_SECONDS_1970_TO_2000, tm);
}

This comment has been minimized.

Copy link
@dpgeorge

dpgeorge Jun 8, 2021

Author Member

As far as I can tell, this is allowed by the C standard, and the compilers are not complaining about it.

This comment has been minimized.

Copy link
@gvanem

gvanem Jun 9, 2021

True, but MSVC/clang-cl gives an annoying warning. Should it?

This comment has been minimized.

Copy link
@dpgeorge

dpgeorge Jun 9, 2021

Author Member

Really? The clang and macOS CI do not fail (but maybe they don't build this particular line..).

So we should fix it then.

This comment has been minimized.

Copy link
@stinos

stinos Jun 9, 2021

Contributor

Pretty sure cl is wrong here. Couldn't immediately find the bug report though. Change compiler flags to not emit this warning (you're probably building with way more warnings enabled than what we use by default for msvc)?

This comment has been minimized.

Copy link
@dpgeorge

dpgeorge Jun 15, 2021

Author Member

If this needs follow-up then please create a new issue for it.

}

static inline uint64_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) {
return timeutils_mktime_2000(year, month, mday, hours, minutes, seconds) + TIMEUTILS_SECONDS_1970_TO_2000;
}

static inline uint64_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) {
return timeutils_seconds_since_2000(year, month, date, hour, minute, second) + TIMEUTILS_SECONDS_1970_TO_2000;
Expand All @@ -75,6 +84,7 @@ static inline uint64_t timeutils_nanoseconds_since_epoch_to_nanoseconds_since_19

#define timeutils_seconds_since_epoch_to_struct_time timeutils_seconds_since_2000_to_struct_time
#define timeutils_seconds_since_epoch timeutils_seconds_since_2000
#define timeutils_mktime timeutils_mktime_2000

static inline uint64_t timeutils_seconds_since_epoch_to_nanoseconds_since_1970(mp_uint_t s) {
return ((uint64_t)s + TIMEUTILS_SECONDS_1970_TO_2000) * 1000000000ULL;
Expand Down

0 comments on commit 33f1038

Please sign in to comment.