2
from itertools import starmap
3
from datetime import datetime as DT
5
from scales import ScaleSystem
6
from time_scale import dt_to_sec, trange, TimeScale, HMSScales
7
from formatters import TimeFormatter
9
from scales_test_case import TicksTestCase
13
""" Returns a unix-timestamp-like time """
14
return dt_to_sec(DT(*args, **kw))
16
def sec_from_hms(start, *times):
17
""" Returns a list of times based on adding each offset tuple in times
18
to the start time (which should be in seconds). Offset tuples can be
19
in any of the forms: (hours), (hours,minutes), or (hours,minutes,seconds).
34
class TRangeTestCase(TicksTestCase):
36
def test_null_ranges(self):
38
((2005,3,15,10,23,15), (2005,3,15,10,23,45), {"minutes":1}),
39
((2005,3,15,10,23), (2005,3,15,10,47), {"hours":1}),
40
((2005,3,15,5,23), (2005,3,15,18,43), {"days":1}),
41
((2005,3,15,10,30), (2005,12,25,18,30), {"years":1})
43
for start, end, kw in ranges:
44
self.assert_empty(trange(DTS(*start), DTS(*end), **kw))
47
def test_microseconds(self):
48
base = DTS(2005, 3, 15, 10, 45, 10)
49
start = base + 0.0000027
50
end = base + 0.0000088
51
ticks = trange(start, end, microseconds=1)
52
desired = [base+i for i in (3e-6, 4e-6, 5e-6, 6e-6, 7e-6, 8e-6)]
53
self.check_ticks(ticks, desired)
55
def test_milliseconds(self):
56
base = DTS(2005, 3, 15, 10, 45, 10)
59
ticks = trange(start, end, milliseconds=1)
60
desired = [base + i for i in (0.003, 0.004, 0.005, 0.006, 0.007)]
61
self.check_ticks(ticks, desired)
62
ticks = trange(start, end, milliseconds=2)
63
self.check_ticks(ticks, (base+0.004, base+0.006))
66
base = DTS(2005, 1, 1)
67
secs_per_day = 24*3600
68
ticks = trange(base, base + secs_per_day*5, days=1)
69
self.check_ticks(ticks, [base+i*secs_per_day for i in range(5)])
71
def test_daily_leap(self):
72
start = DTS(2004, 2, 27)
74
ticks = trange(start, end, days=1)
75
desired = [start + i*24*3600 for i in range(5)]
76
self.check_ticks(ticks, desired)
78
def test_hourly(self):
79
# test between Feb 29,2004 10:15pm and Mar 1st 3:15am
80
ticks = trange(DTS(2004,2,29,22,15), DTS(2004,3,1,3,15), hours=1)
81
start = DTS(2004,2,29,23)
82
desired = [start + i*3600 for i in range(5)]
83
self.check_ticks(ticks, desired)
85
def test_multiday_increment(self):
86
start = DTS(2005, 1, 1)
87
ticks = trange(start, start + 9*24*3600, days=3)
88
self.check_ticks(ticks, [start+i*3*24*3600 for i in range(3)])
92
class TimeScaleTestCase(TicksTestCase):
93
""" This exercises a single TimeScale set at various resolutions """
95
def test_hourly(self):
96
ts = TimeScale(hours=1)
97
start = DTS(2005, 3, 15, 10, 30)
98
end = DTS(2005, 3, 15, 16, 59)
99
desired_start = DTS(2005, 3, 15)
100
desired = [desired_start + i*3600 for i in (11, 12, 13, 14, 15, 16)]
101
self.check_ticks(ts.ticks(start, end), desired)
103
def test_minutes(self):
104
ts = TimeScale(minutes=15)
105
start = DTS(2005, 3, 15, 10, 20)
106
end = DTS(2005, 3, 15, 11, 55)
107
dstart = DTS(2005, 3, 15)
108
desired = ((10,30), (10,45), (11,00), (11,15), (11,30), (11,45))
109
self.check_ticks(ts.ticks(start, end),
110
sec_from_hms(dstart, *desired))
112
def test_day_of_month(self):
113
ts = TimeScale(day_of_month=(1,8,15,22))
114
start = DTS(2005,3,12)
116
desired = starmap(DTS, ((2005,3,15), (2005,3,22), (2005,4,1), (2005,4,8),
117
(2005,4,15), (2005,4,22), (2005,5,1)))
118
self.check_ticks(ts.ticks(start,end), desired)
120
# test adjacent months
121
start = DTS(2005, 3, 12)
122
end = DTS(2005, 4, 10)
123
desired = starmap(DTS, ((2005,3,15), (2005,3,22), (2005,4,1), (2005,4,8)))
124
self.check_ticks(ts.ticks(start,end), desired)
127
def test_month_of_year(self):
128
ts = TimeScale(month_of_year=(1,4,8))
129
start = DTS(2005,1,1)
131
desired = starmap(DTS, ((2005,1,1), (2005,4,1), (2005,8,1), (2006,1,1), (2006,4,1)))
132
self.check_ticks(ts.ticks(start,end), desired)
134
def test_microsecond(self):
135
ts = TimeScale(microseconds=1)
136
base = DTS(1975, 3, 15, 10, 45, 10)
137
start = base + 2.8e-6
139
desired = [base+i for i in (3e-6, 4e-6, 5e-6, 6e-6, 7e-6, 8e-6, 9e-6)]
140
self.check_ticks(ts.ticks(start, end), desired)
144
class CalendarScaleSystemTestCase(TicksTestCase):
145
""" This exercises the ability of multiple TimeScale objects to play well
146
within a single ScaleSystem.
149
def test_hourly_scales(self):
150
scales = [TimeScale(seconds=dt) for dt in (1, 5, 15, 30)] + \
151
[TimeScale(minutes=dt) for dt in (1, 5, 15, 30)] + \
152
[TimeScale(hours=dt) for dt in (1, 2, 3, 4, 6, 12)]
154
def test_yearly_scales(self):
155
ticker = ScaleSystem(TimeScale(month_of_year=1), default_scale=None)
156
ticks = ticker.ticks(DTS(2000,1,1), DTS(2007,1,1), 10)
157
desired = starmap(DTS, ((2000,1,1), (2001,1,1), (2002,1,1), (2003,1,1),
158
(2004,1,1), (2005,1,1), (2006,1,1), (2007,1,1)))
159
self.check_ticks(ticks, desired)
162
class TimeFormatterTestCase(TicksTestCase):
164
def test_widths(self):
165
fmt = TimeFormatter()
166
scale = TimeScale(minutes = 5)
167
test_intervals = ([(2005,3,15,10,30), (2005,3,15,10,50), 50],
170
for start, end, width in test_intervals:
171
est_width = scale.label_width(DTS(*start), DTS(*end), char_width=width)
173
print " avail:", width, "est:", est_width[1], "numlabels:", est_width[0]
176
def test_labels(self):
177
fmt = TimeFormatter()
178
scale = ScaleSystem(*HMSScales)
180
test_intervals = ([(2005,3,15,10,30), (2005,3,15,10,50), 150],
183
for start, end, width in test_intervals:
184
labels = scale.labels(DTS(*start), DTS(*end), char_width=width)
185
print start, end, " avail:", width,
186
print " used:", sum([len(x[1]) for x in labels]),
191
if __name__ == "__main__":