From a3a14b9db79d3af24e724832092d1861ec231365 Mon Sep 17 00:00:00 2001 From: Dave Hylands Date: Mon, 18 May 2015 08:26:58 -0700 Subject: 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. --- lib/timeutils/timeutils.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/timeutils/timeutils.c') diff --git a/lib/timeutils/timeutils.c b/lib/timeutils/timeutils.c index 77e5f043f..94bdada98 100644 --- a/lib/timeutils/timeutils.c +++ b/lib/timeutils/timeutils.c @@ -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; -- cgit v1.2.3