Skip to content

Commit d6b4a32

Browse files
committed
datetime: Added strftime
Added strftime to support new calendar module. strftime does not support all formatting options. Only enough required for calendar.
1 parent 03c48fa commit d6b4a32

File tree

3 files changed

+120
-2
lines changed

3 files changed

+120
-2
lines changed

python-stdlib/datetime/datetime.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,37 @@
88
_DIM = (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
99
_TIME_SPEC = ("auto", "hours", "minutes", "seconds", "milliseconds", "microseconds")
1010

11+
_SHORT_WEEKDAY = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
12+
_LONG_WEEKDAY = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
13+
_FULL_MONTH_NAME = [
14+
"January",
15+
"February",
16+
"March",
17+
"April",
18+
"May",
19+
"June",
20+
"July",
21+
"August",
22+
"September",
23+
"October",
24+
"November",
25+
"December",
26+
]
27+
_ABBREVIATED_MONTH = [
28+
"Jan",
29+
"Feb",
30+
"Mar",
31+
"Apr",
32+
"May",
33+
"Jun",
34+
"Jul",
35+
"Aug",
36+
"Sep",
37+
"Oct",
38+
"Nov",
39+
"Dec",
40+
]
41+
1142

1243
def _leap(y):
1344
return y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)
@@ -56,6 +87,89 @@ def _o2ymd(n):
5687
return y, m, n + 1
5788

5889

90+
def _strftime(newformat, timetuple):
91+
# No strftime function in built-ins. Implement basic formatting on an as-needed
92+
# basis here.
93+
if newformat == "%a":
94+
return _SHORT_WEEKDAY[timetuple[6]]
95+
if newformat == "%A":
96+
return _LONG_WEEKDAY[timetuple[6]]
97+
if newformat == "%b":
98+
return _ABBREVIATED_MONTH[timetuple[1] - 1]
99+
if newformat == "%B":
100+
return _FULL_MONTH_NAME[timetuple[1] - 1]
101+
102+
# Format not implemented.
103+
raise NotImplementedError(
104+
f"Unknown format for strftime, format={newformat}, timetuple={timetuple}"
105+
)
106+
107+
108+
# Correctly substitute for %z and %Z escapes in strftime formats.
109+
def _wrap_strftime(object, format, timetuple):
110+
# Don't call utcoffset() or tzname() unless actually needed.
111+
freplace = None # the string to use for %f
112+
zreplace = None # the string to use for %z
113+
Zreplace = None # the string to use for %Z
114+
115+
# Scan format for %z and %Z escapes, replacing as needed.
116+
newformat = []
117+
push = newformat.append
118+
i, n = 0, len(format)
119+
while i < n:
120+
ch = format[i]
121+
i += 1
122+
if ch == "%":
123+
if i < n:
124+
ch = format[i]
125+
i += 1
126+
if ch == "f":
127+
if freplace is None:
128+
freplace = "%06d" % getattr(object, "microsecond", 0)
129+
newformat.append(freplace)
130+
elif ch == "z":
131+
if zreplace is None:
132+
zreplace = ""
133+
if hasattr(object, "utcoffset"):
134+
offset = object.utcoffset()
135+
if offset is not None:
136+
sign = "+"
137+
if offset.days < 0:
138+
offset = -offset
139+
sign = "-"
140+
h, rest = divmod(offset, timedelta(hours=1))
141+
m, rest = divmod(rest, timedelta(minutes=1))
142+
s = rest.seconds
143+
u = offset.microseconds
144+
if u:
145+
zreplace = "%c%02d%02d%02d.%06d" % (sign, h, m, s, u)
146+
elif s:
147+
zreplace = "%c%02d%02d%02d" % (sign, h, m, s)
148+
else:
149+
zreplace = "%c%02d%02d" % (sign, h, m)
150+
assert "%" not in zreplace
151+
newformat.append(zreplace)
152+
elif ch == "Z":
153+
if Zreplace is None:
154+
Zreplace = ""
155+
if hasattr(object, "tzname"):
156+
s = object.tzname()
157+
if s is not None:
158+
# strftime is going to have at this: escape %
159+
Zreplace = s.replace("%", "%%")
160+
newformat.append(Zreplace)
161+
else:
162+
push("%")
163+
push(ch)
164+
else:
165+
push("%")
166+
else:
167+
push(ch)
168+
newformat = "".join(newformat)
169+
170+
return _strftime(newformat, timetuple)
171+
172+
59173
MINYEAR = 1
60174
MAXYEAR = 9_999
61175

@@ -395,6 +509,10 @@ def isoformat(self):
395509
def __repr__(self):
396510
return "datetime.date(0, 0, {})".format(self._ord)
397511

512+
def strftime(self, fmt):
513+
"Format using strftime()."
514+
return _wrap_strftime(self, fmt, self.timetuple())
515+
398516
__str__ = isoformat
399517

400518
def __hash__(self):

python-stdlib/datetime/metadata.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
srctype = micropython-lib
22
type = module
3-
version = 4.0.0
3+
version = 4.0.1
44
author = Lorenzo Cappelletti

python-stdlib/datetime/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
setup(
1212
name="micropython-datetime",
13-
version="4.0.0",
13+
version="4.0.1",
1414
description="datetime module for MicroPython",
1515
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.",
1616
url="https://github.com/micropython/micropython-lib",

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