test_datetime.py 7.87 KB
Newer Older
CharlieC's avatar
CharlieC committed
1
# Copyright (c) 2010-2021 openpyxl
2

3
# Python stdlib imports
4
5
6
7
8
9
10
from datetime import (
    datetime,
    date,
    timedelta,
    time,
    tzinfo
)
11

CharlieC's avatar
CharlieC committed
12
import pytest
13

14

15
16
17
18
19
20
@pytest.mark.parametrize("value, expected",
                         [
                             (datetime(2013, 7, 15, 6, 52, 33), "2013-07-15T06:52:33"),
                             (datetime(2013, 7, 15, 6, 52, 33, 123456), "2013-07-15T06:52:33.123"),
                             (date(2013, 7, 15), "2013-07-15"),
                             (time(0, 1, 42), "00:01:42"),
21
                             (time(0, 1, 42, 123456), "00:01:42.123"),
22
23
24
                         ]
                         )
def test_to_iso(value, expected):
25
    from ..datetime import to_ISO8601
26
    assert to_ISO8601(value) == expected
27
28


CharlieC's avatar
CharlieC committed
29
30
31
@pytest.mark.parametrize("value, group, expected",
                         [
                             ("2011-06-30", "date", "2011-06-30"),
32
                             ("12:19", "time", "12:19"),
CharlieC's avatar
CharlieC committed
33
                             ("12:19:01", "time", "12:19:01"),
34
35
                             ("12:19:01.123", "time", "12:19:01.123"),
                             ("12:19:01.2", "time", "12:19:01.2"),
CharlieC's avatar
CharlieC committed
36
37
38
                         ]
                         )
def test_iso_regex(value, group, expected):
39
40
    from ..datetime import ISO_REGEX
    match = ISO_REGEX.match(value)
CharlieC's avatar
CharlieC committed
41
42
43
44
    assert match is not None
    assert match.groupdict()[group] == expected


CharlieC's avatar
CharlieC committed
45
46
47
48
@pytest.mark.parametrize("value, expected",
                         [
                             ("2011-06-30T13:35:26Z", datetime(2011, 6, 30, 13, 35, 26)),
                             ("2013-03-04T12:19:01.00Z", datetime(2013, 3, 4, 12, 19, 1)),
CharlieC's avatar
CharlieC committed
49
                             ("2011-06-30", date(2011, 6, 30)),
50
                             ("12:19", time(12, 19)),
CharlieC's avatar
CharlieC committed
51
                             ("12:19:01", time(12, 19, 1)),
52
53
                             ("12:19:01.123", time(12, 19, 1, 123_000)),
                             ("12:19:01.2", time(12, 19, 1, 200_000)),
54
55
56
                             ("2020-12-03T12:19:01.300Z", datetime(2020, 12, 3, 12, 19, 1, 300_000)),
                             ("2020-12-03T12:19:01.030", datetime(2020, 12, 3, 12, 19, 1, 30_000)),
                             ("2020-12-03T12:19:01.003Z", datetime(2020, 12, 3, 12, 19, 1, 3000)),
57
58
                             ("2020-12-03T12:19:01.3Z", datetime(2020, 12, 3, 12, 19, 1, 300_000)),
                             ("2020-12-03T12:19:01.03", datetime(2020, 12, 3, 12, 19, 1, 30_000)),
CharlieC's avatar
CharlieC committed
59
60
                         ]
                         )
61
62
63
def test_from_iso(value, expected):
    from ..datetime  import from_ISO8601
    assert from_ISO8601(value) == expected
64
65


66
67
@pytest.mark.parametrize("value, expected",
                         [
68
                             (date(1899, 12, 31), 0),
69
                             (date(1900, 1, 1), 1),
70
71
                             (date(1900, 1, 15), 15),
                             (date(1900, 2, 28), 59),
72
                             (datetime(1900, 2, 28, 21, 0, 0), 59.875),
73
                             (date(1900, 3, 1), 61),
74
                             (datetime(2010, 1, 18, 14, 15, 20, 1600), 40196.5939815),
CharlieC's avatar
CharlieC committed
75
                             (date(2009, 12, 20), 40167),
76
                             (datetime(1506, 10, 15), -143617.0),
77
                             (date(1, 1, 1), -693593),
78
79
80
81
                             (time(0), 0),
                             (time(6, 0), 0.25),
                             (timedelta(hours=6), 0.25),
                             (timedelta(hours=-6), -0.25),
82
83
                         ])
def test_to_excel(value, expected):
CharlieC's avatar
CharlieC committed
84
    from ..datetime import to_excel
85
86
87
88
    FUT = to_excel
    assert FUT(value) == expected


89
90
@pytest.mark.parametrize("value, expected",
                         [
CharlieC's avatar
CharlieC committed
91
92
                             (date(1904, 1, 1), 0),
                             (date(2011, 10, 31), 39385),
93
                             (datetime(2010, 1, 18, 14, 15, 20, 1600), 38734.5939815),
CharlieC's avatar
CharlieC committed
94
                             (date(2009, 12, 20), 38705),
95
                             (datetime(1506, 10, 15), -145079.0),
96
                             (date(1, 1, 1), -695055),
97
98
99
100
                             (time(0), 0),
                             (time(6, 0), 0.25),
                             (timedelta(hours=6), 0.25),
                             (timedelta(hours=-6), -0.25),
101
102
                         ])
def test_to_excel_mac(value, expected):
CharlieC's avatar
CharlieC committed
103
    from ..datetime import to_excel, CALENDAR_MAC_1904
104
105
106
107
    FUT = to_excel
    assert FUT(value, CALENDAR_MAC_1904) == expected


108
109
110
111
@pytest.mark.parametrize("value, expected",
                         [
                             (40167, datetime(2009, 12, 20)),
                             (21980, datetime(1960,  3,  5)),
112
                             (59, datetime(1900, 2, 28)),
113
                             (-25063, datetime(1831, 5, 18, 0, 0)),
114
115
116
117
                             (59.875, datetime(1900, 2, 28, 21, 0, 0)),
                             (60, datetime(1900, 2, 28, 0, 0)),
                             (60.5, datetime(1900, 2, 28, 12, 0)),
                             (61, datetime(1900, 3, 1, 0, 0)),
118
                             (40372.27616898148, datetime(2010, 7, 13, 6, 37, 41)),
119
                             (40196.5939815, datetime(2010, 1, 18, 14, 15, 20, 2000)),
CharlieC's avatar
CharlieC committed
120
                             (0.125, time(3, 0)),
121
                             (42126.958333333219, datetime(2015, 5, 2, 23, 0, 0, 0)),
122
                             (42126.999999999884, datetime(2015, 5, 3, 0, 0, 0)),
123
                             (0, time(0)),
124
125
                             (0.9999999995, datetime(1900, 1, 1)),
                             (1, datetime(1900, 1, 1)),
126
                             (-0.25, datetime(1899, 12, 29, 18, 0, 0)),
CharlieC's avatar
CharlieC committed
127
                             (None, None),
128
129
                         ])
def test_from_excel(value, expected):
CharlieC's avatar
CharlieC committed
130
    from ..datetime import from_excel
131
132
    FUT = from_excel
    assert FUT(value) == expected
133
134
135
136
137
138


@pytest.mark.parametrize("value, expected",
                         [
                             (39385, datetime(2011, 10, 31)),
                             (21980, datetime(1964,  3,  6)),
139
140
141
142
                             (0, time(0, 0)),
                             (-25063, datetime(1835, 5, 19)),
                             (0.75, time(18, 0)),
                             (-0.25, datetime(1903, 12, 31, 18, 0, 0)),
143
144
                         ])
def test_from_excel_mac(value, expected):
CharlieC's avatar
CharlieC committed
145
    from ..datetime import from_excel, CALENDAR_MAC_1904
146
147
148
    FUT = from_excel
    assert FUT(value, CALENDAR_MAC_1904) == expected

CharlieC's avatar
CharlieC committed
149
150

def test_time_to_days():
CharlieC's avatar
CharlieC committed
151
    from ..datetime  import time_to_days
CharlieC's avatar
CharlieC committed
152
153
    FUT = time_to_days
    t1 = time(13, 55, 12, 36)
154
    assert FUT(t1) == 0.5800000004166667
155
156
    t2 = time(3, 0, 0)
    assert FUT(t2) == 0.125
CharlieC's avatar
CharlieC committed
157
158
159


def test_timedelta_to_days():
CharlieC's avatar
CharlieC committed
160
    from ..datetime import timedelta_to_days
CharlieC's avatar
CharlieC committed
161
162
163
    FUT = timedelta_to_days
    td = timedelta(days=1, hours=3)
    assert FUT(td) == 1.125
164
165
166


def test_days_to_time():
CharlieC's avatar
CharlieC committed
167
    from ..datetime import days_to_time
168
169
    td = timedelta(0, 51320, 1600)
    FUT = days_to_time
CharlieC's avatar
CharlieC committed
170
    assert FUT(td) == time(14, 15, 20, 1600)
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193


class CET(tzinfo):

    def utcoffset(self, dt):
        return timedelta(hours=1) + self.dst(dt)

    def dst(self, dt):
        # DST starts last Sunday in March
        d = datetime(dt.year, 4, 1)   # ends last Sunday in October
        self.dston = d - timedelta(days=d.weekday() + 1)
        d = datetime(dt.year, 11, 1)
        self.dstoff = d - timedelta(days=d.weekday() + 1)
        if self.dston <=  dt.replace(tzinfo=None) < self.dstoff:
            return timedelta(hours=1)
        else:
            return timedelta(0)

    def tzname(self,dt):
        return "CET +1"


def test_localised_time():
CharlieC's avatar
CharlieC committed
194

195
    from ..datetime import time_to_days, UTC
CharlieC's avatar
CharlieC committed
196

197
    dt1 = datetime(2015, 7, 24, tzinfo=UTC)
198
199
200
201
202
203
204
205
    dt2 = datetime(2015, 7, 24, 2, tzinfo=CET())
    assert dt2 - dt2 == timedelta(0)

    e1 = time_to_days(dt1)
    e2 = time_to_days(dt2)

    assert e1 == 0
    assert e2 == 0