Skip to content

time: Add time module to provide strftime. #508

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

Merged
merged 2 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python-stdlib/time/manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
metadata(version="0.1")

module("time.py")
42 changes: 42 additions & 0 deletions python-stdlib/time/test_time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import time
import unittest

DAYS = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

MONTHS = (
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
)

TIME_TUPLE = (2022, 12, 14, 0, 45, 17, 2, 348, 0)


class TestStrftime(unittest.TestCase):
def test_not_formatting(self):
fmt = "a string with no formatting {}[]() 0123456789 !@#$^&*"
self.assertEqual(time.strftime(fmt, TIME_TUPLE), fmt)

def test_days(self):
for i, day in enumerate(DAYS):
t = (0, 0, 0, 0, 0, 0, i, 0, 0)
self.assertEqual(time.strftime("%a%A", t), day[:3] + day)

def test_months(self):
for i, month in enumerate(MONTHS):
t = (0, i + 1, 0, 0, 0, 0, 0, 0, 0)
self.assertEqual(time.strftime("%b%B", t), month[:3] + month)

def test_full(self):
fmt = "%Y-%m-%d %a %b %I:%M:%S %%%P%%"
expected = "2022-12-14 Wed Dec 00:45:17 %AM%"
self.assertEqual(time.strftime(fmt, TIME_TUPLE), expected)
79 changes: 79 additions & 0 deletions python-stdlib/time/time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from utime import *
from micropython import const

_TS_YEAR = const(0)
_TS_MON = const(1)
_TS_MDAY = const(2)
_TS_HOUR = const(3)
_TS_MIN = const(4)
_TS_SEC = const(5)
_TS_WDAY = const(6)
_TS_YDAY = const(7)
_TS_ISDST = const(8)

_WDAY = const(("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
_MDAY = const(
(
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
)
)


def strftime(datefmt, ts):
from io import StringIO

fmtsp = False
ftime = StringIO()
for k in datefmt:
if fmtsp:
if k == "a":
ftime.write(_WDAY[ts[_TS_WDAY]][0:3])
elif k == "A":
ftime.write(_WDAY[ts[_TS_WDAY]])
elif k == "b":
ftime.write(_MDAY[ts[_TS_MON] - 1][0:3])
elif k == "B":
ftime.write(_MDAY[ts[_TS_MON] - 1])
elif k == "d":
ftime.write("%02d" % ts[_TS_MDAY])
elif k == "H":
ftime.write("%02d" % ts[_TS_HOUR])
elif k == "I":
ftime.write("%02d" % (ts[_TS_HOUR] % 12))
elif k == "j":
ftime.write("%03d" % ts[_TS_YDAY])
elif k == "m":
ftime.write("%02d" % ts[_TS_MON])
elif k == "M":
ftime.write("%02d" % ts[_TS_MIN])
elif k == "P":
ftime.write("AM" if ts[_TS_HOUR] < 12 else "PM")
elif k == "S":
ftime.write("%02d" % ts[_TS_SEC])
elif k == "w":
ftime.write(str(ts[_TS_WDAY]))
elif k == "y":
ftime.write("%02d" % (ts[_TS_YEAR] % 100))
elif k == "Y":
ftime.write(str(ts[_TS_YEAR]))
else:
ftime.write(k)
fmtsp = False
elif k == "%":
fmtsp = True
else:
ftime.write(k)
val = ftime.getvalue()
ftime.close()
return val