Date time
date_time
#
Functions:
Name | Description |
---|---|
convert_dictionary_datetime_values |
Convert date/time/duration strings in a DICOM metadata dictionary. |
datetime_to_iso_string |
Convert datetime/date/time to an ISO 8601 string with second precision. |
parse_datetime |
Parse date/time or duration values from keyword/value pairs. |
convert_dictionary_datetime_values
#
convert_dictionary_datetime_values(
dicom_dict: dict[str, str],
) -> dict[str, datetime.date | datetime.time | float | str]
Convert date/time/duration strings in a DICOM metadata dictionary.
This function iterates over all key-value pairs in a dictionary and attempts
to convert strings that appear to represent date, time, or duration fields into
corresponding Python types (date
, time
, or float
). Keys that do not match
temporal patterns are returned unchanged with their original string values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
dict[str, str]
|
|
required |
Returns:
Type | Description |
---|---|
dict[str, datetime.date | datetime.time | float | str]
|
A new dictionary where values for date/time/duration fields are parsed into native Python types when possible. All keys are retained. |
Examples:
>>> dicom_dict = {
... "AcquisitionDate": "20240101",
... "EchoTime": "45.0",
... "InstanceNumber": "5",
... }
>>> convert_dictionary_datetime_values(dicom_dict)
{
'AcquisitionDate': datetime.date(2024, 1, 1),
'EchoTime': 45.0,
'InstanceNumber': '5'
}
Source code in src/imgtools/utils/date_time.py
datetime_to_iso_string
#
datetime_to_iso_string(
datetime_obj: (
datetime.datetime | datetime.date | datetime.time
),
) -> str
Convert datetime/date/time to an ISO 8601 string with second precision.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
datetime.datetime.datetime or datetime.datetime.date or datetime.datetime.time
|
The datetime, date, or time object to convert. |
required |
Returns:
Type | Description |
---|---|
str
|
Formatted ISO 8601 string. |
Examples:
>>> datetime_to_iso_string(
... datetime.datetime(2024, 1, 1, 12, 0, 0)
... )
'2024-01-01T12:00:00'
>>> datetime_to_iso_string(datetime.date(2024, 1, 1))
'2024-01-01'
>>> datetime_to_iso_string(datetime.time(12, 0, 0))
'12:00:00'
Source code in src/imgtools/utils/date_time.py
parse_datetime
#
Parse date/time or duration values from keyword/value pairs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The name of the DICOM field (e.g., "AcquisitionTime", "EchoTime"). |
required |
|
str
|
The associated value to be parsed. |
required |
Returns:
Type | Description |
---|---|
datetime.date or datetime.time or float
|
Depending on the field type, returns either a Python date, time, or a float representing a duration. |
Examples:
>>> parse_datetime("EchoTime", "45.0")
45.0
>>> parse_datetime("AcquisitionDate", "20240101")
datetime.date(2024, 1, 1)
>>> parse_datetime("AcquisitionTime", "230000")
datetime.time(23, 0)
Source code in src/imgtools/utils/date_time.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|