Skip to content

Commit 1614261

Browse files
committed
adjust datetime parsing
1 parent 6454408 commit 1614261

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/pynxtools_xps/value_mappers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ def parse_datetime(
160160
all of which can be checked with this method.
161161
Optionally, a timezone (tzinfo) can be applied to the datetime object if provided.
162162
163-
164163
Parameters
165164
----------
166165
datetime_string : str
@@ -179,18 +178,20 @@ def parse_datetime(
179178
Returns
180179
-------
181180
str
182-
Datetime in ISO8601 format.
183-
181+
Datetime in ISO 8601 format.
184182
"""
185183
for date_fmt in possible_date_formats:
186184
try:
187185
datetime_obj = datetime.datetime.strptime(datetime_string, date_fmt)
188186

189187
if tzinfo is not None:
188+
# Apply the specified timezone to the datetime object
190189
datetime_obj = datetime_obj.replace(tzinfo=tzinfo)
191190

192-
return datetime_obj.astimezone().isoformat()
191+
# Convert to ISO 8601 format
192+
return datetime_obj.isoformat()
193193

194194
except ValueError:
195195
continue
196+
196197
raise ValueError("Date and time could not be converted to ISO 8601 format.")

tests/test_helpers.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,57 +93,57 @@ def test_extract_unit(
9393

9494

9595
@pytest.mark.parametrize(
96-
"datetime_string, possible_date_formats, expected_iso8601, tzinfo",
96+
"datetime_string, possible_date_formats, tzinfo, expected_iso8601",
9797
[
9898
# Test cases with valid datetime strings and formats
9999
(
100100
"2023-10-23 14:30:00",
101101
["%Y-%m-%d %H:%M:%S"],
102-
"2023-10-23T14:30:00+00:00",
103102
datetime.timezone.utc,
103+
"2023-10-23T14:30:00+00:00",
104104
),
105105
(
106106
"23/10/2023 14:30",
107107
["%d/%m/%Y %H:%M"],
108-
"2023-10-23T14:30:00+00:00",
109108
datetime.timezone.utc,
109+
"2023-10-23T14:30:00+00:00",
110110
),
111111
(
112112
"October 23, 2023 14:30",
113113
["%B %d, %Y %H:%M"],
114-
"2023-10-23T14:30:00+00:00",
115114
datetime.timezone.utc,
115+
"2023-10-23T14:30:00+00:00",
116116
),
117117
(
118118
"2023-10-23T14:30:00Z",
119119
["%Y-%m-%dT%H:%M:%SZ"],
120-
"2023-10-23T14:30:00+00:00",
121120
datetime.timezone.utc,
121+
"2023-10-23T14:30:00+00:00",
122122
),
123123
(
124124
"2023-10-23T14:30:00+0200",
125125
["%Y-%m-%dT%H:%M:%S%z"],
126-
"2023-10-23T14:30:00+02:00",
127126
datetime.timezone(datetime.timedelta(hours=2)),
127+
"2023-10-23T14:30:00+02:00",
128128
),
129129
# Test cases with timezone information
130130
(
131131
"2023-10-23 14:30:00",
132132
["%Y-%m-%d %H:%M:%S"],
133-
"2023-10-23T14:30:00+00:00",
134133
datetime.timezone(datetime.timedelta(hours=1)),
134+
"2023-10-23T14:30:00+01:00",
135135
),
136136
# Test case with missing timezone should still return UTC
137137
(
138138
"2023-10-23 14:30:00",
139139
["%Y-%m-%d %H:%M:%S"],
140-
"2023-10-23T14:30:00+00:00",
141140
None,
141+
"2023-10-23T14:30:00",
142142
),
143143
],
144144
)
145145
def test_parse_datetime(
146-
datetime_string, possible_date_formats, expected_iso8601, tzinfo
146+
datetime_string, possible_date_formats, tzinfo, expected_iso8601
147147
):
148148
result = parse_datetime(datetime_string, possible_date_formats, tzinfo)
149149
assert result == expected_iso8601, f"Expected {expected_iso8601} but got {result}"

0 commit comments

Comments
 (0)