1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 #
18 # $Id: test_dates.py,v 1.21 2003-03-19 03:25:30 richard Exp $
20 import unittest, time
22 from roundup.date import Date, Interval, Range, fixTimeOverflow
24 class DateTestCase(unittest.TestCase):
25 def testDateInterval(self):
26 ae = self.assertEqual
27 date = Date("2000-06-26.00:34:02 + 2d")
28 ae(str(date), '2000-06-28.00:34:02')
29 date = Date("2000-02-27 + 2d")
30 ae(str(date), '2000-02-29.00:00:00')
31 date = Date("2001-02-27 + 2d")
32 ae(str(date), '2001-03-01.00:00:00')
34 def testDate(self):
35 ae = self.assertEqual
36 date = Date("2000-04-17")
37 ae(str(date), '2000-04-17.00:00:00')
38 date = Date("2000-4-7")
39 ae(str(date), '2000-04-07.00:00:00')
40 date = Date("2000-4-17")
41 ae(str(date), '2000-04-17.00:00:00')
42 date = Date("01-25")
43 y, m, d, x, x, x, x, x, x = time.gmtime(time.time())
44 ae(str(date), '%s-01-25.00:00:00'%y)
45 date = Date("2000-04-17.03:45")
46 ae(str(date), '2000-04-17.03:45:00')
47 date = Date("08-13.22:13")
48 ae(str(date), '%s-08-13.22:13:00'%y)
49 date = Date("11-07.09:32:43")
50 ae(str(date), '%s-11-07.09:32:43'%y)
51 date = Date("14:25")
52 ae(str(date), '%s-%02d-%02d.14:25:00'%(y, m, d))
53 date = Date("8:47:11")
54 ae(str(date), '%s-%02d-%02d.08:47:11'%(y, m, d))
56 def testDateError(self):
57 self.assertRaises(ValueError, Date, "12")
59 def testOffset(self):
60 ae = self.assertEqual
61 date = Date("2000-04-17", -5)
62 ae(str(date), '2000-04-17.05:00:00')
63 date = Date("01-25", -5)
64 y, m, d, x, x, x, x, x, x = time.gmtime(time.time())
65 ae(str(date), '%s-01-25.05:00:00'%y)
66 date = Date("2000-04-17.03:45", -5)
67 ae(str(date), '2000-04-17.08:45:00')
68 date = Date("08-13.22:13", -5)
69 ae(str(date), '%s-08-14.03:13:00'%y)
70 date = Date("11-07.09:32:43", -5)
71 ae(str(date), '%s-11-07.14:32:43'%y)
72 date = Date("14:25", -5)
73 ae(str(date), '%s-%02d-%02d.19:25:00'%(y, m, d))
74 date = Date("8:47:11", -5)
75 ae(str(date), '%s-%02d-%02d.13:47:11'%(y, m, d))
77 def testOffsetRandom(self):
78 ae = self.assertEqual
79 # XXX unsure of the usefulness of these, they're pretty random
80 date = Date('2000-01-01') + Interval('- 2y 2m')
81 ae(str(date), '1997-11-01.00:00:00')
82 date = Date('2000-01-01 - 2y 2m')
83 ae(str(date), '1997-11-01.00:00:00')
84 date = Date('2000-01-01') + Interval('2m')
85 ae(str(date), '2000-03-01.00:00:00')
86 date = Date('2000-01-01 + 2m')
87 ae(str(date), '2000-03-01.00:00:00')
89 date = Date('2000-01-01') + Interval('60d')
90 ae(str(date), '2000-03-01.00:00:00')
91 date = Date('2001-01-01') + Interval('60d')
92 ae(str(date), '2001-03-02.00:00:00')
94 def testOffsetAdd(self):
95 ae = self.assertEqual
96 date = Date('2000-02-28.23:59:59') + Interval('00:00:01')
97 ae(str(date), '2000-02-29.00:00:00')
98 date = Date('2001-02-28.23:59:59') + Interval('00:00:01')
99 ae(str(date), '2001-03-01.00:00:00')
101 date = Date('2000-02-28.23:58:59') + Interval('00:01:01')
102 ae(str(date), '2000-02-29.00:00:00')
103 date = Date('2001-02-28.23:58:59') + Interval('00:01:01')
104 ae(str(date), '2001-03-01.00:00:00')
106 date = Date('2000-02-28.22:58:59') + Interval('01:01:01')
107 ae(str(date), '2000-02-29.00:00:00')
108 date = Date('2001-02-28.22:58:59') + Interval('01:01:01')
109 ae(str(date), '2001-03-01.00:00:00')
111 date = Date('2000-02-28.22:58:59') + Interval('00:00:3661')
112 ae(str(date), '2000-02-29.00:00:00')
113 date = Date('2001-02-28.22:58:59') + Interval('00:00:3661')
114 ae(str(date), '2001-03-01.00:00:00')
116 def testOffsetSub(self):
117 ae = self.assertEqual
118 date = Date('2000-01-01') - Interval('- 2y 2m')
119 ae(str(date), '2002-03-01.00:00:00')
120 date = Date('2000-01-01') - Interval('2m')
121 ae(str(date), '1999-11-01.00:00:00')
123 date = Date('2000-03-01') - Interval('60d')
124 ae(str(date), '2000-01-01.00:00:00')
125 date = Date('2001-03-02') - Interval('60d')
126 ae(str(date), '2001-01-01.00:00:00')
128 date = Date('2000-02-29.00:00:00') - Interval('00:00:01')
129 ae(str(date), '2000-02-28.23:59:59')
130 date = Date('2001-03-01.00:00:00') - Interval('00:00:01')
131 ae(str(date), '2001-02-28.23:59:59')
133 date = Date('2000-02-29.00:00:00') - Interval('00:01:01')
134 ae(str(date), '2000-02-28.23:58:59')
135 date = Date('2001-03-01.00:00:00') - Interval('00:01:01')
136 ae(str(date), '2001-02-28.23:58:59')
138 date = Date('2000-02-29.00:00:00') - Interval('01:01:01')
139 ae(str(date), '2000-02-28.22:58:59')
140 date = Date('2001-03-01.00:00:00') - Interval('01:01:01')
141 ae(str(date), '2001-02-28.22:58:59')
143 date = Date('2000-02-29.00:00:00') - Interval('00:00:3661')
144 ae(str(date), '2000-02-28.22:58:59')
145 date = Date('2001-03-01.00:00:00') - Interval('00:00:3661')
146 ae(str(date), '2001-02-28.22:58:59')
148 def testDateLocal(self):
149 ae = self.assertEqual
150 date = Date("02:42:20")
151 date = date.local(10)
152 y, m, d, x, x, x, x, x, x = time.gmtime(time.time())
153 ae(str(date), '%s-%02d-%02d.12:42:20'%(y, m, d))
155 def testIntervalInit(self):
156 ae = self.assertEqual
157 ae(str(Interval('3y')), '+ 3y')
158 ae(str(Interval('2 y 1 m')), '+ 2y 1m')
159 ae(str(Interval('1m 25d')), '+ 1m 25d')
160 ae(str(Interval('-2w 3 d ')), '- 17d')
161 ae(str(Interval(' - 1 d 2:50 ')), '- 1d 2:50')
162 ae(str(Interval(' 14:00 ')), '+ 14:00')
163 ae(str(Interval(' 0:04:33 ')), '+ 0:04:33')
165 def testIntervalAdd(self):
166 ae = self.assertEqual
167 ae(str(Interval('1y') + Interval('1y')), '+ 2y')
168 ae(str(Interval('1y') + Interval('1m')), '+ 1y 1m')
169 ae(str(Interval('1y') + Interval('2:40')), '+ 1y 2:40')
170 ae(str(Interval('1y') + Interval('- 1y')), '')
171 ae(str(Interval('- 1y') + Interval('1y')), '')
172 ae(str(Interval('- 1y') + Interval('- 1y')), '- 2y')
173 ae(str(Interval('1y') + Interval('- 1m')), '+ 11m')
174 ae(str(Interval('1:00') + Interval('1:00')), '+ 2:00')
175 ae(str(Interval('0:50') + Interval('0:50')), '+ 1:40')
176 ae(str(Interval('1:50') + Interval('- 1:50')), '')
177 ae(str(Interval('- 1:50') + Interval('1:50')), '')
178 ae(str(Interval('- 1:50') + Interval('- 1:50')), '- 3:40')
179 ae(str(Interval('1:59:59') + Interval('00:00:01')), '+ 2:00')
180 ae(str(Interval('2:00') + Interval('- 00:00:01')), '+ 1:59:59')
182 def testIntervalSub(self):
183 ae = self.assertEqual
184 ae(str(Interval('1y') - Interval('- 1y')), '+ 2y')
185 ae(str(Interval('1y') - Interval('- 1m')), '+ 1y 1m')
186 ae(str(Interval('1y') - Interval('- 2:40')), '+ 1y 2:40')
187 ae(str(Interval('1y') - Interval('1y')), '')
188 ae(str(Interval('1y') - Interval('1m')), '+ 11m')
189 ae(str(Interval('1:00') - Interval('- 1:00')), '+ 2:00')
190 ae(str(Interval('0:50') - Interval('- 0:50')), '+ 1:40')
191 ae(str(Interval('1:50') - Interval('1:50')), '')
192 ae(str(Interval('1:59:59') - Interval('- 00:00:01')), '+ 2:00')
193 ae(str(Interval('2:00') - Interval('00:00:01')), '+ 1:59:59')
195 def testOverflow(self):
196 ae = self.assertEqual
197 ae(fixTimeOverflow((1,0,0,0, 0, 0, 60)), (1,0,0,0, 0, 1, 0))
198 ae(fixTimeOverflow((1,0,0,0, 0, 0, 100)), (1,0,0,0, 0, 1, 40))
199 ae(fixTimeOverflow((1,0,0,0, 0, 0, 60*60)), (1,0,0,0, 1, 0, 0))
200 ae(fixTimeOverflow((1,0,0,0, 0, 0, 24*60*60)), (1,0,0,1, 0, 0, 0))
201 ae(fixTimeOverflow((1,0,0,0, 0, 0, -1)), (-1,0,0,0, 0, 0, 1))
202 ae(fixTimeOverflow((1,0,0,0, 0, 0, -100)), (-1,0,0,0, 0, 1, 40))
203 ae(fixTimeOverflow((1,0,0,0, 0, 0, -60*60)), (-1,0,0,0, 1, 0, 0))
204 ae(fixTimeOverflow((1,0,0,0, 0, 0, -24*60*60)), (-1,0,0,1, 0, 0, 0))
205 ae(fixTimeOverflow((-1,0,0,0, 0, 0, 1)), (-1,0,0,0, 0, 0, 1))
206 ae(fixTimeOverflow((-1,0,0,0, 0, 0, 100)), (-1,0,0,0, 0, 1, 40))
207 ae(fixTimeOverflow((-1,0,0,0, 0, 0, 60*60)), (-1,0,0,0, 1, 0, 0))
208 ae(fixTimeOverflow((-1,0,0,0, 0, 0, 24*60*60)), (-1,0,0,1, 0, 0, 0))
210 def testDivision(self):
211 ae = self.assertEqual
212 ae(str(Interval('1y')/2), '+ 6m')
213 ae(str(Interval('1:00')/2), '+ 0:30')
214 ae(str(Interval('00:01')/2), '+ 0:00:30')
216 def testSorting(self):
217 ae = self.assertEqual
218 i1 = Interval('1y')
219 i2 = Interval('1d')
220 l = [i1, i2]; l.sort()
221 ae(l, [i2, i1])
222 l = [i2, i1]; l.sort()
223 ae(l, [i2, i1])
224 i1 = Interval('- 2d')
225 i2 = Interval('1d')
226 l = [i1, i2]; l.sort()
227 ae(l, [i1, i2])
229 i1 = Interval("1:20")
230 i2 = Interval("2d")
231 i3 = Interval("3:30")
232 l = [i1, i2, i3]; l.sort()
233 ae(l, [i1, i3, i2])
235 def suite():
236 return unittest.makeSuite(DateTestCase, 'test')
239 # vim: set filetype=python ts=4 sw=4 et si