Skip to content

Commit 949758a

Browse files
committed
time: Add time module to provide strftime.
1 parent 70e422d commit 949758a

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

python-stdlib/time/metadata.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
srctype=micropython-lib
2+
type=module
3+
version = 0.0.1

python-stdlib/time/setup.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
3+
# Remove current dir from sys.path, otherwise setuptools will peek up our
4+
# module instead of system's.
5+
sys.path.pop(0)
6+
from setuptools import setup
7+
8+
sys.path.append("..")
9+
import sdist_upip
10+
11+
setup(
12+
name="micropython-time",
13+
version="0.0.1",
14+
description="time module for MicroPython",
15+
long_description="This is a module reimplemented specifically for MicroPython standard library,\nwith efficient and lean design in mind. Note that this module is likely work\nin progress and likely supports just a subset of CPython's corresponding\nmodule. Please help with the development if you are interested in this\nmodule.",
16+
url="https://github.com/micropython/micropython-lib",
17+
author="micropython-lib Developers",
18+
author_email="micro-python@googlegroups.com",
19+
maintainer="micropython-lib Developers",
20+
maintainer_email="micro-python@googlegroups.com",
21+
license="MIT",
22+
cmdclass={"sdist": sdist_upip.sdist},
23+
py_modules=["time"],
24+
)

python-stdlib/time/time.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import re
2+
3+
_TS_YEAR=0
4+
_TS_MON=1
5+
_TS_MDAY=2
6+
_TS_HOUR=3
7+
_TS_MIN=4
8+
_TS_SEC=5
9+
_TS_WDAY=6
10+
_TS_YDAY=7
11+
_TS_ISDST=8
12+
13+
_WDAY = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
14+
"Saturday", "Sunday"]
15+
_MDAY = ["January", "February", "March", "April", "May", "June",
16+
"July", "August", "September", "October", "November", "December"]
17+
18+
_time_regex = re.compile(r"%(-?[a-zA-Z])")
19+
20+
def strftime(datefmt, ts):
21+
return _time_regex.sub(r"%(\1)s", datefmt) % {
22+
"a" : _WDAY[ts[_TS_WDAY]][0:3],
23+
"A" : _WDAY[ts[_TS_WDAY]],
24+
"b" : _MDAY[ts[_TS_MON]][0:3],
25+
"B" : _MDAY[ts[_TS_MON]],
26+
"d" : f"{ts[_TS_MDAY]:02d}",
27+
"H" : f"{ts[_TS_HOUR]:02d}",
28+
"I" : f"{ts[_TS_HOUR]%12:02d}",
29+
"j" : f"{ts[_TS_YDAY]:03d}",
30+
"m" : f"{ts[_TS_MON]:02d}",
31+
"M" : f"{ts[_TS_MIN]:02d}",
32+
"P" : "AM" if ts[_TS_HOUR] < 12 else "PM",
33+
"S" : f"{ts[_TS_SEC]:02d}",
34+
"w" : f"{ts[_TS_WDAY]}",
35+
"y" : f"{ts[_TS_YEAR]%100:02d}",
36+
"Y" : f"{ts[_TS_YEAR]}",
37+
}

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