annotate Lib/_strptime.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents cbc9dc1c977e
children d234dd21374a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Strptime-related classes and functions.
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 CLASSES:
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
4 LocaleTime -- Discovers and stores locale-specific time information
25372
caa3a2fc6a20 Brett's fixes for various bugs and coding issues. Closes SF patch #
Barry Warsaw <barry@python.org>
parents: 25071
diff changeset
5 TimeRE -- Creates regexes for pattern matching a string of text containing
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
6 time information
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 FUNCTIONS:
28243
f13adc00c165 SF patch #691928: Use datetime in _strptime
Raymond Hettinger <python@rcn.com>
parents: 27507
diff changeset
9 _getlang -- Figure out what language is being used for the locale
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 strptime -- Calculates the time struct represented by the passed-in string
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 """
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 import time
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 import locale
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 import calendar
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 from re import compile as re_compile
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 from re import IGNORECASE
33674
d99de65f5425 Locale data that contains regex metacharacters are now properly escaped.
Brett Cannon <bcannon@gmail.com>
parents: 31967
diff changeset
18 from re import escape as re_escape
28243
f13adc00c165 SF patch #691928: Use datetime in _strptime
Raymond Hettinger <python@rcn.com>
parents: 27507
diff changeset
19 from datetime import date as datetime_date
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
20 try:
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
21 from thread import allocate_lock as _thread_allocate_lock
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
22 except:
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
23 from dummy_thread import allocate_lock as _thread_allocate_lock
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
45767
212e9c8ad087 add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents: 41938
diff changeset
25 __all__ = []
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
27293
bfc140c7e3d0 SF patch 670194: Performance enhancement for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27288
diff changeset
27 def _getlang():
bfc140c7e3d0 SF patch 670194: Performance enhancement for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27288
diff changeset
28 # Figure out what the current language is set to.
29991
b33c60fcce74 Remove caching of TimeRE (and thus LocaleTime) instance. Error was being
Brett Cannon <bcannon@gmail.com>
parents: 29831
diff changeset
29 return locale.getlocale(locale.LC_TIME)
25071
05b15ce28e31 Many hopefully benign style clean ups. Still passes the test suite of
Barry Warsaw <barry@python.org>
parents: 25070
diff changeset
30
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 class LocaleTime(object):
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 """Stores and handles locale-specific information related to time.
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
34 ATTRIBUTES:
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 f_weekday -- full weekday names (7-item list)
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 a_weekday -- abbreviated weekday names (7-item list)
30089
ba48e571a4d5 Fix docstring in LocaleTime.
Brett Cannon <bcannon@gmail.com>
parents: 30050
diff changeset
37 f_month -- full month names (13-item list; dummy value in [0], which
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 is added by code)
30089
ba48e571a4d5 Fix docstring in LocaleTime.
Brett Cannon <bcannon@gmail.com>
parents: 30050
diff changeset
39 a_month -- abbreviated month names (13-item list, dummy value in
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 [0], which is added by code)
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 am_pm -- AM/PM representation (2-item list)
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 LC_date_time -- format string for date/time representation (string)
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 LC_date -- format string for date representation (string)
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 LC_time -- format string for time representation (string)
24674
3269bdd65988 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24322
diff changeset
45 timezone -- daylight- and non-daylight-savings timezone representation
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
46 (2-item list of sets)
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
47 lang -- Language used by instance (2-item tuple)
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 """
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
50 def __init__(self):
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
51 """Set all attributes.
30698
3e3fd56ce595 Let library modules use the new keyword arguments for list.sort().
Raymond Hettinger <python@rcn.com>
parents: 30228
diff changeset
52
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
53 Order of methods called matters for dependency reasons.
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
54
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
55 The locale language is set at the offset and then checked again before
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
56 exiting. This is to make sure that the attributes were not set with a
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
57 mix of information from more than one locale. This would most likely
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
58 happen when using threads where one thread calls a locale-dependent
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
59 function while another thread changes the locale while the function in
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
60 the other thread is still running. Proper coding would call for
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
61 locks to prevent changing the locale while locale-dependent code is
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
62 running. The check here is done in case someone does not think about
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
63 doing this.
30117
40aa4a10f49b Fix handling of bad locale setup where time.tzname[0] == time.tzname[1] and
Brett Cannon <bcannon@gmail.com>
parents: 30089
diff changeset
64
40aa4a10f49b Fix handling of bad locale setup where time.tzname[0] == time.tzname[1] and
Brett Cannon <bcannon@gmail.com>
parents: 30089
diff changeset
65 Only other possible issue is if someone changed the timezone and did
40aa4a10f49b Fix handling of bad locale setup where time.tzname[0] == time.tzname[1] and
Brett Cannon <bcannon@gmail.com>
parents: 30089
diff changeset
66 not call tz.tzset . That is an issue for the programmer, though,
40aa4a10f49b Fix handling of bad locale setup where time.tzname[0] == time.tzname[1] and
Brett Cannon <bcannon@gmail.com>
parents: 30089
diff changeset
67 since changing the timezone is worthless without that call.
30698
3e3fd56ce595 Let library modules use the new keyword arguments for list.sort().
Raymond Hettinger <python@rcn.com>
parents: 30228
diff changeset
68
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
69 """
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
70 self.lang = _getlang()
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
71 self.__calc_weekday()
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
72 self.__calc_month()
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
73 self.__calc_am_pm()
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
74 self.__calc_timezone()
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
75 self.__calc_date_time()
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
76 if _getlang() != self.lang:
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
77 raise ValueError("locale changed during initialization")
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 def __pad(self, seq, front):
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
80 # Add '' to seq to either the front (is True), else the back.
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 seq = list(seq)
25071
05b15ce28e31 Many hopefully benign style clean ups. Still passes the test suite of
Barry Warsaw <barry@python.org>
parents: 25070
diff changeset
82 if front:
05b15ce28e31 Many hopefully benign style clean ups. Still passes the test suite of
Barry Warsaw <barry@python.org>
parents: 25070
diff changeset
83 seq.insert(0, '')
05b15ce28e31 Many hopefully benign style clean ups. Still passes the test suite of
Barry Warsaw <barry@python.org>
parents: 25070
diff changeset
84 else:
05b15ce28e31 Many hopefully benign style clean ups. Still passes the test suite of
Barry Warsaw <barry@python.org>
parents: 25070
diff changeset
85 seq.append('')
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 return seq
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 def __calc_weekday(self):
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
89 # Set self.a_weekday and self.f_weekday using the calendar
25071
05b15ce28e31 Many hopefully benign style clean ups. Still passes the test suite of
Barry Warsaw <barry@python.org>
parents: 25070
diff changeset
90 # module.
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
91 a_weekday = [calendar.day_abbr[i].lower() for i in range(7)]
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
92 f_weekday = [calendar.day_name[i].lower() for i in range(7)]
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
93 self.a_weekday = a_weekday
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
94 self.f_weekday = f_weekday
24674
3269bdd65988 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24322
diff changeset
95
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 def __calc_month(self):
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
97 # Set self.f_month and self.a_month using the calendar module.
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
98 a_month = [calendar.month_abbr[i].lower() for i in range(13)]
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
99 f_month = [calendar.month_name[i].lower() for i in range(13)]
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
100 self.a_month = a_month
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
101 self.f_month = f_month
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 def __calc_am_pm(self):
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
104 # Set self.am_pm by using time.strftime().
24674
3269bdd65988 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24322
diff changeset
105
25071
05b15ce28e31 Many hopefully benign style clean ups. Still passes the test suite of
Barry Warsaw <barry@python.org>
parents: 25070
diff changeset
106 # The magic date (1999,3,17,hour,44,55,2,76,0) is not really that
05b15ce28e31 Many hopefully benign style clean ups. Still passes the test suite of
Barry Warsaw <barry@python.org>
parents: 25070
diff changeset
107 # magical; just happened to have used it everywhere else where a
05b15ce28e31 Many hopefully benign style clean ups. Still passes the test suite of
Barry Warsaw <barry@python.org>
parents: 25070
diff changeset
108 # static date was needed.
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 am_pm = []
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 for hour in (01,22):
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 time_tuple = time.struct_time((1999,3,17,hour,44,55,2,76,0))
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
112 am_pm.append(time.strftime("%p", time_tuple).lower())
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
113 self.am_pm = am_pm
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 def __calc_date_time(self):
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
116 # Set self.date_time, self.date, & self.time by using
25071
05b15ce28e31 Many hopefully benign style clean ups. Still passes the test suite of
Barry Warsaw <barry@python.org>
parents: 25070
diff changeset
117 # time.strftime().
24674
3269bdd65988 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24322
diff changeset
118
25071
05b15ce28e31 Many hopefully benign style clean ups. Still passes the test suite of
Barry Warsaw <barry@python.org>
parents: 25070
diff changeset
119 # Use (1999,3,17,22,44,55,2,76,0) for magic date because the amount of
05b15ce28e31 Many hopefully benign style clean ups. Still passes the test suite of
Barry Warsaw <barry@python.org>
parents: 25070
diff changeset
120 # overloaded numbers is minimized. The order in which searches for
05b15ce28e31 Many hopefully benign style clean ups. Still passes the test suite of
Barry Warsaw <barry@python.org>
parents: 25070
diff changeset
121 # values within the format string is very important; it eliminates
05b15ce28e31 Many hopefully benign style clean ups. Still passes the test suite of
Barry Warsaw <barry@python.org>
parents: 25070
diff changeset
122 # possible ambiguity for what something represents.
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 time_tuple = time.struct_time((1999,3,17,22,44,55,2,76,0))
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 date_time = [None, None, None]
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
125 date_time[0] = time.strftime("%c", time_tuple).lower()
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
126 date_time[1] = time.strftime("%x", time_tuple).lower()
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
127 date_time[2] = time.strftime("%X", time_tuple).lower()
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
128 replacement_pairs = [('%', '%%'), (self.f_weekday[2], '%A'),
25372
caa3a2fc6a20 Brett's fixes for various bugs and coding issues. Closes SF patch #
Barry Warsaw <barry@python.org>
parents: 25071
diff changeset
129 (self.f_month[3], '%B'), (self.a_weekday[2], '%a'),
caa3a2fc6a20 Brett's fixes for various bugs and coding issues. Closes SF patch #
Barry Warsaw <barry@python.org>
parents: 25071
diff changeset
130 (self.a_month[3], '%b'), (self.am_pm[1], '%p'),
caa3a2fc6a20 Brett's fixes for various bugs and coding issues. Closes SF patch #
Barry Warsaw <barry@python.org>
parents: 25071
diff changeset
131 ('1999', '%Y'), ('99', '%y'), ('22', '%H'),
caa3a2fc6a20 Brett's fixes for various bugs and coding issues. Closes SF patch #
Barry Warsaw <barry@python.org>
parents: 25071
diff changeset
132 ('44', '%M'), ('55', '%S'), ('76', '%j'),
caa3a2fc6a20 Brett's fixes for various bugs and coding issues. Closes SF patch #
Barry Warsaw <barry@python.org>
parents: 25071
diff changeset
133 ('17', '%d'), ('03', '%m'), ('3', '%m'),
caa3a2fc6a20 Brett's fixes for various bugs and coding issues. Closes SF patch #
Barry Warsaw <barry@python.org>
parents: 25071
diff changeset
134 # '3' needed for when no leading zero.
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
135 ('2', '%w'), ('10', '%I')]
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
136 replacement_pairs.extend([(tz, "%Z") for tz_values in self.timezone
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
137 for tz in tz_values])
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
138 for offset,directive in ((0,'%c'), (1,'%x'), (2,'%X')):
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
139 current_format = date_time[offset]
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
140 for old, new in replacement_pairs:
27258
e8c1a9542f66 Checking in Brett Cannon's patch #662053, which fixes bug #661354.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26887
diff changeset
141 # Must deal with possible lack of locale info
e8c1a9542f66 Checking in Brett Cannon's patch #662053, which fixes bug #661354.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26887
diff changeset
142 # manifesting itself as the empty string (e.g., Swedish's
e8c1a9542f66 Checking in Brett Cannon's patch #662053, which fixes bug #661354.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26887
diff changeset
143 # lack of AM/PM info) or a platform returning a tuple of empty
e8c1a9542f66 Checking in Brett Cannon's patch #662053, which fixes bug #661354.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26887
diff changeset
144 # strings (e.g., MacOS 9 having timezone as ('','')).
e8c1a9542f66 Checking in Brett Cannon's patch #662053, which fixes bug #661354.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26887
diff changeset
145 if old:
25372
caa3a2fc6a20 Brett's fixes for various bugs and coding issues. Closes SF patch #
Barry Warsaw <barry@python.org>
parents: 25071
diff changeset
146 current_format = current_format.replace(old, new)
35477
baed8b7deb56 Fix logic error introduced in last commit. Also add a comment to explain what
Brett Cannon <bcannon@gmail.com>
parents: 35475
diff changeset
147 # If %W is used, then Sunday, 2005-01-03 will fall on week 0 since
baed8b7deb56 Fix logic error introduced in last commit. Also add a comment to explain what
Brett Cannon <bcannon@gmail.com>
parents: 35475
diff changeset
148 # 2005-01-03 occurs before the first Monday of the year. Otherwise
baed8b7deb56 Fix logic error introduced in last commit. Also add a comment to explain what
Brett Cannon <bcannon@gmail.com>
parents: 35475
diff changeset
149 # %U is used.
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 time_tuple = time.struct_time((1999,1,3,1,1,1,6,3,0))
35475
89e8a5ec3da0 Fix bug where __contains__ should have been used over str.find()
Brett Cannon <bcannon@gmail.com>
parents: 33853
diff changeset
151 if '00' in time.strftime(directive, time_tuple):
35477
baed8b7deb56 Fix logic error introduced in last commit. Also add a comment to explain what
Brett Cannon <bcannon@gmail.com>
parents: 35475
diff changeset
152 U_W = '%W'
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 else:
35477
baed8b7deb56 Fix logic error introduced in last commit. Also add a comment to explain what
Brett Cannon <bcannon@gmail.com>
parents: 35475
diff changeset
154 U_W = '%U'
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 date_time[offset] = current_format.replace('11', U_W)
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
156 self.LC_date_time = date_time[0]
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
157 self.LC_date = date_time[1]
30698
3e3fd56ce595 Let library modules use the new keyword arguments for list.sort().
Raymond Hettinger <python@rcn.com>
parents: 30228
diff changeset
158 self.LC_time = date_time[2]
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 def __calc_timezone(self):
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
161 # Set self.timezone by using time.tzname.
30117
40aa4a10f49b Fix handling of bad locale setup where time.tzname[0] == time.tzname[1] and
Brett Cannon <bcannon@gmail.com>
parents: 30089
diff changeset
162 # Do not worry about possibility of time.tzname[0] == timetzname[1]
40aa4a10f49b Fix handling of bad locale setup where time.tzname[0] == time.tzname[1] and
Brett Cannon <bcannon@gmail.com>
parents: 30089
diff changeset
163 # and time.daylight; handle that in strptime .
29108
cb0025451482 Call time.tzset (if available) just before calculating possible timezones from time.tzname.
Brett Cannon <bcannon@gmail.com>
parents: 29073
diff changeset
164 try:
cb0025451482 Call time.tzset (if available) just before calculating possible timezones from time.tzname.
Brett Cannon <bcannon@gmail.com>
parents: 29073
diff changeset
165 time.tzset()
cb0025451482 Call time.tzset (if available) just before calculating possible timezones from time.tzname.
Brett Cannon <bcannon@gmail.com>
parents: 29073
diff changeset
166 except AttributeError:
cb0025451482 Call time.tzset (if available) just before calculating possible timezones from time.tzname.
Brett Cannon <bcannon@gmail.com>
parents: 29073
diff changeset
167 pass
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents: 30698
diff changeset
168 no_saving = frozenset(["utc", "gmt", time.tzname[0].lower()])
29073
c4e8c6721e45 Beefed up timezone support. UTC and GMT are now always recognized timezones
Brett Cannon <bcannon@gmail.com>
parents: 28871
diff changeset
169 if time.daylight:
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents: 30698
diff changeset
170 has_saving = frozenset([time.tzname[1].lower()])
29073
c4e8c6721e45 Beefed up timezone support. UTC and GMT are now always recognized timezones
Brett Cannon <bcannon@gmail.com>
parents: 28871
diff changeset
171 else:
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents: 30698
diff changeset
172 has_saving = frozenset()
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
173 self.timezone = (no_saving, has_saving)
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 class TimeRE(dict):
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 """Handle conversion from format directives to regexes."""
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178
29993
730abfcf2610 Fixes bug of having default argument for TimeRE's __init__ that caused the
Brett Cannon <bcannon@gmail.com>
parents: 29991
diff changeset
179 def __init__(self, locale_time=None):
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
180 """Create keys/values.
30698
3e3fd56ce595 Let library modules use the new keyword arguments for list.sort().
Raymond Hettinger <python@rcn.com>
parents: 30228
diff changeset
181
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
182 Order of execution is important for dependency reasons.
30698
3e3fd56ce595 Let library modules use the new keyword arguments for list.sort().
Raymond Hettinger <python@rcn.com>
parents: 30228
diff changeset
183
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
184 """
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
185 if locale_time:
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
186 self.locale_time = locale_time
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
187 else:
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
188 self.locale_time = LocaleTime()
26887
45fc0e4b5084 Fix SF #658820, regex fixes for _strptime (Brett Cannon)
Neal Norwitz <nnorwitz@gmail.com>
parents: 26798
diff changeset
189 base = super(TimeRE, self)
45fc0e4b5084 Fix SF #658820, regex fixes for _strptime (Brett Cannon)
Neal Norwitz <nnorwitz@gmail.com>
parents: 26798
diff changeset
190 base.__init__({
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
191 # The " \d" part of the regex is to make %c from ANSI C work
26887
45fc0e4b5084 Fix SF #658820, regex fixes for _strptime (Brett Cannon)
Neal Norwitz <nnorwitz@gmail.com>
parents: 26798
diff changeset
192 'd': r"(?P<d>3[0-1]|[1-2]\d|0[1-9]|[1-9]| [1-9])",
45767
212e9c8ad087 add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents: 41938
diff changeset
193 'f': r"(?P<f>[0-9]{1,6})",
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 'H': r"(?P<H>2[0-3]|[0-1]\d|\d)",
26887
45fc0e4b5084 Fix SF #658820, regex fixes for _strptime (Brett Cannon)
Neal Norwitz <nnorwitz@gmail.com>
parents: 26798
diff changeset
195 'I': r"(?P<I>1[0-2]|0[1-9]|[1-9])",
45fc0e4b5084 Fix SF #658820, regex fixes for _strptime (Brett Cannon)
Neal Norwitz <nnorwitz@gmail.com>
parents: 26798
diff changeset
196 'j': r"(?P<j>36[0-6]|3[0-5]\d|[1-2]\d\d|0[1-9]\d|00[1-9]|[1-9]\d|0[1-9]|[1-9])",
45fc0e4b5084 Fix SF #658820, regex fixes for _strptime (Brett Cannon)
Neal Norwitz <nnorwitz@gmail.com>
parents: 26798
diff changeset
197 'm': r"(?P<m>1[0-2]|0[1-9]|[1-9])",
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 'M': r"(?P<M>[0-5]\d|\d)",
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 'S': r"(?P<S>6[0-1]|[0-5]\d|\d)",
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 'U': r"(?P<U>5[0-3]|[0-4]\d|\d)",
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 'w': r"(?P<w>[0-6])",
26887
45fc0e4b5084 Fix SF #658820, regex fixes for _strptime (Brett Cannon)
Neal Norwitz <nnorwitz@gmail.com>
parents: 26798
diff changeset
202 # W is set below by using 'U'
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 'y': r"(?P<y>\d\d)",
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
204 #XXX: Does 'Y' need to worry about having less or more than
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
205 # 4 digits?
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
206 'Y': r"(?P<Y>\d\d\d\d)",
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
207 'A': self.__seqToRE(self.locale_time.f_weekday, 'A'),
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
208 'a': self.__seqToRE(self.locale_time.a_weekday, 'a'),
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
209 'B': self.__seqToRE(self.locale_time.f_month[1:], 'B'),
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
210 'b': self.__seqToRE(self.locale_time.a_month[1:], 'b'),
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
211 'p': self.__seqToRE(self.locale_time.am_pm, 'p'),
33676
5ae3a1c80e3f Convert a listcomp to a gencomp (was already editing code).
Brett Cannon <bcannon@gmail.com>
parents: 33674
diff changeset
212 'Z': self.__seqToRE((tz for tz_names in self.locale_time.timezone
5ae3a1c80e3f Convert a listcomp to a gencomp (was already editing code).
Brett Cannon <bcannon@gmail.com>
parents: 33674
diff changeset
213 for tz in tz_names),
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
214 'Z'),
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
215 '%': '%'})
33784
f175c9838373 Add support for %U and %W to contribute to calculating the date when the year
Brett Cannon <bcannon@gmail.com>
parents: 33678
diff changeset
216 base.__setitem__('W', base.__getitem__('U').replace('U', 'W'))
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
217 base.__setitem__('c', self.pattern(self.locale_time.LC_date_time))
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
218 base.__setitem__('x', self.pattern(self.locale_time.LC_date))
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
219 base.__setitem__('X', self.pattern(self.locale_time.LC_time))
24674
3269bdd65988 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24322
diff changeset
220
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 def __seqToRE(self, to_convert, directive):
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
222 """Convert a list to a regex string for matching a directive.
30698
3e3fd56ce595 Let library modules use the new keyword arguments for list.sort().
Raymond Hettinger <python@rcn.com>
parents: 30228
diff changeset
223
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
224 Want possible matching values to be from longest to shortest. This
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
225 prevents the possibility of a match occuring for a value that also
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
226 a substring of a larger value that should have matched (e.g., 'abc'
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
227 matching when 'abcdef' should have been the match).
30698
3e3fd56ce595 Let library modules use the new keyword arguments for list.sort().
Raymond Hettinger <python@rcn.com>
parents: 30228
diff changeset
228
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
229 """
33678
b31a51206774 Fix bug introduced by the previous patch by changing TimeRE.__seqToRe() to
Brett Cannon <bcannon@gmail.com>
parents: 33676
diff changeset
230 to_convert = sorted(to_convert, key=len, reverse=True)
27258
e8c1a9542f66 Checking in Brett Cannon's patch #662053, which fixes bug #661354.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26887
diff changeset
231 for value in to_convert:
e8c1a9542f66 Checking in Brett Cannon's patch #662053, which fixes bug #661354.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26887
diff changeset
232 if value != '':
e8c1a9542f66 Checking in Brett Cannon's patch #662053, which fixes bug #661354.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26887
diff changeset
233 break
e8c1a9542f66 Checking in Brett Cannon's patch #662053, which fixes bug #661354.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26887
diff changeset
234 else:
e8c1a9542f66 Checking in Brett Cannon's patch #662053, which fixes bug #661354.
Jack Jansen <jack.jansen@cwi.nl>
parents: 26887
diff changeset
235 return ''
33674
d99de65f5425 Locale data that contains regex metacharacters are now properly escaped.
Brett Cannon <bcannon@gmail.com>
parents: 31967
diff changeset
236 regex = '|'.join(re_escape(stuff) for stuff in to_convert)
25372
caa3a2fc6a20 Brett's fixes for various bugs and coding issues. Closes SF patch #
Barry Warsaw <barry@python.org>
parents: 25071
diff changeset
237 regex = '(?P<%s>%s' % (directive, regex)
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 return '%s)' % regex
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
239
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 def pattern(self, format):
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
241 """Return regex pattern for the format string.
28774
c9bee11eaa8b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28686
diff changeset
242
28686
70d6f8b0fc69 Make _strptime escape regex syntax in format string to prevent use in internal regex.
Brett Cannon <bcannon@gmail.com>
parents: 28243
diff changeset
243 Need to make sure that any characters that might be interpreted as
30117
40aa4a10f49b Fix handling of bad locale setup where time.tzname[0] == time.tzname[1] and
Brett Cannon <bcannon@gmail.com>
parents: 30089
diff changeset
244 regex syntax are escaped.
28774
c9bee11eaa8b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28686
diff changeset
245
28686
70d6f8b0fc69 Make _strptime escape regex syntax in format string to prevent use in internal regex.
Brett Cannon <bcannon@gmail.com>
parents: 28243
diff changeset
246 """
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 processed_format = ''
28686
70d6f8b0fc69 Make _strptime escape regex syntax in format string to prevent use in internal regex.
Brett Cannon <bcannon@gmail.com>
parents: 28243
diff changeset
248 # The sub() call escapes all characters that might be misconstrued
33674
d99de65f5425 Locale data that contains regex metacharacters are now properly escaped.
Brett Cannon <bcannon@gmail.com>
parents: 31967
diff changeset
249 # as regex syntax. Cannot use re.escape since we have to deal with
d99de65f5425 Locale data that contains regex metacharacters are now properly escaped.
Brett Cannon <bcannon@gmail.com>
parents: 31967
diff changeset
250 # format directives (%m, etc.).
30228
a0f53a069470 Make sure parentheses are escaped when used in the format string.
Brett Cannon <bcannon@gmail.com>
parents: 30117
diff changeset
251 regex_chars = re_compile(r"([\\.^$*+?\(\){}\[\]|])")
28686
70d6f8b0fc69 Make _strptime escape regex syntax in format string to prevent use in internal regex.
Brett Cannon <bcannon@gmail.com>
parents: 28243
diff changeset
252 format = regex_chars.sub(r"\\\1", format)
27293
bfc140c7e3d0 SF patch 670194: Performance enhancement for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27288
diff changeset
253 whitespace_replacement = re_compile('\s+')
41938
e19972834170 Make _strptime.TimeRE().pattern() use ``\s+`` for matching whitespace instead
Brett Cannon <bcannon@gmail.com>
parents: 41349
diff changeset
254 format = whitespace_replacement.sub('\s+', format)
31967
3d046d063a84 Replace str.find()!=1 with the more readable "in" operator.
Raymond Hettinger <python@rcn.com>
parents: 31619
diff changeset
255 while '%' in format:
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 directive_index = format.index('%')+1
24674
3269bdd65988 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24322
diff changeset
257 processed_format = "%s%s%s" % (processed_format,
25071
05b15ce28e31 Many hopefully benign style clean ups. Still passes the test suite of
Barry Warsaw <barry@python.org>
parents: 25070
diff changeset
258 format[:directive_index-1],
05b15ce28e31 Many hopefully benign style clean ups. Still passes the test suite of
Barry Warsaw <barry@python.org>
parents: 25070
diff changeset
259 self[format[directive_index]])
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 format = format[directive_index+1:]
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 return "%s%s" % (processed_format, format)
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
262
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 def compile(self, format):
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 """Return a compiled re object for the format string."""
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 return re_compile(self.pattern(format), IGNORECASE)
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
267 _cache_lock = _thread_allocate_lock()
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
268 # DO NOT modify _TimeRE_cache or _regex_cache without acquiring the cache lock
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
269 # first!
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
270 _TimeRE_cache = TimeRE()
30117
40aa4a10f49b Fix handling of bad locale setup where time.tzname[0] == time.tzname[1] and
Brett Cannon <bcannon@gmail.com>
parents: 30089
diff changeset
271 _CACHE_MAX_SIZE = 5 # Max number of regexes stored in _regex_cache
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
272 _regex_cache = {}
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents:
diff changeset
273
40714
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
274 def _calc_julian_from_U_or_W(year, week_of_year, day_of_week, week_starts_Mon):
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
275 """Calculate the Julian day based on the year, week of the year, and day of
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
276 the week, with week_start_day representing whether the week of the year
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
277 assumes the week starts on Sunday or Monday (6 or 0)."""
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
278 first_weekday = datetime_date(year, 1, 1).weekday()
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
279 # If we are dealing with the %U directive (week starts on Sunday), it's
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
280 # easier to just shift the view to Sunday being the first day of the
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
281 # week.
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
282 if not week_starts_Mon:
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
283 first_weekday = (first_weekday + 1) % 7
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
284 day_of_week = (day_of_week + 1) % 7
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
285 # Need to watch out for a week 0 (when the first day of the year is not
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
286 # the same as that specified by %U or %W).
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
287 week_0_length = (7 - first_weekday) % 7
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
288 if week_of_year == 0:
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
289 return 1 + day_of_week - first_weekday
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
290 else:
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
291 days_to_week = week_0_length + (7 * (week_of_year - 1))
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
292 return 1 + days_to_week + day_of_week
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
293
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
294
45767
212e9c8ad087 add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents: 41938
diff changeset
295 def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
30117
40aa4a10f49b Fix handling of bad locale setup where time.tzname[0] == time.tzname[1] and
Brett Cannon <bcannon@gmail.com>
parents: 30089
diff changeset
296 """Return a time struct based on the input string and the format string."""
35562
ac88044cde48 Clear out the regex cache when the TimeRE cache is invalidated by a locale
Brett Cannon <bcannon@gmail.com>
parents: 35477
diff changeset
297 global _TimeRE_cache, _regex_cache
40724
1782b622aac7 Use the thread lock's context manager instead of a try/finally statement.
Brett Cannon <bcannon@gmail.com>
parents: 40716
diff changeset
298 with _cache_lock:
41349
b5b750cf85c2 time.strptime's caching of its locale object was being recreated when the
Brett Cannon <bcannon@gmail.com>
parents: 40724
diff changeset
299 if _getlang() != _TimeRE_cache.locale_time.lang:
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
300 _TimeRE_cache = TimeRE()
41349
b5b750cf85c2 time.strptime's caching of its locale object was being recreated when the
Brett Cannon <bcannon@gmail.com>
parents: 40724
diff changeset
301 _regex_cache.clear()
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
302 if len(_regex_cache) > _CACHE_MAX_SIZE:
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
303 _regex_cache.clear()
41349
b5b750cf85c2 time.strptime's caching of its locale object was being recreated when the
Brett Cannon <bcannon@gmail.com>
parents: 40724
diff changeset
304 locale_time = _TimeRE_cache.locale_time
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
305 format_regex = _regex_cache.get(format)
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
306 if not format_regex:
35830
029b6c5f258e Change time.strptime() to raise ValueError whenever there is an error in the
Brett Cannon <bcannon@gmail.com>
parents: 35562
diff changeset
307 try:
41349
b5b750cf85c2 time.strptime's caching of its locale object was being recreated when the
Brett Cannon <bcannon@gmail.com>
parents: 40724
diff changeset
308 format_regex = _TimeRE_cache.compile(format)
35830
029b6c5f258e Change time.strptime() to raise ValueError whenever there is an error in the
Brett Cannon <bcannon@gmail.com>
parents: 35562
diff changeset
309 # KeyError raised when a bad format is found; can be specified as
029b6c5f258e Change time.strptime() to raise ValueError whenever there is an error in the
Brett Cannon <bcannon@gmail.com>
parents: 35562
diff changeset
310 # \\, in which case it was a stray % but with a space after it
029b6c5f258e Change time.strptime() to raise ValueError whenever there is an error in the
Brett Cannon <bcannon@gmail.com>
parents: 35562
diff changeset
311 except KeyError, err:
029b6c5f258e Change time.strptime() to raise ValueError whenever there is an error in the
Brett Cannon <bcannon@gmail.com>
parents: 35562
diff changeset
312 bad_directive = err.args[0]
029b6c5f258e Change time.strptime() to raise ValueError whenever there is an error in the
Brett Cannon <bcannon@gmail.com>
parents: 35562
diff changeset
313 if bad_directive == "\\":
029b6c5f258e Change time.strptime() to raise ValueError whenever there is an error in the
Brett Cannon <bcannon@gmail.com>
parents: 35562
diff changeset
314 bad_directive = "%"
029b6c5f258e Change time.strptime() to raise ValueError whenever there is an error in the
Brett Cannon <bcannon@gmail.com>
parents: 35562
diff changeset
315 del err
029b6c5f258e Change time.strptime() to raise ValueError whenever there is an error in the
Brett Cannon <bcannon@gmail.com>
parents: 35562
diff changeset
316 raise ValueError("'%s' is a bad directive in format '%s'" %
029b6c5f258e Change time.strptime() to raise ValueError whenever there is an error in the
Brett Cannon <bcannon@gmail.com>
parents: 35562
diff changeset
317 (bad_directive, format))
029b6c5f258e Change time.strptime() to raise ValueError whenever there is an error in the
Brett Cannon <bcannon@gmail.com>
parents: 35562
diff changeset
318 # IndexError only occurs when the format string is "%"
029b6c5f258e Change time.strptime() to raise ValueError whenever there is an error in the
Brett Cannon <bcannon@gmail.com>
parents: 35562
diff changeset
319 except IndexError:
029b6c5f258e Change time.strptime() to raise ValueError whenever there is an error in the
Brett Cannon <bcannon@gmail.com>
parents: 35562
diff changeset
320 raise ValueError("stray %% in format '%s'" % format)
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
321 _regex_cache[format] = format_regex
27293
bfc140c7e3d0 SF patch 670194: Performance enhancement for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27288
diff changeset
322 found = format_regex.match(data_string)
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
323 if not found:
39889
b072228c95a2 Make the error message for when the time data and format do not match clearer.
Brett Cannon <bcannon@gmail.com>
parents: 35830
diff changeset
324 raise ValueError("time data %r does not match format %r" %
29831
92f1eeaeb41d SF 748201: time.strptime() should display format and date on error
Raymond Hettinger <python@rcn.com>
parents: 29753
diff changeset
325 (data_string, format))
28871
f55b553a0b34 Raise a ValueError when there is data that was not covered in the format string. Done to match behavior of pre-existing C-based strptime implementations.
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
326 if len(data_string) != found.end():
f55b553a0b34 Raise a ValueError when there is data that was not covered in the format string. Done to match behavior of pre-existing C-based strptime implementations.
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
327 raise ValueError("unconverted data remains: %s" %
f55b553a0b34 Raise a ValueError when there is data that was not covered in the format string. Done to match behavior of pre-existing C-based strptime implementations.
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
328 data_string[found.end():])
76866
69d407b016c1 Issue #14157: Fix time.strptime failing without a year on February 29th.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45767
diff changeset
329
69d407b016c1 Issue #14157: Fix time.strptime failing without a year on February 29th.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45767
diff changeset
330 year = None
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
331 month = day = 1
45767
212e9c8ad087 add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents: 41938
diff changeset
332 hour = minute = second = fraction = 0
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
333 tz = -1
33785
817c241bffff Fix comments.
Brett Cannon <bcannon@gmail.com>
parents: 33784
diff changeset
334 # Default to -1 to signify that values not known; not critical to have,
817c241bffff Fix comments.
Brett Cannon <bcannon@gmail.com>
parents: 33784
diff changeset
335 # though
33784
f175c9838373 Add support for %U and %W to contribute to calculating the date when the year
Brett Cannon <bcannon@gmail.com>
parents: 33678
diff changeset
336 week_of_year = -1
f175c9838373 Add support for %U and %W to contribute to calculating the date when the year
Brett Cannon <bcannon@gmail.com>
parents: 33678
diff changeset
337 week_of_year_start = -1
33785
817c241bffff Fix comments.
Brett Cannon <bcannon@gmail.com>
parents: 33784
diff changeset
338 # weekday and julian defaulted to -1 so as to signal need to calculate
817c241bffff Fix comments.
Brett Cannon <bcannon@gmail.com>
parents: 33784
diff changeset
339 # values
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
340 weekday = julian = -1
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
341 found_dict = found.groupdict()
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
342 for group_key in found_dict.iterkeys():
33784
f175c9838373 Add support for %U and %W to contribute to calculating the date when the year
Brett Cannon <bcannon@gmail.com>
parents: 33678
diff changeset
343 # Directives not explicitly handled below:
f175c9838373 Add support for %U and %W to contribute to calculating the date when the year
Brett Cannon <bcannon@gmail.com>
parents: 33678
diff changeset
344 # c, x, X
f175c9838373 Add support for %U and %W to contribute to calculating the date when the year
Brett Cannon <bcannon@gmail.com>
parents: 33678
diff changeset
345 # handled by making out of other directives
f175c9838373 Add support for %U and %W to contribute to calculating the date when the year
Brett Cannon <bcannon@gmail.com>
parents: 33678
diff changeset
346 # U, W
f175c9838373 Add support for %U and %W to contribute to calculating the date when the year
Brett Cannon <bcannon@gmail.com>
parents: 33678
diff changeset
347 # worthless without day of the week
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
348 if group_key == 'y':
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
349 year = int(found_dict['y'])
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
350 # Open Group specification for strptime() states that a %y
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
351 #value in the range of [00, 68] is in the century 2000, while
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
352 #[69,99] is in the century 1900
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
353 if year <= 68:
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
354 year += 2000
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
355 else:
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
356 year += 1900
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
357 elif group_key == 'Y':
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
358 year = int(found_dict['Y'])
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
359 elif group_key == 'm':
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
360 month = int(found_dict['m'])
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
361 elif group_key == 'B':
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
362 month = locale_time.f_month.index(found_dict['B'].lower())
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
363 elif group_key == 'b':
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
364 month = locale_time.a_month.index(found_dict['b'].lower())
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
365 elif group_key == 'd':
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
366 day = int(found_dict['d'])
29656
582887ba7a68 use == like all the other conditionals
Neal Norwitz <nnorwitz@gmail.com>
parents: 29108
diff changeset
367 elif group_key == 'H':
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
368 hour = int(found_dict['H'])
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
369 elif group_key == 'I':
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
370 hour = int(found_dict['I'])
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
371 ampm = found_dict.get('p', '').lower()
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
372 # If there was no AM/PM indicator, we'll treat this like AM
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
373 if ampm in ('', locale_time.am_pm[0]):
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
374 # We're in AM so the hour is correct unless we're
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
375 # looking at 12 midnight.
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
376 # 12 midnight == 12 AM == hour 0
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
377 if hour == 12:
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
378 hour = 0
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
379 elif ampm == locale_time.am_pm[1]:
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
380 # We're in PM so we need to add 12 to the hour unless
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
381 # we're looking at 12 noon.
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
382 # 12 noon == 12 PM == hour 12
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
383 if hour != 12:
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
384 hour += 12
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
385 elif group_key == 'M':
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
386 minute = int(found_dict['M'])
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
387 elif group_key == 'S':
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
388 second = int(found_dict['S'])
45767
212e9c8ad087 add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents: 41938
diff changeset
389 elif group_key == 'f':
212e9c8ad087 add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents: 41938
diff changeset
390 s = found_dict['f']
212e9c8ad087 add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents: 41938
diff changeset
391 # Pad to always return microseconds.
212e9c8ad087 add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents: 41938
diff changeset
392 s += "0" * (6 - len(s))
212e9c8ad087 add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents: 41938
diff changeset
393 fraction = int(s)
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
394 elif group_key == 'A':
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
395 weekday = locale_time.f_weekday.index(found_dict['A'].lower())
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
396 elif group_key == 'a':
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
397 weekday = locale_time.a_weekday.index(found_dict['a'].lower())
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
398 elif group_key == 'w':
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
399 weekday = int(found_dict['w'])
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
400 if weekday == 0:
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
401 weekday = 6
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
402 else:
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
403 weekday -= 1
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
404 elif group_key == 'j':
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
405 julian = int(found_dict['j'])
33784
f175c9838373 Add support for %U and %W to contribute to calculating the date when the year
Brett Cannon <bcannon@gmail.com>
parents: 33678
diff changeset
406 elif group_key in ('U', 'W'):
f175c9838373 Add support for %U and %W to contribute to calculating the date when the year
Brett Cannon <bcannon@gmail.com>
parents: 33678
diff changeset
407 week_of_year = int(found_dict[group_key])
f175c9838373 Add support for %U and %W to contribute to calculating the date when the year
Brett Cannon <bcannon@gmail.com>
parents: 33678
diff changeset
408 if group_key == 'U':
40714
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
409 # U starts week on Sunday.
33784
f175c9838373 Add support for %U and %W to contribute to calculating the date when the year
Brett Cannon <bcannon@gmail.com>
parents: 33678
diff changeset
410 week_of_year_start = 6
f175c9838373 Add support for %U and %W to contribute to calculating the date when the year
Brett Cannon <bcannon@gmail.com>
parents: 33678
diff changeset
411 else:
40714
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
412 # W starts week on Monday.
33784
f175c9838373 Add support for %U and %W to contribute to calculating the date when the year
Brett Cannon <bcannon@gmail.com>
parents: 33678
diff changeset
413 week_of_year_start = 0
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
414 elif group_key == 'Z':
29073
c4e8c6721e45 Beefed up timezone support. UTC and GMT are now always recognized timezones
Brett Cannon <bcannon@gmail.com>
parents: 28871
diff changeset
415 # Since -1 is default value only need to worry about setting tz if
c4e8c6721e45 Beefed up timezone support. UTC and GMT are now always recognized timezones
Brett Cannon <bcannon@gmail.com>
parents: 28871
diff changeset
416 # it can be something other than -1.
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
417 found_zone = found_dict['Z'].lower()
30117
40aa4a10f49b Fix handling of bad locale setup where time.tzname[0] == time.tzname[1] and
Brett Cannon <bcannon@gmail.com>
parents: 30089
diff changeset
418 for value, tz_values in enumerate(locale_time.timezone):
40aa4a10f49b Fix handling of bad locale setup where time.tzname[0] == time.tzname[1] and
Brett Cannon <bcannon@gmail.com>
parents: 30089
diff changeset
419 if found_zone in tz_values:
40aa4a10f49b Fix handling of bad locale setup where time.tzname[0] == time.tzname[1] and
Brett Cannon <bcannon@gmail.com>
parents: 30089
diff changeset
420 # Deal with bad locale setup where timezone names are the
40aa4a10f49b Fix handling of bad locale setup where time.tzname[0] == time.tzname[1] and
Brett Cannon <bcannon@gmail.com>
parents: 30089
diff changeset
421 # same and yet time.daylight is true; too ambiguous to
40aa4a10f49b Fix handling of bad locale setup where time.tzname[0] == time.tzname[1] and
Brett Cannon <bcannon@gmail.com>
parents: 30089
diff changeset
422 # be able to tell what timezone has daylight savings
31619
8f60217a7547 Deal with possible case of having time.tzname[1] containing UTC or GMT.
Brett Cannon <bcannon@gmail.com>
parents: 31389
diff changeset
423 if (time.tzname[0] == time.tzname[1] and
8f60217a7547 Deal with possible case of having time.tzname[1] containing UTC or GMT.
Brett Cannon <bcannon@gmail.com>
parents: 31389
diff changeset
424 time.daylight and found_zone not in ("utc", "gmt")):
31389
aca1ce1b4b61 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 30929
diff changeset
425 break
30117
40aa4a10f49b Fix handling of bad locale setup where time.tzname[0] == time.tzname[1] and
Brett Cannon <bcannon@gmail.com>
parents: 30089
diff changeset
426 else:
30050
494b07a5e2e5 Re-introduce caching of TimeRE and compiled regexes with added thread-safety.
Brett Cannon <bcannon@gmail.com>
parents: 29993
diff changeset
427 tz = value
30117
40aa4a10f49b Fix handling of bad locale setup where time.tzname[0] == time.tzname[1] and
Brett Cannon <bcannon@gmail.com>
parents: 30089
diff changeset
428 break
76934
cbc9dc1c977e Followup to issue #14157: respect the relative ordering of values produced by time.strptime().
Antoine Pitrou <solipsis@pitrou.net>
parents: 76866
diff changeset
429 leap_year_fix = False
76866
69d407b016c1 Issue #14157: Fix time.strptime failing without a year on February 29th.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45767
diff changeset
430 if year is None and month == 2 and day == 29:
69d407b016c1 Issue #14157: Fix time.strptime failing without a year on February 29th.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45767
diff changeset
431 year = 1904 # 1904 is first leap year of 20th century
76934
cbc9dc1c977e Followup to issue #14157: respect the relative ordering of values produced by time.strptime().
Antoine Pitrou <solipsis@pitrou.net>
parents: 76866
diff changeset
432 leap_year_fix = True
76866
69d407b016c1 Issue #14157: Fix time.strptime failing without a year on February 29th.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45767
diff changeset
433 elif year is None:
69d407b016c1 Issue #14157: Fix time.strptime failing without a year on February 29th.
Antoine Pitrou <solipsis@pitrou.net>
parents: 45767
diff changeset
434 year = 1900
33784
f175c9838373 Add support for %U and %W to contribute to calculating the date when the year
Brett Cannon <bcannon@gmail.com>
parents: 33678
diff changeset
435 # If we know the week of the year and what day of that week, we can figure
40714
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
436 # out the Julian day of the year.
33853
d41ac11e9c60 Fix bug of implementation of algorithm for calculating the date from year, week
Brett Cannon <bcannon@gmail.com>
parents: 33785
diff changeset
437 if julian == -1 and week_of_year != -1 and weekday != -1:
40714
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
438 week_starts_Mon = True if week_of_year_start == 0 else False
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
439 julian = _calc_julian_from_U_or_W(year, week_of_year, weekday,
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
440 week_starts_Mon)
28243
f13adc00c165 SF patch #691928: Use datetime in _strptime
Raymond Hettinger <python@rcn.com>
parents: 27507
diff changeset
441 # Cannot pre-calculate datetime_date() since can change in Julian
40714
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
442 # calculation and thus could have different value for the day of the week
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
443 # calculation.
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
444 if julian == -1:
28243
f13adc00c165 SF patch #691928: Use datetime in _strptime
Raymond Hettinger <python@rcn.com>
parents: 27507
diff changeset
445 # Need to add 1 to result since first day of the year is 1, not 0.
f13adc00c165 SF patch #691928: Use datetime in _strptime
Raymond Hettinger <python@rcn.com>
parents: 27507
diff changeset
446 julian = datetime_date(year, month, day).toordinal() - \
f13adc00c165 SF patch #691928: Use datetime in _strptime
Raymond Hettinger <python@rcn.com>
parents: 27507
diff changeset
447 datetime_date(year, 1, 1).toordinal() + 1
f13adc00c165 SF patch #691928: Use datetime in _strptime
Raymond Hettinger <python@rcn.com>
parents: 27507
diff changeset
448 else: # Assume that if they bothered to include Julian day it will
40714
15deca39e0e6 Fix time.strptime's %U support. Basically rewrote the algorithm to be more
Brett Cannon <bcannon@gmail.com>
parents: 39889
diff changeset
449 # be accurate.
28243
f13adc00c165 SF patch #691928: Use datetime in _strptime
Raymond Hettinger <python@rcn.com>
parents: 27507
diff changeset
450 datetime_result = datetime_date.fromordinal((julian - 1) + datetime_date(year, 1, 1).toordinal())
f13adc00c165 SF patch #691928: Use datetime in _strptime
Raymond Hettinger <python@rcn.com>
parents: 27507
diff changeset
451 year = datetime_result.year
f13adc00c165 SF patch #691928: Use datetime in _strptime
Raymond Hettinger <python@rcn.com>
parents: 27507
diff changeset
452 month = datetime_result.month
f13adc00c165 SF patch #691928: Use datetime in _strptime
Raymond Hettinger <python@rcn.com>
parents: 27507
diff changeset
453 day = datetime_result.day
27288
97f494bf7319 SF patch 670012: Compatibility changes for _strptime.py.
Tim Peters <tim.peters@gmail.com>
parents: 27258
diff changeset
454 if weekday == -1:
28243
f13adc00c165 SF patch #691928: Use datetime in _strptime
Raymond Hettinger <python@rcn.com>
parents: 27507
diff changeset
455 weekday = datetime_date(year, month, day).weekday()
76934
cbc9dc1c977e Followup to issue #14157: respect the relative ordering of values produced by time.strptime().
Antoine Pitrou <solipsis@pitrou.net>
parents: 76866
diff changeset
456 if leap_year_fix:
cbc9dc1c977e Followup to issue #14157: respect the relative ordering of values produced by time.strptime().
Antoine Pitrou <solipsis@pitrou.net>
parents: 76866
diff changeset
457 # the caller didn't supply a year but asked for Feb 29th. We couldn't
cbc9dc1c977e Followup to issue #14157: respect the relative ordering of values produced by time.strptime().
Antoine Pitrou <solipsis@pitrou.net>
parents: 76866
diff changeset
458 # use the default of 1900 for computations. We set it back to ensure
cbc9dc1c977e Followup to issue #14157: respect the relative ordering of values produced by time.strptime().
Antoine Pitrou <solipsis@pitrou.net>
parents: 76866
diff changeset
459 # that February 29th is smaller than March 1st.
cbc9dc1c977e Followup to issue #14157: respect the relative ordering of values produced by time.strptime().
Antoine Pitrou <solipsis@pitrou.net>
parents: 76866
diff changeset
460 year = 1900
cbc9dc1c977e Followup to issue #14157: respect the relative ordering of values produced by time.strptime().
Antoine Pitrou <solipsis@pitrou.net>
parents: 76866
diff changeset
461
45767
212e9c8ad087 add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents: 41938
diff changeset
462 return (time.struct_time((year, month, day,
212e9c8ad087 add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents: 41938
diff changeset
463 hour, minute, second,
212e9c8ad087 add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents: 41938
diff changeset
464 weekday, julian, tz)), fraction)
212e9c8ad087 add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents: 41938
diff changeset
465
212e9c8ad087 add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents: 41938
diff changeset
466 def _strptime_time(data_string, format="%a %b %d %H:%M:%S %Y"):
212e9c8ad087 add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents: 41938
diff changeset
467 return _strptime(data_string, format)[0]