Skip to content

Commit 849aa94

Browse files
Apply suggestions from code review
Co-authored-by: Yaroslav Halchenko <[email protected]>
1 parent 6a666fe commit 849aa94

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

heudiconv/utils.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -713,10 +713,7 @@ def strptime(datetime_string: str, fmts: list[str]) -> datetime:
713713
datetime_str = datetime_string.strip()
714714
for fmt in fmts:
715715
try:
716-
#return datetime.datetime.strptime(datetime_str, fmt)
717-
retval = datetime.datetime.strptime(datetime_str, fmt)
718-
print(retval)
719-
return retval
716+
return datetime.datetime.strptime(datetime_str, fmt)
720717
except ValueError:
721718
pass
722719
raise ValueError(f"Unable to parse datetime string: {datetime_str}")
@@ -741,9 +738,8 @@ def strptime_dcm_da_tm(dcm_data: dcm.Dataset, da_tag: TagType, tm_tag: TagType)
741738
742739
Parameters
743740
----------
744-
dcm_data : dcm.FileDataset
741+
dcm_data : dcm.Dataset
745742
DICOM with header, e.g., as read by pydicom.dcmread.
746-
Objects with __getitem__ and have those keys with values properly formatted may also work
747743
da_tag: str
748744
Dicom tag with DA value representation
749745
tm_tag: str
@@ -760,8 +756,8 @@ def strptime_dcm_da_tm(dcm_data: dcm.Dataset, da_tag: TagType, tm_tag: TagType)
760756

761757
datetime_obj = datetime.datetime.combine(date.date(), time.time())
762758

763-
if (0x0008, 0x0201) in dcm_data:
764-
utc_offset = dcm_data[0x0008, 0x0201].value
759+
if utc_offset_dcm := dcm_data.get([0x0008, 0x0201]):
760+
utc_offset = utc_offset_dcm.value
765761
datetime_obj = datetime_utc_offset(datetime_obj, utc_offset) if utc_offset else datetime_obj
766762
return datetime_obj
767763

@@ -783,9 +779,13 @@ def strptime_dcm_dt(dcm_data: dcm.Dataset, dt_tag: TagType) -> datetime:
783779
"%Y", "%Y%m", "%Y%m%d", "%Y%m%d%H", "%Y%m%d%H%M", "%Y%m%d%H%M%S", "%Y%m%d%H%M%S.%f"]
784780
datetime_obj = strptime(datetime_str, fmts)
785781

786-
if not datetime_obj.tzinfo and (0x0008, 0x0201) in dcm_data:
787-
utc_offset = dcm_data[0x0008, 0x0201].value
788-
datetime_obj = datetime_utc_offset(datetime_obj, utc_offset) if utc_offset else datetime_obj
782+
if utc_offset_dcm := dcm_data.get([0x0008, 0x0201]):
783+
if utc_offset := utc_offset_dcm.value:
784+
datetime_obj2 = datetime_utc_offset(datetime_obj, utc_offset)
785+
if datetime_obj.tzinfo and datetime_obj2 != datetime_obj:
786+
lgr.warning("Unexpectedly previously parsed datetime %s contains zoneinfo which is different from the one obtained from DICOMs UTFOffset field: %s", datetime_obj, datetime_obj2)
787+
else:
788+
datetime_obj = datetime_obj2
789789
return datetime_obj
790790

791791

0 commit comments

Comments
 (0)