Skip to content

Commit a0ee55c

Browse files
committed
time: Add time module to provide strftime.
1 parent d717b04 commit a0ee55c

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

python-stdlib/time/manifest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
metadata(version="0.1")
2+
3+
module("time.py")

python-stdlib/time/time.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import re
2+
from utime import *
3+
from micropython import const
4+
5+
_TS_YEAR = const(0)
6+
_TS_MON = const(1)
7+
_TS_MDAY = const(2)
8+
_TS_HOUR = const(3)
9+
_TS_MIN = const(4)
10+
_TS_SEC = const(5)
11+
_TS_WDAY = const(6)
12+
_TS_YDAY = const(7)
13+
_TS_ISDST = const(8)
14+
15+
_WDAY = const(("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
16+
_MDAY = const(
17+
(
18+
"January",
19+
"February",
20+
"March",
21+
"April",
22+
"May",
23+
"June",
24+
"July",
25+
"August",
26+
"September",
27+
"October",
28+
"November",
29+
"December",
30+
)
31+
)
32+
33+
_time_regex = None
34+
35+
36+
def strftime(datefmt, ts):
37+
from io import StringIO
38+
39+
fmtsp = False
40+
ftime = StringIO()
41+
for k in datefmt:
42+
if fmtsp:
43+
if k == "a":
44+
ftime.write(_WDAY[ts[_TS_WDAY]][0:3])
45+
elif k == "A":
46+
ftime.write(_WDAY[ts[_TS_WDAY]])
47+
elif k == "b":
48+
ftime.write(_MDAY[ts[_TS_MON] - 1][0:3])
49+
elif k == "B":
50+
ftime.write(_MDAY[ts[_TS_MON] - 1])
51+
elif k == "d":
52+
ftime.write("%02d" % ts[_TS_MDAY])
53+
elif k == "H":
54+
ftime.write("%02d" % ts[_TS_HOUR])
55+
elif k == "I":
56+
ftime.write("%02d" % (ts[_TS_HOUR] % 12))
57+
elif k == "j":
58+
ftime.write("%03d" % ts[_TS_YDAY])
59+
elif k == "m":
60+
ftime.write("%02d" % ts[_TS_MON])
61+
elif k == "M":
62+
ftime.write("%02d" % ts[_TS_MIN])
63+
elif k == "P":
64+
ftime.write("AM" if ts[_TS_HOUR] < 12 else "PM")
65+
elif k == "S":
66+
ftime.write("%02d" % ts[_TS_SEC])
67+
elif k == "w":
68+
ftime.write(str(ts[_TS_WDAY]))
69+
elif k == "y":
70+
ftime.write("%02d" % (ts[_TS_YEAR] % 100))
71+
elif k == "Y":
72+
ftime.write(str(ts[_TS_YEAR]))
73+
else:
74+
ftime.write(k)
75+
fmtsp = False
76+
elif k == "%":
77+
fmtsp = True
78+
else:
79+
ftime.write(k)
80+
val = ftime.getvalue()
81+
ftime.close()
82+
return val
83+
84+
85+
if __name__ == "__main__":
86+
import time
87+
88+
fmt = "%Y-%m-%d %a %b %I:%M:%S %%%P%%"
89+
print(strftime(fmt, time.localtime()))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy