This repository has been archived by the owner on Dec 7, 2022. It is now read-only.
/
test_dateutils.py
146 lines (110 loc) · 4.63 KB
/
test_dateutils.py
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
142
143
144
145
146
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright © 2011 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
import datetime
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)) + "/../common/")
import testutil
from pulp.common import dateutils
# test timezones and timezone conversions --------------------------------------
_zero = datetime.timedelta(0)
_one_hour = datetime.timedelta(hours=1)
class _StdZone(datetime.tzinfo):
def __init__(self, utc_offset=0):
self.utc_offset = utc_offset
def dst(self, dt):
return _zero
def fromutc(self, dt):
return dt + self.utcoffset(dt)
def tzname(self, dt):
return 'UTC %d' % self.utc_offset
def utcoffset(self, dt):
return datetime.timedelta(minutes=(self.utc_offset * 60)) - self.dst(dt)
class _DayZone(datetime.tzinfo):
def __init__(self, utc_offset=0):
self.utc_offset = utc_offset
def dst(self, dt):
return _one_hour
def fromutc(self, dt):
return dt + self.utcoffset(dt)
def tzname(self, dt):
return 'UTC %d' % self.utc_offset
def utcoffset(self, dt):
return datetime.timedelta(minutes=(self.utc_offset * 60)) - self.dst(dt)
class TimezoneTester(testutil.PulpAsyncTest):
def test_utc_conversion(self):
s = datetime.datetime.now(_StdZone())
d = s.astimezone(_DayZone())
su = s.astimezone(dateutils.utc_tz())
du = d.astimezone(dateutils.utc_tz())
self.assertTrue(su == du)
def test_local_to_utz_tz_conversion(self):
n1 = datetime.datetime.now(dateutils.local_tz())
u = dateutils.to_utc_datetime(n1)
n2 = dateutils.to_local_datetime(u)
self.assertTrue(n1 == n2)
def test_utc_offset(self):
n1 = datetime.datetime.now(dateutils.local_tz())
u1 = dateutils.to_utc_datetime(n1)
n2 = n1.replace(tzinfo=None)
u2 = u1.replace(tzinfo=None)
self.assertTrue(n2 - u2 == dateutils.local_utcoffset_delta())
# test iso8601 support ---------------------------------------------------------
class ISO8601Tester(testutil.PulpAsyncTest):
def setUp(self):
testutil.PulpAsyncTest.setUp(self)
self.dt_fields = ('year', 'month', 'day', 'hour', 'minute', 'second')
def test_datetime_sans_tz(self):
n = datetime.datetime.now()
s = dateutils.format_iso8601_datetime(n)
b = dateutils.parse_iso8601_datetime(s)
for f in self.dt_fields:
self.assertTrue(getattr(n, f) == getattr(b, f), 'Field mismatch: %s' % f)
def test_datetime_with_tz(self):
n = datetime.datetime.now(dateutils.local_tz())
s = dateutils.format_iso8601_datetime(n)
b = dateutils.parse_iso8601_datetime(s)
for f in self.dt_fields:
self.assertTrue(getattr(n, f) == getattr(b, f), 'Field mismatch: %s' % f)
def test_interval(self):
d = datetime.timedelta(hours=1)
s = dateutils.format_iso8601_interval(d)
i, t, r = dateutils.parse_iso8601_interval(s)
self.assertTrue(d == i)
def test_interval_recurrences(self):
d = datetime.timedelta(hours=4, minutes=2, seconds=59)
c = 4
s = dateutils.format_iso8601_interval(d, recurrences=c)
i, t, r = dateutils.parse_iso8601_interval(s)
self.assertEqual(d, i)
self.assertEqual(c, r)
def test_interval_start_time(self):
d = datetime.timedelta(minutes=2)
t = datetime.datetime(year=2014, month=11, day=5, hour=0, minute=23)
s = dateutils.format_iso8601_interval(d, t)
i, e, r = dateutils.parse_iso8601_interval(s)
self.assertEqual(d, i)
self.assertEqual(t, e)
def test_interval_full(self):
i1 = datetime.timedelta(hours=100)
t1 = datetime.datetime(year=2, month=6, day=20, hour=2, minute=22, second=46)
r1 = 5
s = dateutils.format_iso8601_interval(i1, t1, r1)
i2, t2, r2 = dateutils.parse_iso8601_interval(s)
self.assertEqual(i1, i2)
self.assertEqual(t1, t2)
self.assertEqual(r1, r2)
# ------------------------------------------------------------------------------
if __name__ == '__main__':
unittest.main()