@@ -70,9 +70,8 @@ class DateParseError(ValueError):
70
70
_DEFAULT_DATETIME = datetime(1 , 1 , 1 ).replace(hour = 0 , minute = 0 ,
71
71
second = 0 , microsecond = 0 )
72
72
73
- cdef object _TIMEPAT = re.compile(r ' ^ ( [01 ]? [0-9 ]| 2[0-3 ]) :( [0-5 ][0-9 ]) ' )
74
-
75
- cdef set _not_datelike_strings = {' a' , ' A' , ' m' , ' M' , ' p' , ' P' , ' t' , ' T' }
73
+ cdef:
74
+ set _not_datelike_strings = {' a' , ' A' , ' m' , ' M' , ' p' , ' P' , ' t' , ' T' }
76
75
77
76
# ----------------------------------------------------------------------
78
77
cdef:
@@ -170,6 +169,38 @@ cdef inline object _parse_delimited_date(object date_string, bint dayfirst):
170
169
raise DateParseError(" Invalid date specified ({}/{})" .format(month, day))
171
170
172
171
172
+ cdef inline bint does_string_look_like_time(object parse_string):
173
+ """
174
+ Checks whether given string is a time: it has to start either from
175
+ H:MM or from HH:MM, and hour and minute values must be valid.
176
+
177
+ Parameters
178
+ ----------
179
+ date_string : str
180
+
181
+ Returns:
182
+ --------
183
+ whether given string is a time
184
+ """
185
+ cdef:
186
+ const char * buf
187
+ Py_ssize_t length
188
+ int hour = - 1 , minute = - 1
189
+
190
+ buf = get_c_string_buf_and_size(parse_string, & length)
191
+ if length >= 4 :
192
+ if buf[1 ] == b' :' :
193
+ # h:MM format
194
+ hour = getdigit_ascii(buf[0 ], - 1 )
195
+ minute = _parse_2digit(buf + 2 )
196
+ elif buf[2 ] == b' :' :
197
+ # HH:MM format
198
+ hour = _parse_2digit(buf)
199
+ minute = _parse_2digit(buf + 3 )
200
+
201
+ return 0 <= hour <= 23 and 0 <= minute <= 59
202
+
203
+
173
204
def parse_datetime_string (date_string , freq = None , dayfirst = False ,
174
205
yearfirst = False , **kwargs ):
175
206
""" parse datetime string, only returns datetime.
@@ -186,7 +217,7 @@ def parse_datetime_string(date_string, freq=None, dayfirst=False,
186
217
if not _does_string_look_like_datetime(date_string):
187
218
raise ValueError (' Given date string not likely a datetime.' )
188
219
189
- if _TIMEPAT.match (date_string):
220
+ if does_string_look_like_time (date_string):
190
221
# use current datetime as default, not pass _DEFAULT_DATETIME
191
222
dt = du_parse(date_string, dayfirst = dayfirst,
192
223
yearfirst = yearfirst, ** kwargs)
0 commit comments