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