Code

more lenient date input and addition Interval input support (sf bug 677764)
[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.22 2003-03-19 05:18:11 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))
60     def testDateError(self):
61         self.assertRaises(ValueError, Date, "12")
63     def testOffset(self):
64         ae = self.assertEqual
65         date = Date("2000-04-17", -5)
66         ae(str(date), '2000-04-17.05:00:00')
67         date = Date("01-25", -5)
68         y, m, d, x, x, x, x, x, x = time.gmtime(time.time())
69         ae(str(date), '%s-01-25.05:00:00'%y)
70         date = Date("2000-04-17.03:45", -5)
71         ae(str(date), '2000-04-17.08:45:00')
72         date = Date("08-13.22:13", -5)
73         ae(str(date), '%s-08-14.03:13:00'%y)
74         date = Date("11-07.09:32:43", -5)
75         ae(str(date), '%s-11-07.14:32:43'%y)
76         date = Date("14:25", -5)
77         ae(str(date), '%s-%02d-%02d.19:25:00'%(y, m, d))
78         date = Date("8:47:11", -5)
79         ae(str(date), '%s-%02d-%02d.13:47:11'%(y, m, d))
81     def testOffsetRandom(self):
82         ae = self.assertEqual
83         # XXX unsure of the usefulness of these, they're pretty random
84         date = Date('2000-01-01') + Interval('- 2y 2m')
85         ae(str(date), '1997-11-01.00:00:00')
86         date = Date('2000-01-01 - 2y 2m')
87         ae(str(date), '1997-11-01.00:00:00')
88         date = Date('2000-01-01') + Interval('2m')
89         ae(str(date), '2000-03-01.00:00:00')
90         date = Date('2000-01-01 + 2m')
91         ae(str(date), '2000-03-01.00:00:00')
93         date = Date('2000-01-01') + Interval('60d')
94         ae(str(date), '2000-03-01.00:00:00')
95         date = Date('2001-01-01') + Interval('60d')
96         ae(str(date), '2001-03-02.00:00:00')
98     def testOffsetAdd(self):
99         ae = self.assertEqual
100         date = Date('2000-02-28.23:59:59') + Interval('00:00:01')
101         ae(str(date), '2000-02-29.00:00:00')
102         date = Date('2001-02-28.23:59:59') + Interval('00:00:01')
103         ae(str(date), '2001-03-01.00:00:00')
105         date = Date('2000-02-28.23:58:59') + Interval('00:01:01')
106         ae(str(date), '2000-02-29.00:00:00')
107         date = Date('2001-02-28.23:58:59') + Interval('00:01:01')
108         ae(str(date), '2001-03-01.00:00:00')
110         date = Date('2000-02-28.22:58:59') + Interval('01:01:01')
111         ae(str(date), '2000-02-29.00:00:00')
112         date = Date('2001-02-28.22:58:59') + Interval('01:01:01')
113         ae(str(date), '2001-03-01.00:00:00')
115         date = Date('2000-02-28.22:58:59') + Interval('00:00:3661')
116         ae(str(date), '2000-02-29.00:00:00')
117         date = Date('2001-02-28.22:58:59') + Interval('00:00:3661')
118         ae(str(date), '2001-03-01.00:00:00')
120     def testOffsetSub(self):
121         ae = self.assertEqual
122         date = Date('2000-01-01') - Interval('- 2y 2m')
123         ae(str(date), '2002-03-01.00:00:00')
124         date = Date('2000-01-01') - Interval('2m')
125         ae(str(date), '1999-11-01.00:00:00')
127         date = Date('2000-03-01') - Interval('60d')
128         ae(str(date), '2000-01-01.00:00:00')
129         date = Date('2001-03-02') - Interval('60d')
130         ae(str(date), '2001-01-01.00:00:00')
132         date = Date('2000-02-29.00:00:00') - Interval('00:00:01')
133         ae(str(date), '2000-02-28.23:59:59')
134         date = Date('2001-03-01.00:00:00') - Interval('00:00:01')
135         ae(str(date), '2001-02-28.23:59:59')
137         date = Date('2000-02-29.00:00:00') - Interval('00:01:01')
138         ae(str(date), '2000-02-28.23:58:59')
139         date = Date('2001-03-01.00:00:00') - Interval('00:01:01')
140         ae(str(date), '2001-02-28.23:58:59')
142         date = Date('2000-02-29.00:00:00') - Interval('01:01:01')
143         ae(str(date), '2000-02-28.22:58:59')
144         date = Date('2001-03-01.00:00:00') - Interval('01:01:01')
145         ae(str(date), '2001-02-28.22:58:59')
147         date = Date('2000-02-29.00:00:00') - Interval('00:00:3661')
148         ae(str(date), '2000-02-28.22:58:59')
149         date = Date('2001-03-01.00:00:00') - Interval('00:00:3661')
150         ae(str(date), '2001-02-28.22:58:59')
152     def testDateLocal(self):
153         ae = self.assertEqual
154         date = Date("02:42:20")
155         date = date.local(10)
156         y, m, d, x, x, x, x, x, x = time.gmtime(time.time())
157         ae(str(date), '%s-%02d-%02d.12:42:20'%(y, m, d))
159     def testIntervalInit(self):
160         ae = self.assertEqual
161         ae(str(Interval('3y')), '+ 3y')
162         ae(str(Interval('2 y 1 m')), '+ 2y 1m')
163         ae(str(Interval('1m 25d')), '+ 1m 25d')
164         ae(str(Interval('-2w 3 d ')), '- 17d')
165         ae(str(Interval(' - 1 d 2:50 ')), '- 1d 2:50')
166         ae(str(Interval(' 14:00 ')), '+ 14:00')
167         ae(str(Interval(' 0:04:33 ')), '+ 0:04:33')
169     def testIntervalInitDate(self):
170         ae = self.assertEqual
171         now = Date('.')
172         now.hour = now.minute = now.second = 0
173         then = now + Interval('2d')
174         ae(str(Interval(str(then))), '+ 2d')
175         then = now - Interval('2d')
176         ae(str(Interval(str(then))), '- 2d')
178     def testIntervalAdd(self):
179         ae = self.assertEqual
180         ae(str(Interval('1y') + Interval('1y')), '+ 2y')
181         ae(str(Interval('1y') + Interval('1m')), '+ 1y 1m')
182         ae(str(Interval('1y') + Interval('2:40')), '+ 1y 2:40')
183         ae(str(Interval('1y') + Interval('- 1y')), '')
184         ae(str(Interval('- 1y') + Interval('1y')), '')
185         ae(str(Interval('- 1y') + Interval('- 1y')), '- 2y')
186         ae(str(Interval('1y') + Interval('- 1m')), '+ 11m')
187         ae(str(Interval('1:00') + Interval('1:00')), '+ 2:00')
188         ae(str(Interval('0:50') + Interval('0:50')), '+ 1:40')
189         ae(str(Interval('1:50') + Interval('- 1:50')), '')
190         ae(str(Interval('- 1:50') + Interval('1:50')), '')
191         ae(str(Interval('- 1:50') + Interval('- 1:50')), '- 3:40')
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 testIntervalSub(self):
196         ae = self.assertEqual
197         ae(str(Interval('1y') - Interval('- 1y')), '+ 2y')
198         ae(str(Interval('1y') - Interval('- 1m')), '+ 1y 1m')
199         ae(str(Interval('1y') - Interval('- 2:40')), '+ 1y 2:40')
200         ae(str(Interval('1y') - Interval('1y')), '')
201         ae(str(Interval('1y') - Interval('1m')), '+ 11m')
202         ae(str(Interval('1:00') - Interval('- 1:00')), '+ 2:00')
203         ae(str(Interval('0:50') - Interval('- 0:50')), '+ 1:40')
204         ae(str(Interval('1:50') - Interval('1:50')), '')
205         ae(str(Interval('1:59:59') - Interval('- 00:00:01')), '+ 2:00')
206         ae(str(Interval('2:00') - Interval('00:00:01')), '+ 1:59:59')
208     def testOverflow(self):
209         ae = self.assertEqual
210         ae(fixTimeOverflow((1,0,0,0, 0, 0, 60)), (1,0,0,0, 0, 1, 0))
211         ae(fixTimeOverflow((1,0,0,0, 0, 0, 100)), (1,0,0,0, 0, 1, 40))
212         ae(fixTimeOverflow((1,0,0,0, 0, 0, 60*60)), (1,0,0,0, 1, 0, 0))
213         ae(fixTimeOverflow((1,0,0,0, 0, 0, 24*60*60)), (1,0,0,1, 0, 0, 0))
214         ae(fixTimeOverflow((1,0,0,0, 0, 0, -1)), (-1,0,0,0, 0, 0, 1))
215         ae(fixTimeOverflow((1,0,0,0, 0, 0, -100)), (-1,0,0,0, 0, 1, 40))
216         ae(fixTimeOverflow((1,0,0,0, 0, 0, -60*60)), (-1,0,0,0, 1, 0, 0))
217         ae(fixTimeOverflow((1,0,0,0, 0, 0, -24*60*60)), (-1,0,0,1, 0, 0, 0))
218         ae(fixTimeOverflow((-1,0,0,0, 0, 0, 1)), (-1,0,0,0, 0, 0, 1))
219         ae(fixTimeOverflow((-1,0,0,0, 0, 0, 100)), (-1,0,0,0, 0, 1, 40))
220         ae(fixTimeOverflow((-1,0,0,0, 0, 0, 60*60)), (-1,0,0,0, 1, 0, 0))
221         ae(fixTimeOverflow((-1,0,0,0, 0, 0, 24*60*60)), (-1,0,0,1, 0, 0, 0))
223     def testDivision(self):
224         ae = self.assertEqual
225         ae(str(Interval('1y')/2), '+ 6m')
226         ae(str(Interval('1:00')/2), '+ 0:30')
227         ae(str(Interval('00:01')/2), '+ 0:00:30')
229     def testSorting(self):
230         ae = self.assertEqual
231         i1 = Interval('1y')
232         i2 = Interval('1d')
233         l = [i1, i2]; l.sort()
234         ae(l, [i2, i1])
235         l = [i2, i1]; l.sort()
236         ae(l, [i2, i1])
237         i1 = Interval('- 2d')
238         i2 = Interval('1d')
239         l = [i1, i2]; l.sort()
240         ae(l, [i1, i2])
242         i1 = Interval("1:20")
243         i2 = Interval("2d")
244         i3 = Interval("3:30")
245         l = [i1, i2, i3]; l.sort()
246         ae(l, [i1, i3, i2])
248 def suite():
249    return unittest.makeSuite(DateTestCase, 'test')
252 # vim: set filetype=python ts=4 sw=4 et si