Code

fix to sf bug 691071, really this time
[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.20 2003-03-10 00:22:21 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 testOffset(self):
57         ae = self.assertEqual
58         date = Date("2000-04-17", -5)
59         ae(str(date), '2000-04-17.05:00:00')
60         date = Date("01-25", -5)
61         y, m, d, x, x, x, x, x, x = time.gmtime(time.time())
62         ae(str(date), '%s-01-25.05:00:00'%y)
63         date = Date("2000-04-17.03:45", -5)
64         ae(str(date), '2000-04-17.08:45:00')
65         date = Date("08-13.22:13", -5)
66         ae(str(date), '%s-08-14.03:13:00'%y)
67         date = Date("11-07.09:32:43", -5)
68         ae(str(date), '%s-11-07.14:32:43'%y)
69         date = Date("14:25", -5)
70         ae(str(date), '%s-%02d-%02d.19:25:00'%(y, m, d))
71         date = Date("8:47:11", -5)
72         ae(str(date), '%s-%02d-%02d.13:47:11'%(y, m, d))
74     def testOffsetRandom(self):
75         ae = self.assertEqual
76         # XXX unsure of the usefulness of these, they're pretty random
77         date = Date('2000-01-01') + Interval('- 2y 2m')
78         ae(str(date), '1997-11-01.00:00:00')
79         date = Date('2000-01-01 - 2y 2m')
80         ae(str(date), '1997-11-01.00:00:00')
81         date = Date('2000-01-01') + Interval('2m')
82         ae(str(date), '2000-03-01.00:00:00')
83         date = Date('2000-01-01 + 2m')
84         ae(str(date), '2000-03-01.00:00:00')
86         date = Date('2000-01-01') + Interval('60d')
87         ae(str(date), '2000-03-01.00:00:00')
88         date = Date('2001-01-01') + Interval('60d')
89         ae(str(date), '2001-03-02.00:00:00')
91     def testOffsetAdd(self):
92         ae = self.assertEqual
93         date = Date('2000-02-28.23:59:59') + Interval('00:00:01')
94         ae(str(date), '2000-02-29.00:00:00')
95         date = Date('2001-02-28.23:59:59') + Interval('00:00:01')
96         ae(str(date), '2001-03-01.00:00:00')
98         date = Date('2000-02-28.23:58:59') + Interval('00:01:01')
99         ae(str(date), '2000-02-29.00:00:00')
100         date = Date('2001-02-28.23:58:59') + Interval('00:01:01')
101         ae(str(date), '2001-03-01.00:00:00')
103         date = Date('2000-02-28.22:58:59') + Interval('01:01:01')
104         ae(str(date), '2000-02-29.00:00:00')
105         date = Date('2001-02-28.22:58:59') + Interval('01:01:01')
106         ae(str(date), '2001-03-01.00:00:00')
108         date = Date('2000-02-28.22:58:59') + Interval('00:00:3661')
109         ae(str(date), '2000-02-29.00:00:00')
110         date = Date('2001-02-28.22:58:59') + Interval('00:00:3661')
111         ae(str(date), '2001-03-01.00:00:00')
113     def testOffsetSub(self):
114         ae = self.assertEqual
115         date = Date('2000-01-01') - Interval('- 2y 2m')
116         ae(str(date), '2002-03-01.00:00:00')
117         date = Date('2000-01-01') - Interval('2m')
118         ae(str(date), '1999-11-01.00:00:00')
120         date = Date('2000-03-01') - Interval('60d')
121         ae(str(date), '2000-01-01.00:00:00')
122         date = Date('2001-03-02') - Interval('60d')
123         ae(str(date), '2001-01-01.00:00:00')
125         date = Date('2000-02-29.00:00:00') - Interval('00:00:01')
126         ae(str(date), '2000-02-28.23:59:59')
127         date = Date('2001-03-01.00:00:00') - Interval('00:00:01')
128         ae(str(date), '2001-02-28.23:59:59')
130         date = Date('2000-02-29.00:00:00') - Interval('00:01:01')
131         ae(str(date), '2000-02-28.23:58:59')
132         date = Date('2001-03-01.00:00:00') - Interval('00:01:01')
133         ae(str(date), '2001-02-28.23:58:59')
135         date = Date('2000-02-29.00:00:00') - Interval('01:01:01')
136         ae(str(date), '2000-02-28.22:58:59')
137         date = Date('2001-03-01.00:00:00') - Interval('01:01:01')
138         ae(str(date), '2001-02-28.22:58:59')
140         date = Date('2000-02-29.00:00:00') - Interval('00:00:3661')
141         ae(str(date), '2000-02-28.22:58:59')
142         date = Date('2001-03-01.00:00:00') - Interval('00:00:3661')
143         ae(str(date), '2001-02-28.22:58:59')
145     def testDateLocal(self):
146         ae = self.assertEqual
147         date = Date("02:42:20")
148         date = date.local(10)
149         y, m, d, x, x, x, x, x, x = time.gmtime(time.time())
150         ae(str(date), '%s-%02d-%02d.12:42:20'%(y, m, d))
152     def testIntervalInit(self):
153         ae = self.assertEqual
154         ae(str(Interval('3y')), '+ 3y')
155         ae(str(Interval('2 y 1 m')), '+ 2y 1m')
156         ae(str(Interval('1m 25d')), '+ 1m 25d')
157         ae(str(Interval('-2w 3 d ')), '- 17d')
158         ae(str(Interval(' - 1 d 2:50 ')), '- 1d 2:50')
159         ae(str(Interval(' 14:00 ')), '+ 14:00')
160         ae(str(Interval(' 0:04:33 ')), '+ 0:04:33')
162     def testIntervalAdd(self):
163         ae = self.assertEqual
164         ae(str(Interval('1y') + Interval('1y')), '+ 2y')
165         ae(str(Interval('1y') + Interval('1m')), '+ 1y 1m')
166         ae(str(Interval('1y') + Interval('2:40')), '+ 1y 2:40')
167         ae(str(Interval('1y') + Interval('- 1y')), '')
168         ae(str(Interval('- 1y') + Interval('1y')), '')
169         ae(str(Interval('- 1y') + Interval('- 1y')), '- 2y')
170         ae(str(Interval('1y') + Interval('- 1m')), '+ 11m')
171         ae(str(Interval('1:00') + Interval('1:00')), '+ 2:00')
172         ae(str(Interval('0:50') + Interval('0:50')), '+ 1:40')
173         ae(str(Interval('1:50') + Interval('- 1:50')), '')
174         ae(str(Interval('- 1:50') + Interval('1:50')), '')
175         ae(str(Interval('- 1:50') + Interval('- 1:50')), '- 3:40')
176         ae(str(Interval('1:59:59') + Interval('00:00:01')), '+ 2:00')
177         ae(str(Interval('2:00') + Interval('- 00:00:01')), '+ 1:59:59')
179     def testIntervalSub(self):
180         ae = self.assertEqual
181         ae(str(Interval('1y') - Interval('- 1y')), '+ 2y')
182         ae(str(Interval('1y') - Interval('- 1m')), '+ 1y 1m')
183         ae(str(Interval('1y') - Interval('- 2:40')), '+ 1y 2:40')
184         ae(str(Interval('1y') - Interval('1y')), '')
185         ae(str(Interval('1y') - Interval('1m')), '+ 11m')
186         ae(str(Interval('1:00') - Interval('- 1:00')), '+ 2:00')
187         ae(str(Interval('0:50') - Interval('- 0:50')), '+ 1:40')
188         ae(str(Interval('1:50') - Interval('1:50')), '')
189         ae(str(Interval('1:59:59') - Interval('- 00:00:01')), '+ 2:00')
190         ae(str(Interval('2:00') - Interval('00:00:01')), '+ 1:59:59')
192     def testOverflow(self):
193         ae = self.assertEqual
194         ae(fixTimeOverflow((1,0,0,0, 0, 0, 60)), (1,0,0,0, 0, 1, 0))
195         ae(fixTimeOverflow((1,0,0,0, 0, 0, 100)), (1,0,0,0, 0, 1, 40))
196         ae(fixTimeOverflow((1,0,0,0, 0, 0, 60*60)), (1,0,0,0, 1, 0, 0))
197         ae(fixTimeOverflow((1,0,0,0, 0, 0, 24*60*60)), (1,0,0,1, 0, 0, 0))
198         ae(fixTimeOverflow((1,0,0,0, 0, 0, -1)), (-1,0,0,0, 0, 0, 1))
199         ae(fixTimeOverflow((1,0,0,0, 0, 0, -100)), (-1,0,0,0, 0, 1, 40))
200         ae(fixTimeOverflow((1,0,0,0, 0, 0, -60*60)), (-1,0,0,0, 1, 0, 0))
201         ae(fixTimeOverflow((1,0,0,0, 0, 0, -24*60*60)), (-1,0,0,1, 0, 0, 0))
202         ae(fixTimeOverflow((-1,0,0,0, 0, 0, 1)), (-1,0,0,0, 0, 0, 1))
203         ae(fixTimeOverflow((-1,0,0,0, 0, 0, 100)), (-1,0,0,0, 0, 1, 40))
204         ae(fixTimeOverflow((-1,0,0,0, 0, 0, 60*60)), (-1,0,0,0, 1, 0, 0))
205         ae(fixTimeOverflow((-1,0,0,0, 0, 0, 24*60*60)), (-1,0,0,1, 0, 0, 0))
207     def testDivision(self):
208         ae = self.assertEqual
209         ae(str(Interval('1y')/2), '+ 6m')
210         ae(str(Interval('1:00')/2), '+ 0:30')
211         ae(str(Interval('00:01')/2), '+ 0:00:30')
213     def testSorting(self):
214         ae = self.assertEqual
215         i1 = Interval('1y')
216         i2 = Interval('1d')
217         l = [i1, i2]; l.sort()
218         ae(l, [i2, i1])
219         l = [i2, i1]; l.sort()
220         ae(l, [i2, i1])
221         i1 = Interval('- 2d')
222         i2 = Interval('1d')
223         l = [i1, i2]; l.sort()
224         ae(l, [i1, i2])
226         i1 = Interval("1:20")
227         i2 = Interval("2d")
228         i3 = Interval("3:30")
229         l = [i1, i2, i3]; l.sort()
230         ae(l, [i1, i3, i2])
232 def suite():
233    return unittest.makeSuite(DateTestCase, 'test')
236 # vim: set filetype=python ts=4 sw=4 et si