Skip to content

Commit 7298776

Browse files
committed
refactor: break parse_iso into parse_iso_date and parse_iso_time
1 parent 6ff6a6f commit 7298776

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

typed_python/lib/datetime/date_parser.py

+30-20
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,28 @@ def parse_tz_offset(tokens: ListOf(str)) -> int:
184184
else:
185185
raise ValueError('Invalid tz offset: ')
186186

187+
@Entrypoint
188+
@staticmethod
189+
def parse_iso_time(time_tokens: ListOf(str)):
190+
hour, minute, second = 0, 0, 0.0
191+
192+
if len(time_tokens) == 1:
193+
if len(time_tokens[0]) == 6:
194+
hour, minute, second = int(time_tokens[0][:2]), int(time_tokens[0][2:4]), float(time_tokens[0][4:6])
195+
elif len(time_tokens[0]) == 4:
196+
hour, minute, second = int(time_tokens[0][:2]), int(time_tokens[0][2:4]), 0.0
197+
elif len(time_tokens[0]) == 2:
198+
hour, minute, second = int(time_tokens[0][:2]), 0, 0.0
199+
elif len(time_tokens) == 2:
200+
hour, minute, second = int(time_tokens[0]), int(time_tokens[1]), 0.0
201+
elif len(time_tokens) == 3:
202+
hour, minute, second = int(time_tokens[0]), int(time_tokens[1]), float(time_tokens[2])
203+
204+
if not is_time(hour, minute, second):
205+
raise ValueError('Invalid time: ', time_tokens)
206+
207+
return time_to_seconds(hour, minute, second)
208+
187209
@Entrypoint
188210
@staticmethod
189211
def parse_iso(date_str: str) -> float:
@@ -224,6 +246,11 @@ def parse_iso(date_str: str) -> float:
224246
if not is_date(year, month, day):
225247
raise ValueError('Invalid date_tokens: ', date_tokens)
226248

249+
dt = date_to_seconds(year, month, day)
250+
251+
if cursor >= len(tokens):
252+
return dt
253+
227254
# Process time segement
228255
time_tokens = ListOf(str)([])
229256
while cursor < len(tokens):
@@ -236,32 +263,15 @@ def parse_iso(date_str: str) -> float:
236263
time_tokens.append(tokens[cursor])
237264
cursor += 1
238265

239-
hour, minute, second = 0, 0, 0.0
240-
241-
if len(time_tokens) == 1:
242-
if len(time_tokens[0]) == 6:
243-
hour, minute, second = int(time_tokens[0][:2]), int(time_tokens[0][2:4]), float(time_tokens[0][4:6])
244-
elif len(time_tokens[0]) == 4:
245-
hour, minute, second = int(time_tokens[0][:2]), int(time_tokens[0][2:4]), 0.0
246-
elif len(time_tokens[0]) == 2:
247-
hour, minute, second = int(time_tokens[0][:2]), 0, 0.0
248-
elif len(time_tokens) == 2:
249-
hour, minute, second = int(time_tokens[0]), int(time_tokens[1]), 0.0
250-
elif len(time_tokens) == 3:
251-
hour, minute, second = int(time_tokens[0]), int(time_tokens[1]), float(time_tokens[2])
252-
253-
if not is_time(hour, minute, second):
254-
raise ValueError('Invalid time: ', time_tokens)
266+
dt += DateParser.parse_iso_time(time_tokens)
267+
if cursor >= len(tokens):
268+
return dt
255269

256270
tz_tokens = ListOf(str)([])
257271
while cursor < len(tokens):
258272
tz_tokens.append(tokens[cursor])
259273
cursor += 1
260274

261-
dt = date_to_seconds(year, month, day) + time_to_seconds(hour, minute, second)
262-
if len(tz_tokens) == 0:
263-
return dt
264-
265275
if len(tz_tokens) == 1:
266276
tz_offset = tz_abbr_to_utc_offset(tz_tokens[0], dt)
267277
elif tz_tokens[0] == PLUS or tz_tokens[0] == DASH:

0 commit comments

Comments
 (0)