Mercurial > p > roundup > code
comparison test/test_dates.py @ 1487:37a1906f4454
fixed Interval maths [SF#665357]
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Thu, 06 Mar 2003 02:33:57 +0000 |
| parents | 0c3546479b52 |
| children | 66cc5c2819dd |
comparison
equal
deleted
inserted
replaced
| 1486:f5f60c75a458 | 1487:37a1906f4454 |
|---|---|
| 13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 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" | 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, | 15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
| 16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | 16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 17 # | 17 # |
| 18 # $Id: test_dates.py,v 1.17 2003-02-24 15:38:51 kedder Exp $ | 18 # $Id: test_dates.py,v 1.18 2003-03-06 02:33:57 richard Exp $ |
| 19 | 19 |
| 20 import unittest, time | 20 import unittest, time |
| 21 | 21 |
| 22 from roundup.date import Date, Interval | 22 from roundup.date import Date, Interval, fixTimeOverflow |
| 23 | 23 |
| 24 class DateTestCase(unittest.TestCase): | 24 class DateTestCase(unittest.TestCase): |
| 25 def testDateInterval(self): | 25 def testDateInterval(self): |
| 26 ae = self.assertEqual | 26 ae = self.assertEqual |
| 27 date = Date("2000-06-26.00:34:02 + 2d") | 27 date = Date("2000-06-26.00:34:02 + 2d") |
| 69 date = Date("14:25", -5) | 69 date = Date("14:25", -5) |
| 70 ae(str(date), '%s-%02d-%02d.19:25:00'%(y, m, d)) | 70 ae(str(date), '%s-%02d-%02d.19:25:00'%(y, m, d)) |
| 71 date = Date("8:47:11", -5) | 71 date = Date("8:47:11", -5) |
| 72 ae(str(date), '%s-%02d-%02d.13:47:11'%(y, m, d)) | 72 ae(str(date), '%s-%02d-%02d.13:47:11'%(y, m, d)) |
| 73 | 73 |
| 74 # now check calculations | 74 def testOffsetRandom(self): |
| 75 ae = self.assertEqual | |
| 76 # XXX unsure of the usefulness of these, they're pretty random | |
| 75 date = Date('2000-01-01') + Interval('- 2y 2m') | 77 date = Date('2000-01-01') + Interval('- 2y 2m') |
| 76 ae(str(date), '1997-11-01.00:00:00') | 78 ae(str(date), '1997-11-01.00:00:00') |
| 77 date = Date('2000-01-01 - 2y 2m') | 79 date = Date('2000-01-01 - 2y 2m') |
| 78 ae(str(date), '1997-11-01.00:00:00') | 80 ae(str(date), '1997-11-01.00:00:00') |
| 79 date = Date('2000-01-01') + Interval('2m') | 81 date = Date('2000-01-01') + Interval('2m') |
| 84 date = Date('2000-01-01') + Interval('60d') | 86 date = Date('2000-01-01') + Interval('60d') |
| 85 ae(str(date), '2000-03-01.00:00:00') | 87 ae(str(date), '2000-03-01.00:00:00') |
| 86 date = Date('2001-01-01') + Interval('60d') | 88 date = Date('2001-01-01') + Interval('60d') |
| 87 ae(str(date), '2001-03-02.00:00:00') | 89 ae(str(date), '2001-03-02.00:00:00') |
| 88 | 90 |
| 89 # time additions | 91 def testOffsetAdd(self): |
| 92 ae = self.assertEqual | |
| 90 date = Date('2000-02-28.23:59:59') + Interval('00:00:01') | 93 date = Date('2000-02-28.23:59:59') + Interval('00:00:01') |
| 91 ae(str(date), '2000-02-29.00:00:00') | 94 ae(str(date), '2000-02-29.00:00:00') |
| 92 date = Date('2001-02-28.23:59:59') + Interval('00:00:01') | 95 date = Date('2001-02-28.23:59:59') + Interval('00:00:01') |
| 93 ae(str(date), '2001-03-01.00:00:00') | 96 ae(str(date), '2001-03-01.00:00:00') |
| 94 | 97 |
| 105 date = Date('2000-02-28.22:58:59') + Interval('00:00:3661') | 108 date = Date('2000-02-28.22:58:59') + Interval('00:00:3661') |
| 106 ae(str(date), '2000-02-29.00:00:00') | 109 ae(str(date), '2000-02-29.00:00:00') |
| 107 date = Date('2001-02-28.22:58:59') + Interval('00:00:3661') | 110 date = Date('2001-02-28.22:58:59') + Interval('00:00:3661') |
| 108 ae(str(date), '2001-03-01.00:00:00') | 111 ae(str(date), '2001-03-01.00:00:00') |
| 109 | 112 |
| 110 # now subtractions | 113 def testOffsetSub(self): |
| 114 ae = self.assertEqual | |
| 111 date = Date('2000-01-01') - Interval('- 2y 2m') | 115 date = Date('2000-01-01') - Interval('- 2y 2m') |
| 112 ae(str(date), '2002-03-01.00:00:00') | 116 ae(str(date), '2002-03-01.00:00:00') |
| 113 date = Date('2000-01-01') - Interval('2m') | 117 date = Date('2000-01-01') - Interval('2m') |
| 114 ae(str(date), '1999-11-01.00:00:00') | 118 ae(str(date), '1999-11-01.00:00:00') |
| 115 | 119 |
| 136 date = Date('2000-02-29.00:00:00') - Interval('00:00:3661') | 140 date = Date('2000-02-29.00:00:00') - Interval('00:00:3661') |
| 137 ae(str(date), '2000-02-28.22:58:59') | 141 ae(str(date), '2000-02-28.22:58:59') |
| 138 date = Date('2001-03-01.00:00:00') - Interval('00:00:3661') | 142 date = Date('2001-03-01.00:00:00') - Interval('00:00:3661') |
| 139 ae(str(date), '2001-02-28.22:58:59') | 143 ae(str(date), '2001-02-28.22:58:59') |
| 140 | 144 |
| 141 # local() | 145 def testDateLocal(self): |
| 146 ae = self.assertEqual | |
| 142 date = Date("02:42:20") | 147 date = Date("02:42:20") |
| 143 date = date.local(10) | 148 date = date.local(10) |
| 149 y, m, d, x, x, x, x, x, x = time.gmtime(time.time()) | |
| 144 ae(str(date), '%s-%02d-%02d.12:42:20'%(y, m, d)) | 150 ae(str(date), '%s-%02d-%02d.12:42:20'%(y, m, d)) |
| 145 | 151 |
| 146 def testInterval(self): | 152 def testIntervalInit(self): |
| 147 ae = self.assertEqual | 153 ae = self.assertEqual |
| 148 ae(str(Interval('3y')), '+ 3y') | 154 ae(str(Interval('3y')), '+ 3y') |
| 149 ae(str(Interval('2 y 1 m')), '+ 2y 1m') | 155 ae(str(Interval('2 y 1 m')), '+ 2y 1m') |
| 150 ae(str(Interval('1m 25d')), '+ 1m 25d') | 156 ae(str(Interval('1m 25d')), '+ 1m 25d') |
| 151 ae(str(Interval('-2w 3 d ')), '- 17d') | 157 ae(str(Interval('-2w 3 d ')), '- 17d') |
| 152 ae(str(Interval(' - 1 d 2:50 ')), '- 1d 2:50') | 158 ae(str(Interval(' - 1 d 2:50 ')), '- 1d 2:50') |
| 153 ae(str(Interval(' 14:00 ')), '+ 14:00') | 159 ae(str(Interval(' 14:00 ')), '+ 14:00') |
| 154 ae(str(Interval(' 0:04:33 ')), '+ 0:04:33') | 160 ae(str(Interval(' 0:04:33 ')), '+ 0:04:33') |
| 155 | 161 |
| 156 # __add__ | 162 def testIntervalAdd(self): |
| 157 # XXX these are fairly arbitrary and need fixing once the __add__ | 163 ae = self.assertEqual |
| 158 # code handles the odd cases more correctly | |
| 159 ae(str(Interval('1y') + Interval('1y')), '+ 2y') | 164 ae(str(Interval('1y') + Interval('1y')), '+ 2y') |
| 160 ae(str(Interval('1y') + Interval('1m')), '+ 1y 1m') | 165 ae(str(Interval('1y') + Interval('1m')), '+ 1y 1m') |
| 161 ae(str(Interval('1y') + Interval('2:40')), '+ 1y 2:40') | 166 ae(str(Interval('1y') + Interval('2:40')), '+ 1y 2:40') |
| 162 ae(str(Interval('1y') + Interval('- 1y')), '+') | 167 ae(str(Interval('1y') + Interval('- 1y')), '') |
| 163 ae(str(Interval('1y') + Interval('- 1m')), '+ 1y -1m') | 168 ae(str(Interval('- 1y') + Interval('1y')), '') |
| 164 | 169 ae(str(Interval('- 1y') + Interval('- 1y')), '- 2y') |
| 165 # TODO test add, subtraction, ?division? | 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') | |
| 178 | |
| 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') | |
| 191 | |
| 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)) | |
| 206 | |
| 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') | |
| 166 | 212 |
| 167 def suite(): | 213 def suite(): |
| 168 return unittest.makeSuite(DateTestCase, 'test') | 214 return unittest.makeSuite(DateTestCase, 'test') |
| 169 | 215 |
| 170 | 216 |
