8000 lib: Fix some issues in timeutils · micropython/micropython@a3a14b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit a3a14b9

Browse files
dhylandsPaul Sokolovsky
authored and
Paul Sokolovsky
committed
lib: Fix some issues in timeutils
In particular, dates prior to Mar 1, 2000 are screwed up. The easiest way to see this is to do: >>> import time >>> time.localtime(0) (2000, 1, 1, 0, 0, 0, 5, 1) >>> time.localtime(1) (2000, 1, 2, 233, 197, 197, 6, 2) With this patch, we instead get: >>> import time >>> time.localtime(1) (2000, 1, 1, 0, 0, 1, 5, 1) Doh - In C % is NOT a modulo operator, it's a remainder operator.
1 parent 6f1cffe commit a3a14b9

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

lib/timeutils/timeutils.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ void timeutils_seconds_since_2000_to_struct_time(mp_uint_t t, timeutils_struct_t
7373

7474
mp_int_t da 8000 ys = seconds / 86400;
7575
seconds %= 86400;
76+
if (seconds < 0) {
77+
seconds += 86400;
78+
days -= 1;
79+
}
7680
tm->tm_hour = seconds / 3600;
7781
tm->tm_min = seconds / 60 % 60;
7882
tm->tm_sec = seconds % 60;

tests/pyb/modtime.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,25 @@ def test():
3535
yday += 1
3636
wday = (wday + 1) % 7
3737

38+
def spot_test(seconds, expected_time):
39+
actual_time = time.localtime(seconds)
40+
for i in range(len(actual_time)):
41+
if actual_time[i] != expected_time[i]:
42+
print("time.localtime(", seconds, ") returned", actual_time, "expecting", expected_time)
43+
return
44+
print("time.localtime(", seconds, ") returned", actual_time, "(pass)")
45+
46+
3847
test()
48+
spot_test( 0, (2000, 1, 1, 0, 0, 0, 5, 1))
49+
spot_test( 1, (2000, 1, 1, 0, 0, 1, 5, 1))
50+
spot_test( 59, (2000, 1, 1, 0, 0, 59, 5, 1))
51+
spot_test( 60, (2000, 1, 1, 0, 1, 0, 5, 1))
52+
spot_test( 3599, (2000, 1, 1, 0, 59, 59, 5, 1))
53+
spot_test( 3600, (2000, 1, 1, 1, 0, 0, 5, 1))
54+
spot_test( -1, (1999, 12, 31, 23, 59, 59, 4, 365))
55+
spot_test( 447549467, (2014, 3, 7, 23, 17, 47, 4, 66))
56+
spot_test( -940984933, (1970, 3, 7, 23, 17, 47, 5, 66))
57+
spot_test(-1072915199, (1966, 1, 1, 0, 0, 1, 5, 1))
58+
spot_test(-1072915200, (1966, 1, 1, 0, 0, 0, 5, 1))
59+
spot_test(-1072915201, (1965, 12, 31, 23, 59, 59, 4, 365))

tests/pyb/modtime.py.exp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,15 @@ Testing 2030
3232
Testing 2031
3333
Testing 2032
3434
Testing 2033
35+
time.localtime( 0 ) returned (2000, 1, 1, 0, 0, 0, 5, 1) (pass)
36+
time.localtime( 1 ) returned (2000, 1, 1, 0, 0, 1, 5, 1) (pass)
37+
time.localtime( 59 ) returned (2000, 1, 1, 0, 0, 59, 5, 1) (pass)
38+
time.localtime( 60 ) returned (2000, 1, 1, 0, 1, 0, 5, 1) (pass)
39+
time.localtime( 3599 ) returned (2000, 1, 1, 0, 59, 59, 5, 1) (pass)
40+
time.localtime( 3600 ) returned (2000, 1, 1, 1, 0, 0, 5, 1) (pass)
41+
time.localtime( -1 ) returned (1999, 12, 31, 23, 59, 59, 4, 365) (pass)
42+
time.localtime( 447549467 ) returned (2014, 3, 7, 23, 17, 47, 4, 66) (pass)
43+
time.localtime( -940984933 ) returned (1970, 3, 7, 23, 17, 47, 5, 66) (pass)
44+
time.localtime( -1072915199 ) returned (1966, 1, 1, 0, 0, 1, 5, 1) (pass)
45+
time.localtime( -1072915200 ) returned (1966, 1, 1, 0, 0, 0, 5, 1) (pass)
46+
time.localtime( -1072915201 ) returned (1965, 12, 31, 23, 59, 59, 4, 365) (pass)

0 commit comments

Comments
 (0)
0