| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import pytest |
| 17 |
import time |
| 18 |
from unittest.mock import Mock |
| 19 |
|
| 20 |
|
| 21 |
@pytest.fixture |
| 22 |
def mock_local_time_zone(monkeypatch: pytest.MonkeyPatch): |
| 23 |
"""A pytest fixture that simulates a given local system time zone. |
| 24 |
|
| 25 |
Args: |
| 26 |
seconds: The number of seconds UTC offset for the base time zone. |
| 27 |
dst_seconds: The number of seconds UTC offset for the daylight savings |
| 28 |
time zone. |
| 29 |
|
| 30 |
Note: these args are positive for time zones East of the prime meridian |
| 31 |
and negative for West. |
| 32 |
""" |
| 33 |
def _mock_local_time_zone(seconds: int, dst_seconds: int = 0) -> None: |
| 34 |
is_dst = 1 if dst_seconds else 0 |
| 35 |
mock_time = Mock(spec=time) |
| 36 |
mock_time.timezone = -seconds |
| 37 |
mock_time.altzone = -dst_seconds |
| 38 |
mock_time.daylight = is_dst |
| 39 |
mock_time.localtime.return_value = Mock(tm_isdst=is_dst) |
| 40 |
monkeypatch.setattr('metomi.isodatetime.timezone.time', mock_time) |
| 41 |
return _mock_local_time_zone |