1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
import pytest |
4
|
|
|
import datetime |
5
|
|
|
|
6
|
|
|
import pytz |
7
|
|
|
|
8
|
|
|
from . import timestamp_and_seconds2datetime |
9
|
|
|
from . import datetime2timestamp_and_seconds |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class Test_timestamp_and_seconds2datetime(object): |
13
|
|
|
|
14
|
|
|
@pytest.mark.parametrize('d, t', [ |
15
|
|
|
(None, 11), |
16
|
|
|
(None, None), |
17
|
|
|
]) |
18
|
|
|
def test_when_none(self, d, t): |
19
|
|
|
act = timestamp_and_seconds2datetime(d, t) |
20
|
|
|
|
21
|
|
|
assert act is None |
22
|
|
|
|
23
|
|
|
@pytest.mark.parametrize('d, t, exp', [ |
24
|
|
|
# 1400111222 |
25
|
|
|
# 2014-05-14T23:47:02+00:00 |
26
|
|
|
# (from https://www.unixtimestamp.com/index.php) |
27
|
|
|
(1400111222, None, datetime.datetime(2014, 5, 14, 23, 47, 2, tzinfo=pytz.utc)), |
28
|
|
|
(1400111222, 11, datetime.datetime(2014, 5, 14, 23, 47, 13, tzinfo=pytz.utc)), |
29
|
|
|
|
30
|
|
|
# 1504137600 is equivalent to: |
31
|
|
|
# 2017-08-31T00:00:00+00:00 in ISO 8601 |
32
|
|
|
# (from https://www.unixtimestamp.com/index.php) |
33
|
|
|
(1504137600, 0, datetime.datetime(2017, 8, 31, 0, 0, 0, tzinfo=pytz.utc)), |
34
|
|
|
(1504137600, 16281, datetime.datetime(2017, 8, 31, 4, 31, 21, tzinfo=pytz.utc)), |
35
|
|
|
]) |
36
|
|
|
def test_normal_conversion_in_utc(self, d, t, exp): |
37
|
|
|
act = timestamp_and_seconds2datetime(d, t) |
38
|
|
|
|
39
|
|
|
assert act == exp |
40
|
|
|
|
41
|
|
|
@pytest.mark.parametrize('d, t, exp', [ |
42
|
|
|
# 1504222593 |
43
|
|
|
# 2017-08-31T23:36:33+00:00 in ISO 8601 |
44
|
|
|
(1504222593, 0, datetime.datetime(2017, 8, 31, 23, 36, 33, tzinfo=pytz.utc)), |
45
|
|
|
(1504222593, 11, datetime.datetime(2017, 8, 31, 23, 36, 44, tzinfo=pytz.utc)), # UTC!! |
46
|
|
|
]) |
47
|
|
|
def test_should_not_drop_TIME_in_from_first_argument(self, d, t, exp): |
48
|
|
|
act = timestamp_and_seconds2datetime(d, t) |
49
|
|
|
|
50
|
|
|
assert act == exp |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
class Test_datetime2timestamp_and_seconds(object): |
54
|
|
|
|
55
|
|
|
TIMEZONE_0300 = pytz.FixedOffset(180) |
56
|
|
|
|
57
|
|
|
@pytest.mark.parametrize('dt', [ |
58
|
|
|
(None), |
59
|
|
|
(11), |
60
|
|
|
('asdf'), |
61
|
|
|
(True), |
62
|
|
|
(4.5), |
63
|
|
|
(0), |
64
|
|
|
]) |
65
|
|
|
def test_raise_on_wrong_type(self, dt): |
66
|
|
|
with pytest.raises(TypeError) as exc_info: |
67
|
|
|
datetime2timestamp_and_seconds(dt) |
68
|
|
|
|
69
|
|
|
exc = exc_info.value |
70
|
|
|
assert 'datetime' in str(exc) |
71
|
|
|
assert 'dt' in str(exc) |
72
|
|
|
|
73
|
|
|
@pytest.mark.parametrize('dt, exp_ts, exp_sec', [ |
74
|
|
|
(datetime.datetime(2014, 5, 14, 0, 0, 11, tzinfo=pytz.utc), 1400025600, 11), |
75
|
|
|
|
76
|
|
|
# 1504137600 is equivalent to: |
77
|
|
|
# 2017-08-31T00:00:00+00:00 in ISO 8601 |
78
|
|
|
# (from https://www.unixtimestamp.com/index.php) |
79
|
|
|
(datetime.datetime(2017, 8, 31, 0, 0, 0, tzinfo=pytz.utc), 1504137600, 0), |
80
|
|
|
(datetime.datetime(2017, 8, 31, 4, 31, 21, tzinfo=pytz.utc), 1504137600, 16281), |
81
|
|
|
]) |
82
|
|
|
def test_normal_conversion_in_utc(self, dt, exp_ts, exp_sec): |
83
|
|
|
act_ts, act_sec = datetime2timestamp_and_seconds(dt) |
84
|
|
|
|
85
|
|
|
assert act_ts == exp_ts |
86
|
|
|
assert act_sec == exp_sec |
87
|
|
|
|
88
|
|
|
@pytest.mark.parametrize('dt, exp_ts, exp_sec', [ |
89
|
|
|
(datetime.datetime(2014, 5, 14, 0, 0, 11, tzinfo=TIMEZONE_0300), 1400014800, 11), |
90
|
|
|
|
91
|
|
|
# 1504137600 is equivalent to: |
92
|
|
|
# 2017-08-31T00:00:00+00:00 in ISO 8601 |
93
|
|
|
# (from https://www.unixtimestamp.com/index.php) |
94
|
|
|
(datetime.datetime(2017, 8, 31, 0, 0, 0, tzinfo=TIMEZONE_0300), 1504126800, 0), |
95
|
|
|
(datetime.datetime(2017, 8, 31, 4, 31, 21, tzinfo=TIMEZONE_0300), 1504126800, 16281), |
96
|
|
|
]) |
97
|
|
|
def test_normal_conversion_in_0300(self, dt, exp_ts, exp_sec): |
98
|
|
|
act_ts, act_sec = datetime2timestamp_and_seconds(dt) |
99
|
|
|
|
100
|
|
|
assert act_ts == exp_ts |
101
|
|
|
assert act_sec == exp_sec |
102
|
|
|
|