Skip to content

Commit 2924dc9

Browse files
author
Val Brodsky
committed
Support for timezone conversion
1 parent 541d7c4 commit 2924dc9

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

labelbox/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def format_iso_datetime(dt: datetime.datetime) -> str:
8686
Formats a datetime object into the format: 2011-11-04T00:05:23Z
8787
Note that datetime.isoformat() outputs 2011-11-04T00:05:23+00:00
8888
"""
89-
return dt.strftime(ISO_DATETIME_FORMAT)
89+
return dt.astimezone(datetime.timezone.utc).strftime(ISO_DATETIME_FORMAT)
9090

9191

9292
def format_iso_from_string(date_string: str) -> datetime.datetime:

tests/unit/test_utils.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
from labelbox.utils import format_iso_datetime, format_iso_from_string
33

44

5-
@pytest.mark.parametrize(
6-
'datetime_str, expected_datetime_str',
7-
[
8-
('2011-11-04T00:05:23Z', '2011-11-04T00:05:23Z'),
9-
('2011-11-04T00:05:23+00:00', '2011-11-04T00:05:23Z'),
10-
('2011-11-04T00:05:23+05:00', '2011-11-04T00:05:23Z'
11-
), # NOTE: The current implementation is not converting from timezone other then UTC. This how it has been working prior to my change.
12-
('2011-11-04T00:05:23', '2011-11-04T00:05:23Z')
13-
])
5+
@pytest.mark.parametrize('datetime_str, expected_datetime_str',
6+
[('2011-11-04T00:05:23Z', '2011-11-04T00:05:23Z'),
7+
('2011-11-04T00:05:23+00:00', '2011-11-04T00:05:23Z'),
8+
('2011-11-04T00:05:23+05:00', '2011-11-03T19:05:23Z'),
9+
('2011-11-04T00:05:23', '2011-11-04T00:05:23Z')])
1410
def test_datetime_parsing(datetime_str, expected_datetime_str):
1511
# NOTE I would normally not take 'expected' using another function from sdk code, but in this case this is exactly the usage in _validate_parse_datetime
1612
assert format_iso_datetime(

0 commit comments

Comments
 (0)