30
30
def test_format_patch_date(self):
31
31
# epoch is always in utc
32
32
self.assertEqual('1970-01-01 00:00:00 +0000',
33
timestamp.format_patch_date(0))
34
self.assertEqual('1970-01-01 00:00:00 +0000',
35
timestamp.format_patch_date(0, 5 * 3600))
36
self.assertEqual('1970-01-01 00:00:00 +0000',
37
timestamp.format_patch_date(0, -5 * 3600))
33
timestamp.format_patch_date(0))
34
self.assertEqual('1970-01-01 00:00:00 +0000',
35
timestamp.format_patch_date(0, 5 * 3600))
36
self.assertEqual('1970-01-01 00:00:00 +0000',
37
timestamp.format_patch_date(0, -5 * 3600))
38
38
# regular timestamp with typical timezone
39
39
self.assertEqual('2007-03-06 10:04:19 -0500',
40
timestamp.format_patch_date(1173193459, -5 * 3600))
40
timestamp.format_patch_date(1173193459, -5 * 3600))
41
41
# the timezone part is HHMM
42
42
self.assertEqual('2007-03-06 09:34:19 -0530',
43
timestamp.format_patch_date(1173193459, -5.5 * 3600))
43
timestamp.format_patch_date(1173193459, -5.5 * 3600))
44
44
# timezones can be offset by single minutes (but no less)
45
45
self.assertEqual('2007-03-06 15:05:19 +0001',
46
timestamp.format_patch_date(1173193459, +1 * 60))
46
timestamp.format_patch_date(1173193459, +1 * 60))
48
48
def test_parse_patch_date(self):
49
self.assertEqual((0, 0),
50
51
timestamp.parse_patch_date('1970-01-01 00:00:00 +0000'))
51
52
# even though we don't emit pre-epoch dates, we can parse them
52
self.assertEqual((0, -5 * 3600),
53
55
timestamp.parse_patch_date('1969-12-31 19:00:00 -0500'))
54
self.assertEqual((0, +5 * 3600),
55
58
timestamp.parse_patch_date('1970-01-01 05:00:00 +0500'))
56
self.assertEqual((1173193459, -5 * 3600),
60
(1173193459, -5 * 3600),
57
61
timestamp.parse_patch_date('2007-03-06 10:04:19 -0500'))
58
62
# offset of three minutes
59
self.assertEqual((1173193459, +3 * 60),
64
(1173193459, +3 * 60),
60
65
timestamp.parse_patch_date('2007-03-06 15:07:19 +0003'))
61
66
# No space between time and offset
62
self.assertEqual((1173193459, -5 * 3600),
68
(1173193459, -5 * 3600),
63
69
timestamp.parse_patch_date('2007-03-06 10:04:19-0500'))
65
self.assertEqual((1173193459, -5 * 3600),
72
(1173193459, -5 * 3600),
66
73
timestamp.parse_patch_date('2007-03-06 10:04:19 -0500'))
68
75
def test_parse_patch_date_bad(self):
69
76
self.assertRaises(ValueError, timestamp.parse_patch_date,
71
78
# Extra data at end
72
79
self.assertRaises(ValueError, timestamp.parse_patch_date,
73
'2007-03-06 10:04:19 -0500x')
80
'2007-03-06 10:04:19 -0500x')
75
82
self.assertRaises(ValueError, timestamp.parse_patch_date,
76
'2007-03 10:04:19 -0500')
83
'2007-03 10:04:19 -0500')
78
85
self.assertRaises(ValueError, timestamp.parse_patch_date,
79
'2007-03-06 10:04 -0500')
86
'2007-03-06 10:04 -0500')
81
88
self.assertRaises(ValueError, timestamp.parse_patch_date,
82
'2007-03-06 10:04:19')
89
'2007-03-06 10:04:19')
83
90
# Missing plus or minus in offset
84
91
self.assertRaises(ValueError, timestamp.parse_patch_date,
85
'2007-03-06 10:04:19 0500')
92
'2007-03-06 10:04:19 0500')
86
93
# Invalid hour in offset
87
94
self.assertRaises(ValueError, timestamp.parse_patch_date,
88
'2007-03-06 10:04:19 +2400')
95
'2007-03-06 10:04:19 +2400')
89
96
self.assertRaises(ValueError, timestamp.parse_patch_date,
90
'2007-03-06 10:04:19 -2400')
97
'2007-03-06 10:04:19 -2400')
91
98
# Invalid minute in offset
92
99
self.assertRaises(ValueError, timestamp.parse_patch_date,
93
'2007-03-06 10:04:19 -0560')
100
'2007-03-06 10:04:19 -0560')
94
101
# Too many digits in offset
95
102
self.assertRaises(ValueError, timestamp.parse_patch_date,
96
'2007-03-06 10:04:19 79500')
103
'2007-03-06 10:04:19 79500')
97
104
# Minus sign in middle of offset
98
105
self.assertRaises(ValueError, timestamp.parse_patch_date,
99
'2007-03-06 10:04:19 +05-5')
106
'2007-03-06 10:04:19 +05-5')
102
109
class UnpackHighresDateTests(tests.TestCase):
104
111
def test_unpack_highres_date(self):
105
112
self.assertEqual(
106
113
(1120153132.3508501, -18000),
107
timestamp.unpack_highres_date('Thu 2005-06-30 12:38:52.350850105 -0500'))
114
timestamp.unpack_highres_date(
115
'Thu 2005-06-30 12:38:52.350850105 -0500'))
108
116
self.assertEqual(
109
117
(1120153132.3508501, 0),
110
timestamp.unpack_highres_date('Thu 2005-06-30 17:38:52.350850105 +0000'))
118
timestamp.unpack_highres_date(
119
'Thu 2005-06-30 17:38:52.350850105 +0000'))
111
120
self.assertEqual(
112
121
(1120153132.3508501, 7200),
113
timestamp.unpack_highres_date('Thu 2005-06-30 19:38:52.350850105 +0200'))
122
timestamp.unpack_highres_date(
123
'Thu 2005-06-30 19:38:52.350850105 +0200'))
114
124
self.assertEqual(
115
125
(1152428738.867522, 19800),
116
timestamp.unpack_highres_date('Sun 2006-07-09 12:35:38.867522001 +0530'))
126
timestamp.unpack_highres_date(
127
'Sun 2006-07-09 12:35:38.867522001 +0530'))
118
129
def test_random(self):
120
131
o = local_time_offset()
121
t2, o2 = timestamp.unpack_highres_date(timestamp.format_highres_date(t, o))
132
t2, o2 = timestamp.unpack_highres_date(
133
timestamp.format_highres_date(t, o))
122
134
self.assertEqual(t, t2)
123
135
self.assertEqual(o, o2)
124
t -= 24*3600*365*2 # Start 2 years ago
136
t -= 24 * 3600 * 365 * 2 # Start 2 years ago
126
138
for count in range(500):
127
t += random.random()*24*3600*30
139
t += random.random() * 24 * 3600 * 30
130
142
except OverflowError:
131
143
# We've reached the maximum for time_t on this platform
133
145
if time.localtime(t).tm_year > 9998:
134
# strptime doesn't always understand years with more than 4 digits.
146
# strptime doesn't always understand years with more than 4
136
o = ((o/3600 + 13) % 25 - 12)*3600 # Add 1 wrap around from [-12, 12]
149
# Add 1 wrap around from [-12, 12]
150
o = ((o / 3600 + 13) % 25 - 12) * 3600
137
151
date = timestamp.format_highres_date(t, o)
138
152
t2, o2 = timestamp.unpack_highres_date(date)
139
self.assertEqual(t, t2,
140
'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t))
141
self.assertEqual(o, o2,
142
'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t))
155
'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2 - t))
158
'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2 - t))