Debian Codegraph @

metomi/isodatetime/tests/conftest.py

raw symbols SCIP
1 # Copyright (C) British Crown (Met Office) & Contributors.
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU Lesser General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
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