MCPcopy Index your code
hub / github.com/RustPython/RustPython / _parse_isoformat_time

Function _parse_isoformat_time

Lib/_pydatetime.py:444–499  ·  view source on GitHub ↗
(tstr)

Source from the content-addressed store, hash-verified

442 return time_comps
443
444def _parse_isoformat_time(tstr):
445 # Format supported is HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]
446 len_str = len(tstr)
447 if len_str < 2:
448 raise ValueError("Isoformat time too short")
449
450 # This is equivalent to re.search('[+-Z]', tstr), but faster
451 tz_pos = (tstr.find('-') + 1 or tstr.find('+') + 1 or tstr.find('Z') + 1)
452 timestr = tstr[:tz_pos-1] if tz_pos > 0 else tstr
453
454 time_comps = _parse_hh_mm_ss_ff(timestr)
455
456 hour, minute, second, microsecond = time_comps
457 became_next_day = False
458 error_from_components = False
459 if (hour == 24):
460 if all(time_comp == 0 for time_comp in time_comps[1:]):
461 hour = 0
462 time_comps[0] = hour
463 became_next_day = True
464 else:
465 error_from_components = True
466
467 tzi = None
468 if tz_pos == len_str and tstr[-1] == 'Z':
469 tzi = timezone.utc
470 elif tz_pos > 0:
471 tzstr = tstr[tz_pos:]
472
473 # Valid time zone strings are:
474 # HH len: 2
475 # HHMM len: 4
476 # HH:MM len: 5
477 # HHMMSS len: 6
478 # HHMMSS.f+ len: 7+
479 # HH:MM:SS len: 8
480 # HH:MM:SS.f+ len: 10+
481
482 if len(tzstr) in (0, 1, 3) or tstr[tz_pos-1] == 'Z':
483 raise ValueError("Malformed time zone string")
484
485 tz_comps = _parse_hh_mm_ss_ff(tzstr)
486
487 if all(x == 0 for x in tz_comps):
488 tzi = timezone.utc
489 else:
490 tzsign = -1 if tstr[tz_pos - 1] == '-' else 1
491
492 td = timedelta(hours=tz_comps[0], minutes=tz_comps[1],
493 seconds=tz_comps[2], microseconds=tz_comps[3])
494
495 tzi = timezone(tzsign * td)
496
497 time_comps.append(tzi)
498
499 return time_comps, became_next_day, error_from_components
500
501# tuple[int, int, int] -> tuple[int, int, int] version of date.fromisocalendar

Callers 2

fromisoformatMethod · 0.85
fromisoformatMethod · 0.85

Calls 7

lenFunction · 0.85
_parse_hh_mm_ss_ffFunction · 0.85
allFunction · 0.85
timedeltaClass · 0.85
timezoneClass · 0.70
findMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected