-
-
Notifications
You must be signed in to change notification settings - Fork 426
Expand file tree
/
Copy pathtest_create_from_timestamp.py
More file actions
65 lines (44 loc) · 2.02 KB
/
test_create_from_timestamp.py
File metadata and controls
65 lines (44 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from __future__ import annotations
import datetime as datetime_
import pendulum
import pytest
from pendulum import timezone
from tests.conftest import assert_datetime
def test_create_from_timestamp_returns_pendulum():
d = pendulum.from_timestamp(pendulum.datetime(1975, 5, 21, 22, 32, 5).timestamp())
assert_datetime(d, 1975, 5, 21, 22, 32, 5)
assert d.timezone_name == "UTC"
def test_create_from_timestamp_with_timezone_string():
d = pendulum.from_timestamp(0, "America/Toronto")
assert d.timezone_name == "America/Toronto"
assert_datetime(d, 1969, 12, 31, 19, 0, 0)
def test_create_from_timestamp_with_timezone():
d = pendulum.from_timestamp(0, timezone("America/Toronto"))
assert d.timezone_name == "America/Toronto"
assert_datetime(d, 1969, 12, 31, 19, 0, 0)
def test_create_from_timestamp_negative():
d = pendulum.from_timestamp(-43201)
assert_datetime(d, 1969, 12, 31, 11, 59, 59)
assert d.timezone_name == "UTC"
def test_create_from_timestamp_negative_with_timezone():
d = pendulum.from_timestamp(-43201, "America/Toronto")
assert d.timezone_name == "America/Toronto"
assert_datetime(d, 1969, 12, 31, 6, 59, 59)
def test_create_from_timestamp_negative_with_microseconds():
d = pendulum.from_timestamp(-43201.5)
assert_datetime(d, 1969, 12, 31, 11, 59, 58, 500000)
assert d.timezone_name == "UTC"
@pytest.mark.parametrize("exception_type", [OSError, OverflowError])
def test_create_from_timestamp_falls_back_for_negative_timestamp(
monkeypatch, exception_type
):
class FakeDateTime(datetime_.datetime):
@classmethod
def fromtimestamp(cls, timestamp: float, tz: datetime_.tzinfo | None = None):
if timestamp == -43201:
raise exception_type("Invalid argument")
return super().fromtimestamp(timestamp, tz=tz)
monkeypatch.setattr(pendulum._datetime, "datetime", FakeDateTime)
d = pendulum.from_timestamp(-43201)
assert_datetime(d, 1969, 12, 31, 11, 59, 59)
assert d.timezone_name == "UTC"