Code

bf594e8c5fdb2dccfbf0f2dd2f1cfa49160d43e4
[roundup.git] / test / test_dates.py
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.26 2003-11-02 09:27:50 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/04/17")
39         ae(str(date), '2000-04-17.00:00:00')
40         date = Date("2000-4-7")
41         ae(str(date), '2000-04-07.00:00:00')
42         date = Date("2000-4-17")
43         ae(str(date), '2000-04-17.00:00:00')
44         date = Date("01-25")
45         y, m, d, x, x, x, x, x, x = time.gmtime(time.time())
46         ae(str(date), '%s-01-25.00:00:00'%y)
47         date = Date("2000-04-17.03:45")
48         ae(str(date), '2000-04-17.03:45:00')
49         date = Date("2000/04/17.03:45")
50         ae(str(date), '2000-04-17.03:45:00')
51         date = Date("08-13.22:13")
52         ae(str(date), '%s-08-13.22:13:00'%y)
53         date = Date("11-07.09:32:43")
54         ae(str(date), '%s-11-07.09:32:43'%y)
55         date = Date("14:25")
56         ae(str(date), '%s-%02d-%02d.14:25:00'%(y, m, d))
57         date = Date("8:47:11")
58         ae(str(date), '%s-%02d-%02d.08:47:11'%(y, m, d))
59         ae(str(Date('2003')), '2003-01-01.00:00:00')
60         ae(str(Date('2004-06')), '2004-06-01.00:00:00')
62     def testDateError(self):
63         self.assertRaises(ValueError, Date, "12")
65     def testOffset(self):
66         ae = self.assertEqual
67         date = Date("2000-04-17", -5)
68         ae(str(date), '2000-04-17.05:00:00')
69         date = Date("01-25", -5)
70         y, m, d, x, x, x, x, x, x = time.gmtime(time.time())
71         ae(str(date), '%s-01-25.05:00:00'%y)
72         date = Date("2000-04-17.03:45", -5)
73         ae(str(date), '2000-04-17.08:45:00')
74         date = Date("08-13.22:13", -5)
75         ae(str(date), '%s-08-14.03:13:00'%y)
76         date = Date("11-07.09:32:43", -5)
77         ae(str(date), '%s-11-07.14:32:43'%y)
78         date = Date("14:25", -5)
79         ae(str(date), '%s-%02d-%02d.19:25:00'%(y, m, d))
80         date = Date("8:47:11", -5)
81         ae(str(date), '%s-%02d-%02d.13:47:11'%(y, m, d))
83     def testOffsetRandom(self):
84         ae = self.assertEqual
85         # XXX unsure of the usefulness of these, they're pretty random
86         date = Date('2000-01-01') + Interval('- 2y 2m')
87         ae(str(date), '1997-11-01.00:00:00')
88         date = Date('2000-01-01 - 2y 2m')
89         ae(str(date), '1997-11-01.00:00:00')
90         date = Date('2000-01-01') + Interval('2m')
91         ae(str(date), '2000-03-01.00:00:00')
92         date = Date('2000-01-01 + 2m')
93         ae(str(date), '2000-03-01.00:00:00')
95         date = Date('2000-01-01') + Interval('60d')
96         ae(str(date), '2000-03-01.00:00:00')
97         date = Date('2001-01-01') + Interval('60d')
98         ae(str(date), '2001-03-02.00:00:00')
100     def testOffsetAdd(self):
101         ae = self.assertEqual
102         date = Date('2000-02-28.23:59:59') + Interval('00:00:01')
103         ae(str(date), '2000-02-29.00:00:00')
104         date = Date('2001-02-28.23:59:59') + Interval('00:00:01')
105         ae(str(date), '2001-03-01.00:00:00')
107         date = Date('2000-02-28.23:58:59') + Interval('00:01:01')
108         ae(str(date), '2000-02-29.00:00:00')
109         date = Date('2001-02-28.23:58:59') + Interval('00:01:01')
110         ae(str(date), '2001-03-01.00:00:00')
112         date = Date('2000-02-28.22:58:59') + Interval('01:01:01')
113         ae(str(date), '2000-02-29.00:00:00')
114         date = Date('2001-02-28.22:58:59') + Interval('01:01:01')
115         ae(str(date), '2001-03-01.00:00:00')
117         date = Date('2000-02-28.22:58:59') + Interval('00:00:3661')
118         ae(str(date), '2000-02-29.00:00:00')
119         date = Date('2001-02-28.22:58:59') + Interval('00:00:3661')
120         ae(str(date), '2001-03-01.00:00:00')
122     def testOffsetSub(self):
123         ae = self.assertEqual
124         date = Date('2000-01-01') - Interval('- 2y 2m')
125         ae(str(date), '2002-03-01.00:00:00')
126         date = Date('2000-01-01') - Interval('2m')
127         ae(str(date), '1999-11-01.00:00:00')
129         date = Date('2000-03-01') - Interval('60d')
130         ae(str(date), '2000-01-01.00:00:00')
131         date = Date('2001-03-02') - Interval('60d')
132         ae(str(date), '2001-01-01.00:00:00')
134         date = Date('2000-02-29.00:00:00') - Interval('00:00:01')
135         ae(str(date), '2000-02-28.23:59:59')
136         date = Date('2001-03-01.00:00:00') - Interval('00:00:01')
137         ae(str(date), '2001-02-28.23:59:59')
139         date = Date('2000-02-29.00:00:00') - Interval('00:01:01')
140         ae(str(date), '2000-02-28.23:58:59')
141         date = Date('2001-03-01.00:00:00') - Interval('00:01:01')
142         ae(str(date), '2001-02-28.23:58:59')
144         date = Date('2000-02-29.00:00:00') - Interval('01:01:01')
145         ae(str(date), '2000-02-28.22:58:59')
146         date = Date('2001-03-01.00:00:00') - Interval('01:01:01')
147         ae(str(date), '2001-02-28.22:58:59')
149         date = Date('2000-02-29.00:00:00') - Interval('00:00:3661')
150         ae(str(date), '2000-02-28.22:58:59')
151         date = Date('2001-03-01.00:00:00') - Interval('00:00:3661')
152         ae(str(date), '2001-02-28.22:58:59')
154     def testDateLocal(self):
155         ae = self.assertEqual
156         date = Date("02:42:20")
157         date = date.local(10)
158         y, m, d, x, x, x, x, x, x = time.gmtime(time.time())
159         ae(str(date), '%s-%02d-%02d.12:42:20'%(y, m, d))
161     def testIntervalInit(self):
162         ae = self.assertEqual
163         ae(str(Interval('3y')), '+ 3y')
164         ae(str(Interval('2 y 1 m')), '+ 2y 1m')
165         ae(str(Interval('1m 25d')), '+ 1m 25d')
166         ae(str(Interval('-2w 3 d ')), '- 17d')
167         ae(str(Interval(' - 1 d 2:50 ')), '- 1d 2:50')
168         ae(str(Interval(' 14:00 ')), '+ 14:00')
169         ae(str(Interval(' 0:04:33 ')), '+ 0:04:33')
171     def testIntervalInitDate(self):
172         ae = self.assertEqual
173         now = Date('.')
174         now.hour = now.minute = now.second = 0
175         then = now + Interval('2d')
176         ae(str(Interval(str(then))), '+ 2d')
177         then = now - Interval('2d')
178         ae(str(Interval(str(then))), '- 2d')
180     def testIntervalAddMonthBoundary(self):
181         # force the transition over a month boundary
182         now = Date('2003-10-30.00:00:00')
183         then = now + Interval('2d')
184         self.assertEqual(str(then), '2003-11-01.00:00:00')
185         now = Date('2004-02-28.00:00:00')
186         then = now + Interval('1d')
187         self.assertEqual(str(then), '2004-02-29.00:00:00')
188         now = Date('2003-02-28.00:00:00')
189         then = now + Interval('1d')
190         self.assertEqual(str(then), '2003-03-01.00:00:00')
191         now = Date('2003-01-01.00:00:00')
192         then = now + Interval('59d')
193         self.assertEqual(str(then), '2003-03-01.00:00:00')
194         now = Date('2004-01-01.00:00:00')
195         then = now + Interval('59d')
196         self.assertEqual(str(then), '2004-02-29.00:00:00')
198     def testIntervalSubtractMonthBoundary(self):
199         # force the transition over a month boundary
200         now = Date('2003-11-01.00:00:00')
201         then = now - Interval('2d')
202         self.assertEqual(str(then), '2003-10-30.00:00:00')
203         now = Date('2004-02-29.00:00:00')
204         then = now - Interval('1d')
205         self.assertEqual(str(then), '2004-02-28.00:00:00')
206         now = Date('2003-03-01.00:00:00')
207         then = now - Interval('1d')
208         self.assertEqual(str(then), '2003-02-08.00:00:00')
209         now = Date('2003-03-01.00:00:00')
210         then = now - Interval('59d')
211         self.assertEqual(str(then), '2003-01-01.00:00:00')
212         now = Date('2004-02-29.00:00:00')
213         then = now - Interval('59d')
214         self.assertEqual(str(then), '2004-01-01.00:00:00')
216     def testIntervalAddYearBoundary(self):
217         # force the transition over a year boundary
218         now = Date('2003-12-30.00:00:00')
219         then = now + Interval('2d')
220         self.assertEqual(str(then), '2004-01-01.00:00:00')
221         now = Date('2003-01-01.00:00:00')
222         then = now + Interval('364d')
223         self.assertEqual(str(then), '2004-01-01.00:00:00')
224         now = Date('2004-01-01.00:00:00')
225         then = now + Interval('365d')
226         self.assertEqual(str(then), '2005-01-01.00:00:00')
228     def testIntervalSubtractYearBoundary(self):
229         # force the transition over a year boundary
230         now = Date('2003-01-01.00:00:00')
231         then = now - Interval('2d')
232         self.assertEqual(str(then), '2002-12-30.00:00:00')
233         now = Date('2004-02-01.00:00:00')
234         then = now - Interval('365d')
235         self.assertEqual(str(then), '2003-02-01.00:00:00')
236         now = Date('2005-02-01.00:00:00')
237         then = now - Interval('365d')
238         self.assertEqual(str(then), '2004-02-02.00:00:00')
240     def testDateSubtract(self):
241         # force the transition over a year boundary
242         i = Date('2003-01-01.00:00:00') - Date('2002-01-01.00:00:00')
243         self.assertEqual(str(i).strip(), '1y')
245     def testIntervalAdd(self):
246         ae = self.assertEqual
247         ae(str(Interval('1y') + Interval('1y')), '+ 2y')
248         ae(str(Interval('1y') + Interval('1m')), '+ 1y 1m')
249         ae(str(Interval('1y') + Interval('2:40')), '+ 1y 2:40')
250         ae(str(Interval('1y') + Interval('- 1y')), '')
251         ae(str(Interval('- 1y') + Interval('1y')), '')
252         ae(str(Interval('- 1y') + Interval('- 1y')), '- 2y')
253         ae(str(Interval('1y') + Interval('- 1m')), '+ 11m')
254         ae(str(Interval('1:00') + Interval('1:00')), '+ 2:00')
255         ae(str(Interval('0:50') + Interval('0:50')), '+ 1:40')
256         ae(str(Interval('1:50') + Interval('- 1:50')), '')
257         ae(str(Interval('- 1:50') + Interval('1:50')), '')
258         ae(str(Interval('- 1:50') + Interval('- 1:50')), '- 3:40')
259         ae(str(Interval('1:59:59') + Interval('00:00:01')), '+ 2:00')
260         ae(str(Interval('2:00') + Interval('- 00:00:01')), '+ 1:59:59')
262     def testIntervalSub(self):
263         ae = self.assertEqual
264         ae(str(Interval('1y') - Interval('- 1y')), '+ 2y')
265         ae(str(Interval('1y') - Interval('- 1m')), '+ 1y 1m')
266         ae(str(Interval('1y') - Interval('- 2:40')), '+ 1y 2:40')
267         ae(str(Interval('1y') - Interval('1y')), '')
268         ae(str(Interval('1y') - Interval('1m')), '+ 11m')
269         ae(str(Interval('1:00') - Interval('- 1:00')), '+ 2:00')
270         ae(str(Interval('0:50') - Interval('- 0:50')), '+ 1:40')
271         ae(str(Interval('1:50') - Interval('1:50')), '')
272         ae(str(Interval('1:59:59') - Interval('- 00:00:01')), '+ 2:00')
273         ae(str(Interval('2:00') - Interval('00:00:01')), '+ 1:59:59')
275     def testOverflow(self):
276         ae = self.assertEqual
277         ae(fixTimeOverflow((1,0,0,0, 0, 0, 60)), (1,0,0,0, 0, 1, 0))
278         ae(fixTimeOverflow((1,0,0,0, 0, 0, 100)), (1,0,0,0, 0, 1, 40))
279         ae(fixTimeOverflow((1,0,0,0, 0, 0, 60*60)), (1,0,0,0, 1, 0, 0))
280         ae(fixTimeOverflow((1,0,0,0, 0, 0, 24*60*60)), (1,0,0,1, 0, 0, 0))
281         ae(fixTimeOverflow((1,0,0,0, 0, 0, -1)), (-1,0,0,0, 0, 0, 1))
282         ae(fixTimeOverflow((1,0,0,0, 0, 0, -100)), (-1,0,0,0, 0, 1, 40))
283         ae(fixTimeOverflow((1,0,0,0, 0, 0, -60*60)), (-1,0,0,0, 1, 0, 0))
284         ae(fixTimeOverflow((1,0,0,0, 0, 0, -24*60*60)), (-1,0,0,1, 0, 0, 0))
285         ae(fixTimeOverflow((-1,0,0,0, 0, 0, 1)), (-1,0,0,0, 0, 0, 1))
286         ae(fixTimeOverflow((-1,0,0,0, 0, 0, 100)), (-1,0,0,0, 0, 1, 40))
287         ae(fixTimeOverflow((-1,0,0,0, 0, 0, 60*60)), (-1,0,0,0, 1, 0, 0))
288         ae(fixTimeOverflow((-1,0,0,0, 0, 0, 24*60*60)), (-1,0,0,1, 0, 0, 0))
290     def testDivision(self):
291         ae = self.assertEqual
292         ae(str(Interval('1y')/2), '+ 6m')
293         ae(str(Interval('1:00')/2), '+ 0:30')
294         ae(str(Interval('00:01')/2), '+ 0:00:30')
296     def testSorting(self):
297         ae = self.assertEqual
298         i1 = Interval('1y')
299         i2 = Interval('1d')
300         l = [i1, i2]; l.sort()
301         ae(l, [i2, i1])
302         l = [i2, i1]; l.sort()
303         ae(l, [i2, i1])
304         i1 = Interval('- 2d')
305         i2 = Interval('1d')
306         l = [i1, i2]; l.sort()
307         ae(l, [i1, i2])
309         i1 = Interval("1:20")
310         i2 = Interval("2d")
311         i3 = Interval("3:30")
312         l = [i1, i2, i3]; l.sort()
313         ae(l, [i1, i3, i2])
315     def testGranularity(self):
316         ae = self.assertEqual
317         ae(str(Date('2003-2-12', add_granularity=1)), '2003-02-12.23:59:59')
318         ae(str(Date('2003-1-1.23:00', add_granularity=1)), '2003-01-01.23:00:59')
319         ae(str(Date('2003', add_granularity=1)), '2003-12-31.23:59:59')
320         ae(str(Date('2003-5', add_granularity=1)), '2003-05-31.23:59:59')
321         ae(str(Interval('+1w', add_granularity=1)), '+ 14d')
322         ae(str(Interval('-2m 3w', add_granularity=1)), '- 2m 14d')
324 def test_suite():
325     suite = unittest.TestSuite()
326     suite.addTest(unittest.makeSuite(DateTestCase))
327     return suite
329 if __name__ == '__main__':
330     runner = unittest.TextTestRunner()
331     unittest.main(testRunner=runner)
333 # vim: set filetype=python ts=4 sw=4 et si