@@ -341,7 +341,8 @@ def array_strptime(object[:] values, object fmt,
341
341
return result, result_timezone.base
342
342
343
343
344
- """ _getlang, LocaleTime, TimeRE, _calc_julian_from_U_or_W are vendored
344
+ """
345
+ _getlang, LocaleTime, TimeRE, _calc_julian_from_U_or_W are vendored
345
346
from the standard library, see
346
347
https://github.com/python/cpython/blob/master/Lib/_strptime.py
347
348
The original module-level docstring follows.
@@ -363,7 +364,8 @@ def _getlang():
363
364
364
365
365
366
class LocaleTime :
366
- """ Stores and handles locale-specific information related to time.
367
+ """
368
+ Stores and handles locale-specific information related to time.
367
369
368
370
ATTRIBUTES:
369
371
f_weekday -- full weekday names (7-item list)
@@ -382,7 +384,8 @@ class LocaleTime:
382
384
"""
383
385
384
386
def __init__ (self ):
385
- """ Set all attributes.
387
+ """
388
+ Set all attributes.
386
389
387
390
Order of methods called matters for dependency reasons.
388
391
@@ -399,7 +402,6 @@ class LocaleTime:
399
402
Only other possible issue is if someone changed the timezone and did
400
403
not call tz.tzset . That is an issue for the programmer, though,
401
404
since changing the timezone is worthless without that call.
402
-
403
405
"""
404
406
self .lang = _getlang()
405
407
self .__calc_weekday()
@@ -518,15 +520,16 @@ class TimeRE(dict):
518
520
"""
519
521
520
522
def __init__ (self , locale_time = None ):
521
- """ Create keys/values.
523
+ """
524
+ Create keys/values.
522
525
523
526
Order of execution is important for dependency reasons.
524
-
525
527
"""
526
528
if locale_time:
527
529
self .locale_time = locale_time
528
530
else :
529
531
self .locale_time = LocaleTime()
532
+ self ._Z = None
530
533
base = super ()
531
534
base.__init__ ({
532
535
# The " \d" part of the regex is to make %c from ANSI C work
@@ -555,21 +558,29 @@ class TimeRE(dict):
555
558
' B' : self .__seqToRE(self .locale_time.f_month[1 :], ' B' ),
556
559
' b' : self .__seqToRE(self .locale_time.a_month[1 :], ' b' ),
557
560
' p' : self .__seqToRE(self .locale_time.am_pm, ' p' ),
558
- ' Z' : self .__seqToRE(pytz.all_timezones, ' Z ' ),
561
+ # 'Z' key is generated lazily via __getitem__
559
562
' %' : ' %' })
560
563
base.__setitem__ (' W' , base.__getitem__ (' U' ).replace(' U' , ' W' ))
561
564
base.__setitem__ (' c' , self .pattern(self .locale_time.LC_date_time))
562
565
base.__setitem__ (' x' , self .pattern(self .locale_time.LC_date))
563
566
base.__setitem__ (' X' , self .pattern(self .locale_time.LC_time))
564
567
568
+ def __getitem__ (self , key ):
569
+ if key == " Z" :
570
+ # lazy computation
571
+ if self ._Z is None :
572
+ self ._Z = self .__seqToRE(pytz.all_timezones, ' Z' )
573
+ return self ._Z
574
+ return super ().__getitem__(key)
575
+
565
576
def __seqToRE (self , to_convert , directive ):
566
- """ Convert a list to a regex string for matching a directive.
577
+ """
578
+ Convert a list to a regex string for matching a directive.
567
579
568
580
Want possible matching values to be from longest to shortest. This
569
581
prevents the possibility of a match occurring for a value that also
570
582
a substring of a larger value that should have matched (e.g., 'abc'
571
583
matching when 'abcdef' should have been the match).
572
-
573
584
"""
574
585
to_convert = sorted (to_convert, key = len , reverse = True )
575
586
for value in to_convert:
@@ -582,11 +593,11 @@ class TimeRE(dict):
582
593
return ' %s )' % regex
583
594
584
595
def pattern (self , format ):
585
- """ Return regex pattern for the format string.
596
+ """
597
+ Return regex pattern for the format string.
586
598
587
599
Need to make sure that any characters that might be interpreted as
588
600
regex syntax are escaped.
589
-
590
601
"""
591
602
processed_format = ' '
592
603
# The sub() call escapes all characters that might be misconstrued
@@ -619,7 +630,8 @@ _regex_cache = {}
619
630
620
631
cdef int _calc_julian_from_U_or_W(int year, int week_of_year,
621
632
int day_of_week, int week_starts_Mon):
622
- """ Calculate the Julian day based on the year, week of the year, and day of
633
+ """
634
+ Calculate the Julian day based on the year, week of the year, and day of
623
635
the week, with week_start_day representing whether the week of the year
624
636
assumes the week starts on Sunday or Monday (6 or 0).
625
637
@@ -660,8 +672,10 @@ cdef int _calc_julian_from_U_or_W(int year, int week_of_year,
660
672
return 1 + days_to_week + day_of_week
661
673
662
674
663
- cdef object _calc_julian_from_V(int iso_year, int iso_week, int iso_weekday):
664
- """ Calculate the Julian day based on the ISO 8601 year, week, and weekday.
675
+ cdef (int , int ) _calc_julian_from_V(int iso_year, int iso_week, int iso_weekday):
676
+ """
677
+ Calculate the Julian day based on the ISO 8601 year, week, and weekday.
678
+
665
679
ISO weeks start on Mondays, with week 01 being the week containing 4 Jan.
666
680
ISO week days range from 1 (Monday) to 7 (Sunday).
667
681
@@ -694,7 +708,7 @@ cdef object _calc_julian_from_V(int iso_year, int iso_week, int iso_weekday):
694
708
return iso_year, ordinal
695
709
696
710
697
- cdef parse_timezone_directive(object z):
711
+ cdef parse_timezone_directive(str z):
698
712
"""
699
713
Parse the '%z ' directive and return a pytz.FixedOffset
700
714
0 commit comments