Skip to content

Commit 4c4ba2c

Browse files
authored
Datetimes are unix timestamps in utc (#47)
* Datetimes are unix timestamps in utc * Update tests
1 parent cafc7be commit 4c4ba2c

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

pyrail/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Module defining data models for the pyrail application."""
22

33
from dataclasses import dataclass, field
4-
from datetime import datetime
4+
from datetime import datetime, timezone
55
from enum import Enum
66
from typing import Any
77

@@ -11,7 +11,7 @@
1111

1212
def _timestamp_to_datetime(timestamp: str) -> datetime:
1313
"""Convert an epoch timestamp to a datetime object."""
14-
return datetime.fromtimestamp(int(timestamp))
14+
return datetime.fromtimestamp(int(timestamp), timezone.utc)
1515

1616

1717
def _str_to_bool(strbool: str) -> bool:

tests/test_irail.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Unit tests for the iRail API wrapper."""
22

3-
from datetime import datetime, timedelta
3+
from datetime import datetime, timedelta, timezone
44
from unittest.mock import AsyncMock, patch
55

66
from aiohttp import ClientSession
@@ -331,21 +331,21 @@ async def test_error_handling():
331331

332332

333333
@pytest.mark.asyncio
334-
@pytest.mark.skip(reason="Timezone is different on different systems")
335334
async def test_timestamp_to_datetime():
336335
"""Test the timestamp_to_datetime function."""
337336
# Test valid timestamps
338-
assert _timestamp_to_datetime("1705593600") == datetime(2024, 1, 18, 17, 0) # 2024-01-18 16:00:00
339-
assert _timestamp_to_datetime("0") == datetime(1970, 1, 1, 1, 0) # Unix epoch
337+
assert _timestamp_to_datetime("1705593600") == datetime(
338+
2024, 1, 18, 16, 0, tzinfo=timezone.utc
339+
) # 2024-01-18 16:00:00
340+
assert _timestamp_to_datetime("0") == datetime(1970, 1, 1, 0, 0, tzinfo=timezone.utc) # Unix epoch
340341

341342

342343
@pytest.mark.asyncio
343-
@pytest.mark.skip(reason="Timezone is different on different systems")
344344
async def test_timestamp_field_deserialization():
345345
"""Test timestamp field deserialization in various models."""
346346
# Test ApiResponse timestamp
347347
api_response = ApiResponse.from_dict({"version": "1.0", "timestamp": "1705593600"})
348-
assert api_response.timestamp == datetime(2024, 1, 18, 17, 0)
348+
assert api_response.timestamp == datetime(2024, 1, 18, 16, 0, tzinfo=timezone.utc)
349349

350350
# Test LiveboardDeparture time
351351
departure = LiveboardDeparture.from_dict(
@@ -381,7 +381,7 @@ async def test_timestamp_field_deserialization():
381381
"departureConnection": "http://irail.be/connections/8821006/20250106/EC9272",
382382
}
383383
)
384-
assert departure.time == datetime(2024, 1, 18, 17, 0)
384+
assert departure.time == datetime(2024, 1, 18, 16, 0, tzinfo=timezone.utc)
385385

386386
# Test Alert start_time and end_time
387387
alert = Alert.from_dict(
@@ -394,8 +394,8 @@ async def test_timestamp_field_deserialization():
394394
"endTime": "1705597200",
395395
}
396396
)
397-
assert alert.start_time == datetime(2024, 1, 18, 17, 0)
398-
assert alert.end_time == datetime(2024, 1, 18, 18, 0)
397+
assert alert.start_time == datetime(2024, 1, 18, 16, 0, tzinfo=timezone.utc)
398+
assert alert.end_time == datetime(2024, 1, 18, 17, 0, tzinfo=timezone.utc)
399399

400400
# Test Disturbance timestamp
401401
disturbance = Disturbance.from_dict(
@@ -434,7 +434,7 @@ async def test_timestamp_field_deserialization():
434434
},
435435
}
436436
)
437-
assert disturbance.timestamp == datetime(2024, 1, 18, 17, 0)
437+
assert disturbance.timestamp == datetime(2024, 1, 18, 16, 0, tzinfo=timezone.utc)
438438

439439

440440
@pytest.mark.asyncio

0 commit comments

Comments
 (0)