test_datetime.py 8.3 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
def test_to_iso():
    from ..datetime import to_ISO8601
    assert to_ISO8601(datetime(2013, 7, 15, 6, 52, 33)) == "2013-07-15T06:52:33Z"
18
19


CharlieC's avatar
CharlieC committed
20
21
22
@pytest.mark.parametrize("value, group, expected",
                         [
                             ("2011-06-30", "date", "2011-06-30"),
23
                             ("12:19", "time", "12:19"),
CharlieC's avatar
CharlieC committed
24
                             ("12:19:01", "time", "12:19:01"),
25
26
                             ("12:19:01.123", "time", "12:19:01.123"),
                             ("12:19:01.2", "time", "12:19:01.2"),
CharlieC's avatar
CharlieC committed
27
28
29
                         ]
                         )
def test_iso_regex(value, group, expected):
30
31
    from ..datetime import ISO_REGEX
    match = ISO_REGEX.match(value)
CharlieC's avatar
CharlieC committed
32
33
34
35
    assert match is not None
    assert match.groupdict()[group] == expected


CharlieC's avatar
CharlieC committed
36
37
38
39
@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
40
                             ("2011-06-30", date(2011, 6, 30)),
41
                             ("12:19", time(12, 19)),
CharlieC's avatar
CharlieC committed
42
                             ("12:19:01", time(12, 19, 1)),
43
44
                             ("12:19:01.123", time(12, 19, 1, 123_000)),
                             ("12:19:01.2", time(12, 19, 1, 200_000)),
45
46
47
                             ("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)),
48
49
                             ("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
50
51
                         ]
                         )
52
53
54
def test_from_iso(value, expected):
    from ..datetime  import from_ISO8601
    assert from_ISO8601(value) == expected
55
56


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


80
81
@pytest.mark.parametrize("value, expected",
                         [
CharlieC's avatar
CharlieC committed
82
83
                             (date(1904, 1, 1), 0),
                             (date(2011, 10, 31), 39385),
84
                             (datetime(2010, 1, 18, 14, 15, 20, 1600), 38734.5939815),
CharlieC's avatar
CharlieC committed
85
                             (date(2009, 12, 20), 38705),
86
                             (datetime(1506, 10, 15), -145079.0),
87
                             (date(1, 1, 1), -695055),
88
89
90
91
                             (time(0), 0),
                             (time(6, 0), 0.25),
                             (timedelta(hours=6), 0.25),
                             (timedelta(hours=-6), -0.25),
92
93
                         ])
def test_to_excel_mac(value, expected):
CharlieC's avatar
CharlieC committed
94
    from ..datetime import to_excel, CALENDAR_MAC_1904
95
96
97
98
    FUT = to_excel
    assert FUT(value, CALENDAR_MAC_1904) == expected


99
100
101
102
@pytest.mark.parametrize("value, expected",
                         [
                             (40167, datetime(2009, 12, 20)),
                             (21980, datetime(1960,  3,  5)),
103
                             (59, datetime(1900, 2, 28)),
104
                             (-25063, datetime(1831, 5, 18, 0, 0)),
105
106
107
108
                             (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)),
109
                             (40372.27616898148, datetime(2010, 7, 13, 6, 37, 41)),
110
                             (40196.5939815, datetime(2010, 1, 18, 14, 15, 20, 2000)),
CharlieC's avatar
CharlieC committed
111
                             (0.125, time(3, 0)),
112
                             (42126.958333333219, datetime(2015, 5, 2, 23, 0, 0, 0)),
113
                             (42126.999999999884, datetime(2015, 5, 3, 0, 0, 0)),
114
                             (0, time(0)),
115
116
                             (0.9999999995, datetime(1900, 1, 1)),
                             (1, datetime(1900, 1, 1)),
117
                             (-0.25, datetime(1899, 12, 29, 18, 0, 0)),
CharlieC's avatar
CharlieC committed
118
                             (None, None),
119
120
                         ])
def test_from_excel(value, expected):
CharlieC's avatar
CharlieC committed
121
    from ..datetime import from_excel
122
123
    FUT = from_excel
    assert FUT(value) == expected
124
125


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
@pytest.mark.parametrize("value, expected",
                         [
                             (0, timedelta(hours=0)),
                             (0.5, timedelta(hours=12)),
                             (-0.5, timedelta(hours=-12)),
                             (1.25, timedelta(hours=30)),
                             (-1.25, timedelta(hours=-30)),
                             (59.5, timedelta(days=59, hours=12)),
                             (60.5, timedelta(days=60, hours=12)),
                             (61.5, timedelta(days=61, hours=12)),
                             (0.9999999995, timedelta(days=1)),
                             (1.0000000005, timedelta(days=1)),
                             (None, None),
                         ])
def test_from_excel_timedelta(value, expected):
    from ..datetime import from_excel
    FUT = from_excel
    assert FUT(value, timedelta=True) == expected


146
147
148
149
@pytest.mark.parametrize("value, expected",
                         [
                             (39385, datetime(2011, 10, 31)),
                             (21980, datetime(1964,  3,  6)),
150
151
152
153
                             (0, time(0, 0)),
                             (-25063, datetime(1835, 5, 19)),
                             (0.75, time(18, 0)),
                             (-0.25, datetime(1903, 12, 31, 18, 0, 0)),
154
155
                         ])
def test_from_excel_mac(value, expected):
CharlieC's avatar
CharlieC committed
156
    from ..datetime import from_excel, CALENDAR_MAC_1904
157
158
159
    FUT = from_excel
    assert FUT(value, CALENDAR_MAC_1904) == expected

CharlieC's avatar
CharlieC committed
160
161

def test_time_to_days():
CharlieC's avatar
CharlieC committed
162
    from ..datetime  import time_to_days
CharlieC's avatar
CharlieC committed
163
164
    FUT = time_to_days
    t1 = time(13, 55, 12, 36)
165
    assert FUT(t1) == 0.5800000004166667
166
167
    t2 = time(3, 0, 0)
    assert FUT(t2) == 0.125
CharlieC's avatar
CharlieC committed
168
169
170


def test_timedelta_to_days():
CharlieC's avatar
CharlieC committed
171
    from ..datetime import timedelta_to_days
CharlieC's avatar
CharlieC committed
172
173
174
    FUT = timedelta_to_days
    td = timedelta(days=1, hours=3)
    assert FUT(td) == 1.125
175
176
177


def test_days_to_time():
CharlieC's avatar
CharlieC committed
178
    from ..datetime import days_to_time
179
180
    td = timedelta(0, 51320, 1600)
    FUT = days_to_time
CharlieC's avatar
CharlieC committed
181
    assert FUT(td) == time(14, 15, 20, 1600)
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204


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
205

206
    from ..datetime import time_to_days, UTC
CharlieC's avatar
CharlieC committed
207

208
    dt1 = datetime(2015, 7, 24, tzinfo=UTC)
209
210
211
212
213
214
215
216
    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