Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/timeutils: Fix problem with dates prior to Mar 1, 2000 #1270

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/timeutils/timeutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ void timeutils_seconds_since_2000_to_struct_time(mp_uint_t t, timeutils_struct_t

mp_int_t days = seconds / 86400;
seconds %= 86400;
if (seconds < 0) {
seconds += 86400;
days -= 1;
}
tm->tm_hour = seconds / 3600;
tm->tm_min = seconds / 60 % 60;
tm->tm_sec = seconds % 60;
Expand Down
21 changes: 21 additions & 0 deletions tests/pyb/modtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,25 @@ def test():
yday += 1
wday = (wday + 1) % 7

def spot_test(seconds, expected_time):
actual_time = time.localtime(seconds)
for i in range(len(actual_time)):
if actual_time[i] != expected_time[i]:
print("time.localtime(", seconds, ") returned", actual_time, "expecting", expected_time)
return
print("time.localtime(", seconds, ") returned", actual_time, "(pass)")


test()
spot_test( 0, (2000, 1, 1, 0, 0, 0, 5, 1))
spot_test( 1, (2000, 1, 1, 0, 0, 1, 5, 1))
spot_test( 59, (2000, 1, 1, 0, 0, 59, 5, 1))
spot_test( 60, (2000, 1, 1, 0, 1, 0, 5, 1))
spot_test( 3599, (2000, 1, 1, 0, 59, 59, 5, 1))
spot_test( 3600, (2000, 1, 1, 1, 0, 0, 5, 1))
spot_test( -1, (1999, 12, 31, 23, 59, 59, 4, 365))
spot_test( 447549467, (2014, 3, 7, 23, 17, 47, 4, 66))
spot_test( -940984933, (1970, 3, 7, 23, 17, 47, 5, 66))
spot_test(-1072915199, (1966, 1, 1, 0, 0, 1, 5, 1))
spot_test(-1072915200, (1966, 1, 1, 0, 0, 0, 5, 1))
spot_test(-1072915201, (1965, 12, 31, 23, 59, 59, 4, 365))
12 changes: 12 additions & 0 deletions tests/pyb/modtime.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ Testing 2030
Testing 2031
Testing 2032
Testing 2033
time.localtime( 0 ) returned (2000, 1, 1, 0, 0, 0, 5, 1) (pass)
time.localtime( 1 ) returned (2000, 1, 1, 0, 0, 1, 5, 1) (pass)
time.localtime( 59 ) returned (2000, 1, 1, 0, 0, 59, 5, 1) (pass)
time.localtime( 60 ) returned (2000, 1, 1, 0, 1, 0, 5, 1) (pass)
time.localtime( 3599 ) returned (2000, 1, 1, 0, 59, 59, 5, 1) (pass)
time.localtime( 3600 ) returned (2000, 1, 1, 1, 0, 0, 5, 1) (pass)
time.localtime( -1 ) returned (1999, 12, 31, 23, 59, 59, 4, 365) (pass)
time.localtime( 447549467 ) returned (2014, 3, 7, 23, 17, 47, 4, 66) (pass)
time.localtime( -940984933 ) returned (1970, 3, 7, 23, 17, 47, 5, 66) (pass)
time.localtime( -1072915199 ) returned (1966, 1, 1, 0, 0, 1, 5, 1) (pass)
time.localtime( -1072915200 ) returned (1966, 1, 1, 0, 0, 0, 5, 1) (pass)
time.localtime( -1072915201 ) returned (1965, 12, 31, 23, 59, 59, 4, 365) (pass)