@@ -130,76 +130,6 @@ def _format_offset(off):
130130 return s
131131
132132
133- # pylint: disable=invalid-name, too-many-locals, too-many-nested-blocks, too-many-branches, too-many-statements
134- def _wrap_strftime (time_obj , strftime_fmt , timetuple ):
135- # Don't call utcoffset() or tzname() unless actually needed.
136- f_replace = None # the string to use for %f
137- z_replace = None # the string to use for %z
138- Z_replace = None # the string to use for %Z
139-
140- # Scan strftime_fmt for %z and %Z escapes, replacing as needed.
141- newformat = []
142- push = newformat .append
143- i , n = 0 , len (strftime_fmt )
144- while i < n :
145- ch = strftime_fmt [i ]
146- i += 1
147- if ch == "%" :
148- if i < n :
149- ch = strftime_fmt [i ]
150- i += 1
151- if ch == "f" :
152- if f_replace is None :
153- f_replace = "%06d" % getattr (time_obj , "microsecond" , 0 )
154- newformat .append (f_replace )
155- elif ch == "z" :
156- if z_replace is None :
157- z_replace = ""
158- if hasattr (time_obj , "utcoffset" ):
159- offset = time_obj .utcoffset ()
160- if offset is not None :
161- sign = "+"
162- if offset .days < 0 :
163- offset = - offset
164- sign = "-"
165- h , rest = divmod (offset , timedelta (hours = 1 ))
166- m , rest = divmod (rest , timedelta (minutes = 1 ))
167- s = rest .seconds
168- u = offset .microseconds
169- if u :
170- z_replace = "%c%02d%02d%02d.%06d" % (
171- sign ,
172- h ,
173- m ,
174- s ,
175- u ,
176- )
177- elif s :
178- z_replace = "%c%02d%02d%02d" % (sign , h , m , s )
179- else :
180- z_replace = "%c%02d%02d" % (sign , h , m )
181- assert "%" not in z_replace
182- newformat .append (z_replace )
183- elif ch == "Z" :
184- if Z_replace is None :
185- Z_replace = ""
186- if hasattr (time_obj , "tzname" ):
187- s = time_obj .tzname ()
188- if s is not None :
189- # strftime is going to have at this: escape %
190- Z_replace = s .replace ("%" , "%%" )
191- newformat .append (Z_replace )
192- else :
193- push ("%" )
194- push (ch )
195- else :
196- push ("%" )
197- else :
198- push (ch )
199- newformat = "" .join (newformat )
200- return _time .strftime (newformat , timetuple )
201-
202-
203133# Utility functions - timezone
204134def _check_tzname (name ):
205135 """"Just raise TypeError if the arg isn't None or a string."""
@@ -370,7 +300,7 @@ def _ord2ymd(n):
370300class timedelta :
371301 """A timedelta object represents a duration, the difference between two dates or times."""
372302
373- # pylint: disable=too-many-arguments
303+ # pylint: disable=too-many-arguments, too-many-locals, too-many-statements
374304 def __new__ (
375305 cls ,
376306 days = 0 ,
@@ -859,13 +789,15 @@ def __new__(cls, offset, name=_Omitted):
859789 raise ValueError (
860790 "offset must be a timedelta" " representing a whole number of minutes"
861791 )
792+ cls ._offset = offset
793+ cls ._name = name
862794 return cls ._create (offset , name )
863795
864- # pylint: disable=protected-access
796+ # pylint: disable=protected-access, bad-super-call
865797 @classmethod
866798 def _create (cls , offset , name = None ):
867799 """High-level creation for a timezone object."""
868- self = tzinfo .__new__ (cls )
800+ self = super ( tzinfo , cls ) .__new__ (cls )
869801 self ._offset = offset
870802 self ._name = name
871803 return self
@@ -998,15 +930,6 @@ def isoformat(self, timespec="auto"):
998930 # For a time t, str(t) is equivalent to t.isoformat()
999931 __str__ = isoformat
1000932
1001- def strftime (self , fmt ):
1002- """Format using strftime(). The date part of the timestamp passed
1003- to underlying strftime should not be used.
1004- """
1005- # The year must be >= 1000 else Python's strftime implementation
1006- # can raise a bogus exception.
1007- timetuple = (1900 , 1 , 1 , self ._hour , self ._minute , self ._second , 0 , 1 , - 1 )
1008- return _wrap_strftime (self , fmt , timetuple )
1009-
1010933 def utcoffset (self ):
1011934 """Return the timezone offset in minutes east of UTC (negative west of
1012935 UTC)."""
@@ -1123,8 +1046,6 @@ def _tzstr(self, sep=":"):
11231046 def __format__ (self , fmt ):
11241047 if not isinstance (fmt , str ):
11251048 raise TypeError ("must be str, not %s" % type (fmt ).__name__ )
1126- if len (fmt ) != 0 :
1127- return self .strftime (fmt )
11281049 return str (self )
11291050
11301051 def __repr__ (self ):
@@ -1259,7 +1180,11 @@ def _fromtimestamp(cls, t, utc, tz):
12591180 t -= 1
12601181 us += 1000000
12611182
1262- converter = _time .gmtime if utc else _time .localtime
1183+ if utc :
1184+ raise NotImplementedError (
1185+ "CircuitPython does not currently implement time.gmtime."
1186+ )
1187+ converter = _time .localtime
12631188 struct_time = converter (t )
12641189 ss = min (struct_time [5 ], 59 ) # clamp out leap seconds if the platform has them
12651190 result = cls (
@@ -1272,39 +1197,7 @@ def _fromtimestamp(cls, t, utc, tz):
12721197 us ,
12731198 tz ,
12741199 )
1275- if tz is None :
1276- # As of version 2015f max fold in IANA database is
1277- # 23 hours at 1969-09-30 13:00:00 in Kwajalein.
1278- # Let's probe 24 hours in the past to detect a transition:
1279- max_fold_seconds = 24 * 3600
1280-
1281- struct_time = converter (t - max_fold_seconds )[:6 ]
1282- probe1 = cls (
1283- struct_time [0 ],
1284- struct_time [1 ],
1285- struct_time [2 ],
1286- struct_time [3 ],
1287- struct_time [4 ],
1288- struct_time [5 ],
1289- us ,
1290- tz ,
1291- )
1292- trans = result - probe1 - timedelta (0 , max_fold_seconds )
1293- if trans .days < 0 :
1294- struct_time = converter (t + trans // timedelta (0 , 1 ))[:6 ]
1295- probe2 = cls (
1296- struct_time [0 ],
1297- struct_time [1 ],
1298- struct_time [2 ],
1299- struct_time [3 ],
1300- struct_time [4 ],
1301- struct_time [5 ],
1302- us ,
1303- tz ,
1304- )
1305- if probe2 == result :
1306- result ._fold = 1
1307- else :
1200+ if tz is not None :
13081201 result = tz .fromutc (result )
13091202 return result
13101203
@@ -1316,7 +1209,7 @@ def fromtimestamp(cls, timestamp, tz=None):
13161209 @classmethod
13171210 def now (cls , timezone = None ):
13181211 """Return the current local date and time."""
1319- return cls .fromtimestamp (_time .time (), timezone )
1212+ return cls .fromtimestamp (_time .time (), tz = timezone )
13201213
13211214 @classmethod
13221215 def utcfromtimestamp (cls , timestamp ):
@@ -1449,19 +1342,18 @@ def weekday(self):
14491342 """Return the day of the week as an integer, where Monday is 0 and Sunday is 6."""
14501343 return (self .toordinal () + 6 ) % 7
14511344
1452- def strftime (self , fmt ):
1453- """Format using strftime(). The date part of the timestamp passed
1454- to underlying strftime should not be used.
1455- """
1456- # The year must be >= 1000 else Python's strftime implementation
1457- # can raise a bogus exception.
1458- timetuple = (1900 , 1 , 1 , self ._hour , self ._minute , self ._second , 0 , 1 , - 1 )
1459- return _wrap_strftime (self , fmt , timetuple )
1460-
1461- def __format__ (self , fmt ):
1462- if len (fmt ) != 0 :
1463- return self .strftime (fmt )
1464- return str (self )
1345+ def ctime (self ):
1346+ "Return string representing the datetime."
1347+ weekday = self .toordinal () % 7 or 7
1348+ return "%s %s %2d %02d:%02d:%02d %04d" % (
1349+ _DAYNAMES [weekday ],
1350+ _MONTHNAMES [self ._month ],
1351+ self ._day ,
1352+ self ._hour ,
1353+ self ._minute ,
1354+ self ._second ,
1355+ self ._year ,
1356+ )
14651357
14661358 def __repr__ (self ):
14671359 """Convert to formal string, for repr()."""
0 commit comments