summaryrefslogtreecommitdiff
path: root/tests/unix/time.py
diff options
context:
space:
mode:
authorAndrew Leech <andrew.leech@planetinnovation.com.au>2019-11-26 11:02:19 +1100
committerDamien George <damien.p.george@gmail.com>2019-12-28 11:11:54 +1100
commit1b844e908c3a867be1e54efdb86a04c6a62b2f26 (patch)
treebed11a7695c36120d49887a0076336c1c669a7c1 /tests/unix/time.py
parentb23bd6433cd68830d32c0c68dbbf554942bb6ad0 (diff)
unix/modtime: Add utime.mktime function, to complement utime.localtime.
This also adds it to the windows port.
Diffstat (limited to 'tests/unix/time.py')
-rw-r--r--tests/unix/time.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/unix/time.py b/tests/unix/time.py
new file mode 100644
index 000000000..a96c3f5b6
--- /dev/null
+++ b/tests/unix/time.py
@@ -0,0 +1,44 @@
+try:
+ import utime as time
+except ImportError:
+ import time
+
+DAYS_PER_MONTH = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
+
+tzseconds = -time.mktime((1970, 1, 1, 0, 0, 0, 0, 0, 0))
+
+def is_leap(year):
+ return (year % 4) == 0
+
+def test():
+ seconds = 0
+ wday = 3 # Jan 1, 1970 was a Thursday
+ for year in range(1970, 2038):
+ print("Testing %d" % year)
+ yday = 1
+ for month in range(1, 13):
+ if month == 2 and is_leap(year):
+ DAYS_PER_MONTH[2] = 29
+ else:
+ DAYS_PER_MONTH[2] = 28
+ for day in range(1, DAYS_PER_MONTH[month] + 1):
+ secs = time.mktime((year, month, day, 0, 0, 0, 0, 0, 0)) + tzseconds
+ if secs != seconds:
+ print("mktime failed for %d-%02d-%02d got %d expected %d" % (year, month, day, secs, seconds))
+ return
+ tuple = time.localtime(seconds)
+ secs = time.mktime(tuple)
+ if secs != seconds:
+ print("localtime failed for %d-%02d-%02d got %d expected %d" % (year, month, day, secs, seconds))
+ return
+ seconds += 86400
+ if yday != tuple[7]:
+ print("locatime for %d-%02d-%02d got yday %d, expecting %d" % (year, month, day, tuple[7], yday))
+ return
+ if wday != tuple[6]:
+ print("locatime for %d-%02d-%02d got wday %d, expecting %d" % (year, month, day, tuple[6], wday))
+ return
+ yday += 1
+ wday = (wday + 1) % 7
+
+test()