summaryrefslogtreecommitdiffstats
path: root/tests/datetime/test_strings.py
blob: 0de340dceb4741c3da863c3b8810c7bdd61799e4 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from __future__ import annotations

import pytest

import pendulum


def test_to_string():
    d = pendulum.datetime(1975, 12, 25, 0, 0, 0, 0, tz="local")
    assert str(d) == d.to_iso8601_string()
    d = pendulum.datetime(1975, 12, 25, 0, 0, 0, 123456, tz="local")
    assert str(d) == d.to_iso8601_string()


def test_to_date_string():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16)

    assert d.to_date_string() == "1975-12-25"


def test_to_formatted_date_string():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16)

    assert d.to_formatted_date_string() == "Dec 25, 1975"


def test_to_timestring():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16)

    assert d.to_time_string() == "14:15:16"


def test_to_atom_string():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
    assert d.to_atom_string() == "1975-12-25T14:15:16-05:00"


def test_to_cookie_string():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
    assert d.to_cookie_string() == "Thursday, 25-Dec-1975 14:15:16 EST"


def test_to_iso8601_string():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
    assert d.to_iso8601_string() == "1975-12-25T14:15:16-05:00"


def test_to_iso8601_string_utc():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16)
    assert d.to_iso8601_string() == "1975-12-25T14:15:16Z"


def test_to_iso8601_extended_string():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, 123456, tz="local")
    assert d.to_iso8601_string() == "1975-12-25T14:15:16.123456-05:00"


def test_to_rfc822_string():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
    assert d.to_rfc822_string() == "Thu, 25 Dec 75 14:15:16 -0500"


def test_to_rfc850_string():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
    assert d.to_rfc850_string() == "Thursday, 25-Dec-75 14:15:16 EST"


def test_to_rfc1036_string():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
    assert d.to_rfc1036_string() == "Thu, 25 Dec 75 14:15:16 -0500"


def test_to_rfc1123_string():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
    assert d.to_rfc1123_string() == "Thu, 25 Dec 1975 14:15:16 -0500"


def test_to_rfc2822_string():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
    assert d.to_rfc2822_string() == "Thu, 25 Dec 1975 14:15:16 -0500"


def test_to_rfc3339_string():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
    assert d.to_rfc3339_string() == "1975-12-25T14:15:16-05:00"


def test_to_rfc3339_extended_string():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, 123456, tz="local")
    assert d.to_rfc3339_string() == "1975-12-25T14:15:16.123456-05:00"


def test_to_rss_string():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
    assert d.to_rss_string() == "Thu, 25 Dec 1975 14:15:16 -0500"


def test_to_w3c_string():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
    assert d.to_w3c_string() == "1975-12-25T14:15:16-05:00"


def test_to_string_invalid():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")

    with pytest.raises(ValueError):
        d._to_string("invalid")


def test_repr():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
    expected = f"DateTime(1975, 12, 25, 14, 15, 16, tzinfo={repr(d.tzinfo)})"
    assert repr(d) == expected

    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, 123456, tz="local")
    expected = f"DateTime(1975, 12, 25, 14, 15, 16, 123456, tzinfo={repr(d.tzinfo)})"
    assert repr(d) == expected


def test_format_with_locale():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
    expected = "jeudi 25e jour de décembre 1975 02:15:16 PM -05:00"
    assert d.format("dddd Do [jour de] MMMM YYYY hh:mm:ss A Z", locale="fr") == expected


def test_strftime():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
    assert d.strftime("%d") == "25"


def test_for_json():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
    assert d.for_json() == "1975-12-25T14:15:16-05:00"


def test_format():
    d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="Europe/Paris")
    assert f"{d}" == "1975-12-25T14:15:16+01:00"
    assert f"{d:YYYY}" == "1975"
    assert f"{d:%Y}" == "1975"
    assert f"{d:%H:%M %d.%m.%Y}" == "14:15 25.12.1975"