2
sys.path.insert(0, "../src")
3
import unittest, time, types
4
import globals, dup_time
6
class TimeTest(unittest.TestCase):
7
def testConversion(self):
8
"""test timetostring and stringtotime"""
10
assert type(dup_time.curtime) is types.FloatType
11
assert type(dup_time.curtimestr) is types.StringType
12
assert (dup_time.cmp(int(dup_time.curtime), dup_time.curtimestr) == 0 or
13
dup_time.cmp(int(dup_time.curtime) + 1, dup_time.curtimestr) == 0)
15
assert dup_time.cmp(time.time(), dup_time.curtime) == 1
16
assert dup_time.cmp(dup_time.timetostring(time.time()), dup_time.curtimestr) == 1
18
def testConversion_separator(self):
19
"""Same as testConversion, but change time Separator"""
20
globals.time_separator = "_"
22
globals.time_separator = ":"
25
"""Test time comparisons"""
30
assert cmp("2001-09-01T21:49:04Z", "2001-08-01T21:49:04Z") == 1
31
assert cmp("2001-09-01T04:49:04+03:23", "2001-09-01T21:49:04Z") == -1
32
assert cmp("2001-09-01T12:00:00Z", "2001-09-01T04:00:00-08:00") == 0
33
assert cmp("2001-09-01T12:00:00-08:00",
34
"2001-09-01T12:00:00-07:00") == 1
36
def testCmp_separator(self):
37
"""Like testCmp but with new separator"""
38
globals.time_separator = "_"
43
assert cmp("2001-09-01T21_49_04Z", "2001-08-01T21_49_04Z") == 1
44
assert cmp("2001-09-01T04_49_04+03_23", "2001-09-01T21_49_04Z") == -1
45
assert cmp("2001-09-01T12_00_00Z", "2001-09-01T04_00_00-08_00") == 0
46
assert cmp("2001-09-01T12_00_00-08_00",
47
"2001-09-01T12_00_00-07_00") == 1
48
globals.time_separator = ":"
50
def testStringtotime(self):
51
"""Test converting string to time"""
52
timesec = int(time.time())
53
assert timesec == int(dup_time.stringtotime(dup_time.timetostring(timesec)))
54
assert not dup_time.stringtotime("2001-18-83T03:03:03Z")
55
assert not dup_time.stringtotime("2001-01-23L03:03:03L")
56
assert not dup_time.stringtotime("2001_01_23T03:03:03Z")
58
def testIntervals(self):
59
"""Test converting strings to intervals"""
60
i2s = dup_time.intstringtoseconds
61
for s in ["32", "", "d", "231I", "MM", "s", "-2h"]:
63
except dup_time.TimeException: pass
65
assert i2s("7D") == 7*86400
66
assert i2s("232s") == 232
67
assert i2s("2M") == 2*30*86400
68
assert i2s("400m") == 400*60
69
assert i2s("1Y") == 365*86400
70
assert i2s("30h") == 30*60*60
71
assert i2s("3W") == 3*7*86400
73
def testIntervalsComposite(self):
74
"""Like above, but allow composite intervals"""
75
i2s = dup_time.intstringtoseconds
76
assert i2s("7D2h") == 7*86400 + 2*3600
77
assert i2s("2Y3s") == 2*365*86400 + 3
78
assert i2s("1M2W4D2h5m20s") == (30*86400 + 2*7*86400 + 4*86400 +
81
def testPrettyIntervals(self):
82
"""Test printable interval conversion"""
83
assert dup_time.inttopretty(3600) == "1 hour"
84
assert dup_time.inttopretty(7220) == "2 hours 20 seconds"
85
assert dup_time.inttopretty(0) == "0 seconds"
86
assert dup_time.inttopretty(353) == "5 minutes 53 seconds"
87
assert dup_time.inttopretty(3661) == "1 hour 1 minute 1 second"
88
assert dup_time.inttopretty(353.234234) == "5 minutes 53.23 seconds"
90
def testGenericString(self):
91
"""Test genstrtotime, conversion of arbitrary string to time"""
92
g2t = dup_time.genstrtotime
93
assert g2t('now', 1000) == 1000
94
assert g2t('2h3s', 10000) == 10000 - 2*3600 - 3
95
assert g2t('2001-09-01T21:49:04Z') == \
96
dup_time.stringtotime('2001-09-01T21:49:04Z')
97
assert g2t('2002-04-26T04:22:01') == \
98
dup_time.stringtotime('2002-04-26T04:22:01' + dup_time.gettzd())
99
t = dup_time.stringtotime('2001-05-12T00:00:00' + dup_time.gettzd())
100
assert g2t('2001-05-12') == t
101
assert g2t('2001/05/12') == t
102
assert g2t('5/12/2001') == t
103
assert g2t('123456') == 123456
105
def testGenericStringErrors(self):
106
"""Test genstrtotime on some bad strings"""
107
g2t = dup_time.genstrtotime
108
self.assertRaises(dup_time.TimeException, g2t, "hello")
109
self.assertRaises(dup_time.TimeException, g2t, "")
110
self.assertRaises(dup_time.TimeException, g2t, "3q")
112
def testSleeping(self):
113
"""Test sleep and sleep ratio"""
116
dup_time.sleep(0) # set initial time
119
dup_time.sleep(sleep_ratio)
123
dup_time.sleep(sleep_ratio)
129
dup_time.sleep(sleep_ratio)
132
assert 0.9 < time3 - time2 < 1.1, time3 - time2
133
assert 0.4 < time5 - time4 < 0.6, time5 - time4
134
assert 0.2 < time7 - time6 < 0.3, time7 - time6
137
if __name__ == '__main__': unittest.main()