|
8 | 8 | _DIM = (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
|
9 | 9 | _TIME_SPEC = ("auto", "hours", "minutes", "seconds", "milliseconds", "microseconds")
|
10 | 10 |
|
| 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 | + |
11 | 42 |
|
12 | 43 | def _leap(y):
|
13 | 44 | return y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)
|
@@ -56,6 +87,89 @@ def _o2ymd(n):
|
56 | 87 | return y, m, n + 1
|
57 | 88 |
|
58 | 89 |
|
| 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 | + |
59 | 173 | MINYEAR = 1
|
60 | 174 | MAXYEAR = 9_999
|
61 | 175 |
|
@@ -395,6 +509,10 @@ def isoformat(self):
|
395 | 509 | def __repr__(self):
|
396 | 510 | return "datetime.date(0, 0, {})".format(self._ord)
|
397 | 511 |
|
| 512 | + def strftime(self, fmt): |
| 513 | + "Format using strftime()." |
| 514 | + return _wrap_strftime(self, fmt, self.timetuple()) |
| 515 | + |
398 | 516 | __str__ = isoformat
|
399 | 517 |
|
400 | 518 | def __hash__(self):
|
|
0 commit comments