@@ -44,8 +44,6 @@ _DEFAULT_DATETIME = datetime(1, 1, 1).replace(hour=0, minute=0,
44
44
second = 0 , microsecond = 0 )
45
45
46
46
cdef:
47
- object _TIMEPAT = re.compile(r ' ^ ( [01 ]? [0-9 ]| 2[0-3 ]) :( [0-5 ][0-9 ]) ' )
48
-
49
47
set _not_datelike_strings = {' a' , ' A' , ' m' , ' M' , ' p' , ' P' , ' t' , ' T' }
50
48
51
49
# ----------------------------------------------------------------------
@@ -144,6 +142,38 @@ cdef inline object _parse_delimited_date(object date_string, bint dayfirst):
144
142
raise DateParseError(" Invalid date specified ({}/{})" .format(month, day))
145
143
146
144
145
+ cdef inline bint does_string_look_like_time(object parse_string):
146
+ """
147
+ Checks whether given string is a time: it has to start either from
148
+ H:MM or from HH:MM, and hour and minute values must be valid.
149
+
150
+ Parameters
151
+ ----------
152
+ date_string : str
153
+
154
+ Returns:
155
+ --------
156
+ whether given string is a time
157
+ """
158
+ cdef:
159
+ const char * buf
160
+ Py_ssize_t length
161
+ int hour = - 1 , minute = - 1
162
+
163
+ buf = get_c_string_buf_and_size(parse_string, & length)
164
+ if length >= 4 :
165
+ if buf[1 ] == b' :' :
166
+ # h:MM format
167
+ hour = getdigit_ascii(buf[0 ], - 1 )
168
+ minute = _parse_2digit(buf + 2 )
169
+ elif buf[2 ] == b' :' :
170
+ # HH:MM format
171
+ hour = _parse_2digit(buf)
172
+ minute = _parse_2digit(buf + 3 )
173
+
174
+ return 0 <= hour <= 23 and 0 <= minute <= 59
175
+
176
+
147
177
def parse_datetime_string (date_string , freq = None , dayfirst = False ,
148
178
yearfirst = False , **kwargs ):
149
179
""" parse datetime string, only returns datetime.
@@ -160,7 +190,7 @@ def parse_datetime_string(date_string, freq=None, dayfirst=False,
160
190
if not _does_string_look_like_datetime(date_string):
161
191
raise ValueError (' Given date string not likely a datetime.' )
162
192
163
- if _TIMEPAT.match (date_string):
193
+ if does_string_look_like_time (date_string):
164
194
# use current datetime as default, not pass _DEFAULT_DATETIME
165
195
dt = du_parse(date_string, dayfirst = dayfirst,
166
196
yearfirst = yearfirst, ** kwargs)
0 commit comments