Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_datetime.py @ 65035:87f4b32905df 2.7
Cosmetic fix to use print function.
| author | Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp> |
|---|---|
| date | Fri, 01 Oct 2010 10:48:47 +0000 |
| parents | 311a253b58b7 |
| children | 614c84670c63 |
| rev | line source |
|---|---|
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
1 """Test date/time type. |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2 |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
3 See http://www.zope.org/Members/fdrake/DateTimeWiki/TestCases |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
4 """ |
|
61454
6a2d0cc7d604
Issue #8644: Improve accuracy of timedelta.total_seconds method.
Mark Dickinson <dickinsm@gmail.com>
parents:
58472
diff
changeset
|
5 from __future__ import division |
|
61938
4745e13aa9e4
Issue #7879: Do not test negative timestamps on any Windows platform
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
61454
diff
changeset
|
6 import sys |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
7 import pickle |
|
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
8 import cPickle |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
9 import unittest |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
10 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
11 from test import test_support |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
12 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
13 from datetime import MINYEAR, MAXYEAR |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
14 from datetime import timedelta |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
15 from datetime import tzinfo |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
16 from datetime import time |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
17 from datetime import date, datetime |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
18 |
|
27689
0ec3896886e6
Build pickler_choices list in a lazier way.
Tim Peters <tim.peters@gmail.com>
parents:
27621
diff
changeset
|
19 pickle_choices = [(pickler, unpickler, proto) |
|
0ec3896886e6
Build pickler_choices list in a lazier way.
Tim Peters <tim.peters@gmail.com>
parents:
27621
diff
changeset
|
20 for pickler in pickle, cPickle |
|
0ec3896886e6
Build pickler_choices list in a lazier way.
Tim Peters <tim.peters@gmail.com>
parents:
27621
diff
changeset
|
21 for unpickler in pickle, cPickle |
|
0ec3896886e6
Build pickler_choices list in a lazier way.
Tim Peters <tim.peters@gmail.com>
parents:
27621
diff
changeset
|
22 for proto in range(3)] |
|
0ec3896886e6
Build pickler_choices list in a lazier way.
Tim Peters <tim.peters@gmail.com>
parents:
27621
diff
changeset
|
23 assert len(pickle_choices) == 2*2*3 |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
24 |
|
27766
c5ad0a2489b8
The Python implementation of datetime was changed in ways that no longer
Tim Peters <tim.peters@gmail.com>
parents:
27763
diff
changeset
|
25 # An arbitrary collection of objects of non-datetime types, for testing |
|
c5ad0a2489b8
The Python implementation of datetime was changed in ways that no longer
Tim Peters <tim.peters@gmail.com>
parents:
27763
diff
changeset
|
26 # mixed-type comparisons. |
|
c5ad0a2489b8
The Python implementation of datetime was changed in ways that no longer
Tim Peters <tim.peters@gmail.com>
parents:
27763
diff
changeset
|
27 OTHERSTUFF = (10, 10L, 34.5, "abc", {}, [], ()) |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
28 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
29 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
30 ############################################################################# |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
31 # module tests |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
32 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
33 class TestModule(unittest.TestCase): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
34 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
35 def test_constants(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
36 import datetime |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
37 self.assertEqual(datetime.MINYEAR, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
38 self.assertEqual(datetime.MAXYEAR, 9999) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
39 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
40 ############################################################################# |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
41 # tzinfo tests |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
42 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
43 class FixedOffset(tzinfo): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
44 def __init__(self, offset, name, dstoffset=42): |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
45 if isinstance(offset, int): |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
46 offset = timedelta(minutes=offset) |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
47 if isinstance(dstoffset, int): |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
48 dstoffset = timedelta(minutes=dstoffset) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
49 self.__offset = offset |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
50 self.__name = name |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
51 self.__dstoffset = dstoffset |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
52 def __repr__(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
53 return self.__name.lower() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
54 def utcoffset(self, dt): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
55 return self.__offset |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
56 def tzname(self, dt): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
57 return self.__name |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
58 def dst(self, dt): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
59 return self.__dstoffset |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
60 |
|
26737
6f1f84203ecf
Changes sufficient so that pickles written by the Python implementation
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
61 class PicklableFixedOffset(FixedOffset): |
|
6f1f84203ecf
Changes sufficient so that pickles written by the Python implementation
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
62 def __init__(self, offset=None, name=None, dstoffset=None): |
|
6f1f84203ecf
Changes sufficient so that pickles written by the Python implementation
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
63 FixedOffset.__init__(self, offset, name, dstoffset) |
|
6f1f84203ecf
Changes sufficient so that pickles written by the Python implementation
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
64 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
65 class TestTZInfo(unittest.TestCase): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
66 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
67 def test_non_abstractness(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
68 # In order to allow subclasses to get pickled, the C implementation |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
69 # wasn't able to get away with having __init__ raise |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
70 # NotImplementedError. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
71 useless = tzinfo() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
72 dt = datetime.max |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
73 self.assertRaises(NotImplementedError, useless.tzname, dt) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
74 self.assertRaises(NotImplementedError, useless.utcoffset, dt) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
75 self.assertRaises(NotImplementedError, useless.dst, dt) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
76 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
77 def test_subclass_must_override(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
78 class NotEnough(tzinfo): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
79 def __init__(self, offset, name): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
80 self.__offset = offset |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
81 self.__name = name |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
82 self.assertTrue(issubclass(NotEnough, tzinfo)) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
83 ne = NotEnough(3, "NotByALongShot") |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
84 self.assertIsInstance(ne, tzinfo) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
85 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
86 dt = datetime.now() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
87 self.assertRaises(NotImplementedError, ne.tzname, dt) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
88 self.assertRaises(NotImplementedError, ne.utcoffset, dt) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
89 self.assertRaises(NotImplementedError, ne.dst, dt) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
90 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
91 def test_normal(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
92 fo = FixedOffset(3, "Three") |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
93 self.assertIsInstance(fo, tzinfo) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
94 for dt in datetime.now(), None: |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
95 self.assertEqual(fo.utcoffset(dt), timedelta(minutes=3)) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
96 self.assertEqual(fo.tzname(dt), "Three") |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
97 self.assertEqual(fo.dst(dt), timedelta(minutes=42)) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
98 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
99 def test_pickling_base(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
100 # There's no point to pickling tzinfo objects on their own (they |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
101 # carry no data), but they need to be picklable anyway else |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
102 # concrete subclasses can't be pickled. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
103 orig = tzinfo.__new__(tzinfo) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
104 self.assertTrue(type(orig) is tzinfo) |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
105 for pickler, unpickler, proto in pickle_choices: |
|
27963
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27766
diff
changeset
|
106 green = pickler.dumps(orig, proto) |
|
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27766
diff
changeset
|
107 derived = unpickler.loads(green) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
108 self.assertTrue(type(derived) is tzinfo) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
109 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
110 def test_pickling_subclass(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
111 # Make sure we can pickle/unpickle an instance of a subclass. |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
112 offset = timedelta(minutes=-300) |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
113 orig = PicklableFixedOffset(offset, 'cookie') |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
114 self.assertIsInstance(orig, tzinfo) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
115 self.assertTrue(type(orig) is PicklableFixedOffset) |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
116 self.assertEqual(orig.utcoffset(None), offset) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
117 self.assertEqual(orig.tzname(None), 'cookie') |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
118 for pickler, unpickler, proto in pickle_choices: |
|
27963
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27766
diff
changeset
|
119 green = pickler.dumps(orig, proto) |
|
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27766
diff
changeset
|
120 derived = unpickler.loads(green) |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
121 self.assertIsInstance(derived, tzinfo) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
122 self.assertTrue(type(derived) is PicklableFixedOffset) |
|
27963
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27766
diff
changeset
|
123 self.assertEqual(derived.utcoffset(None), offset) |
|
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27766
diff
changeset
|
124 self.assertEqual(derived.tzname(None), 'cookie') |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
125 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
126 ############################################################################# |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
127 # Base clase for testing a particular aspect of timedelta, time, date and |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
128 # datetime comparisons. |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
129 |
|
41534
6c4d7678af5e
Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents:
41061
diff
changeset
|
130 class HarmlessMixedComparison: |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
131 # Test that __eq__ and __ne__ don't complain for mixed-type comparisons. |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
132 |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
133 # Subclasses must define 'theclass', and theclass(1, 1, 1) must be a |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
134 # legit constructor. |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
135 |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
136 def test_harmless_mixed_comparison(self): |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
137 me = self.theclass(1, 1, 1) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
138 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
139 self.assertFalse(me == ()) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
140 self.assertTrue(me != ()) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
141 self.assertFalse(() == me) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
142 self.assertTrue(() != me) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
143 |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57987
diff
changeset
|
144 self.assertIn(me, [1, 20L, [], me]) |
|
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57987
diff
changeset
|
145 self.assertIn([], [me, 1, 20L, []]) |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
146 |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
147 def test_harmful_mixed_comparison(self): |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
148 me = self.theclass(1, 1, 1) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
149 |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
150 self.assertRaises(TypeError, lambda: me < ()) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
151 self.assertRaises(TypeError, lambda: me <= ()) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
152 self.assertRaises(TypeError, lambda: me > ()) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
153 self.assertRaises(TypeError, lambda: me >= ()) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
154 |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
155 self.assertRaises(TypeError, lambda: () < me) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
156 self.assertRaises(TypeError, lambda: () <= me) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
157 self.assertRaises(TypeError, lambda: () > me) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
158 self.assertRaises(TypeError, lambda: () >= me) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
159 |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
160 self.assertRaises(TypeError, cmp, (), me) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
161 self.assertRaises(TypeError, cmp, me, ()) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
162 |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
163 ############################################################################# |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
164 # timedelta tests |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
165 |
|
41534
6c4d7678af5e
Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents:
41061
diff
changeset
|
166 class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase): |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
167 |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
168 theclass = timedelta |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
169 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
170 def test_constructor(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
171 eq = self.assertEqual |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
172 td = timedelta |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
173 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
174 # Check keyword args to constructor |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
175 eq(td(), td(weeks=0, days=0, hours=0, minutes=0, seconds=0, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
176 milliseconds=0, microseconds=0)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
177 eq(td(1), td(days=1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
178 eq(td(0, 1), td(seconds=1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
179 eq(td(0, 0, 1), td(microseconds=1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
180 eq(td(weeks=1), td(days=7)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
181 eq(td(days=1), td(hours=24)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
182 eq(td(hours=1), td(minutes=60)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
183 eq(td(minutes=1), td(seconds=60)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
184 eq(td(seconds=1), td(milliseconds=1000)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
185 eq(td(milliseconds=1), td(microseconds=1000)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
186 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
187 # Check float args to constructor |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
188 eq(td(weeks=1.0/7), td(days=1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
189 eq(td(days=1.0/24), td(hours=1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
190 eq(td(hours=1.0/60), td(minutes=1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
191 eq(td(minutes=1.0/60), td(seconds=1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
192 eq(td(seconds=0.001), td(milliseconds=1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
193 eq(td(milliseconds=0.001), td(microseconds=1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
194 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
195 def test_computations(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
196 eq = self.assertEqual |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
197 td = timedelta |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
198 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
199 a = td(7) # One week |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
200 b = td(0, 60) # One minute |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
201 c = td(0, 0, 1000) # One millisecond |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
202 eq(a+b+c, td(7, 60, 1000)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
203 eq(a-b, td(6, 24*3600 - 60)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
204 eq(-a, td(-7)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
205 eq(+a, td(7)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
206 eq(-b, td(-1, 24*3600 - 60)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
207 eq(-c, td(-1, 24*3600 - 1, 999000)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
208 eq(abs(a), a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
209 eq(abs(-a), a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
210 eq(td(6, 24*3600), a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
211 eq(td(0, 0, 60*1000000), b) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
212 eq(a*10, td(70)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
213 eq(a*10, 10*a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
214 eq(a*10L, 10*a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
215 eq(b*10, td(0, 600)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
216 eq(10*b, td(0, 600)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
217 eq(b*10L, td(0, 600)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
218 eq(c*10, td(0, 0, 10000)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
219 eq(10*c, td(0, 0, 10000)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
220 eq(c*10L, td(0, 0, 10000)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
221 eq(a*-1, -a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
222 eq(b*-2, -b-b) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
223 eq(c*-2, -c+-c) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
224 eq(b*(60*24), (b*60)*24) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
225 eq(b*(60*24), (60*b)*24) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
226 eq(c*1000, td(0, 1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
227 eq(1000*c, td(0, 1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
228 eq(a//7, td(1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
229 eq(b//10, td(0, 6)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
230 eq(c//1000, td(0, 0, 1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
231 eq(a//10, td(0, 7*24*360)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
232 eq(a//3600000, td(0, 0, 7*24*1000)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
233 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
234 def test_disallowed_computations(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
235 a = timedelta(42) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
236 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
237 # Add/sub ints, longs, floats should be illegal |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
238 for i in 1, 1L, 1.0: |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
239 self.assertRaises(TypeError, lambda: a+i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
240 self.assertRaises(TypeError, lambda: a-i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
241 self.assertRaises(TypeError, lambda: i+a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
242 self.assertRaises(TypeError, lambda: i-a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
243 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
244 # Mul/div by float isn't supported. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
245 x = 2.3 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
246 self.assertRaises(TypeError, lambda: a*x) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
247 self.assertRaises(TypeError, lambda: x*a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
248 self.assertRaises(TypeError, lambda: a/x) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
249 self.assertRaises(TypeError, lambda: x/a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
250 self.assertRaises(TypeError, lambda: a // x) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
251 self.assertRaises(TypeError, lambda: x // a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
252 |
|
49431
2e794757bdc8
Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48213
diff
changeset
|
253 # Division of int by timedelta doesn't make sense. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
254 # Division by zero doesn't make sense. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
255 for zero in 0, 0L: |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
256 self.assertRaises(TypeError, lambda: zero // a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
257 self.assertRaises(ZeroDivisionError, lambda: a // zero) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
258 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
259 def test_basic_attributes(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
260 days, seconds, us = 1, 7, 31 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
261 td = timedelta(days, seconds, us) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
262 self.assertEqual(td.days, days) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
263 self.assertEqual(td.seconds, seconds) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
264 self.assertEqual(td.microseconds, us) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
265 |
|
57461
a6981f4a74a1
Issue #5788: `datetime.timedelta` objects get a new `total_seconds()` method returning
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
266 def test_total_seconds(self): |
|
a6981f4a74a1
Issue #5788: `datetime.timedelta` objects get a new `total_seconds()` method returning
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
267 td = timedelta(days=365) |
|
a6981f4a74a1
Issue #5788: `datetime.timedelta` objects get a new `total_seconds()` method returning
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
268 self.assertEqual(td.total_seconds(), 31536000.0) |
|
a6981f4a74a1
Issue #5788: `datetime.timedelta` objects get a new `total_seconds()` method returning
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
269 for total_seconds in [123456.789012, -123456.789012, 0.123456, 0, 1e6]: |
|
a6981f4a74a1
Issue #5788: `datetime.timedelta` objects get a new `total_seconds()` method returning
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
270 td = timedelta(seconds=total_seconds) |
|
a6981f4a74a1
Issue #5788: `datetime.timedelta` objects get a new `total_seconds()` method returning
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
271 self.assertEqual(td.total_seconds(), total_seconds) |
|
61454
6a2d0cc7d604
Issue #8644: Improve accuracy of timedelta.total_seconds method.
Mark Dickinson <dickinsm@gmail.com>
parents:
58472
diff
changeset
|
272 # Issue8644: Test that td.total_seconds() has the same |
|
6a2d0cc7d604
Issue #8644: Improve accuracy of timedelta.total_seconds method.
Mark Dickinson <dickinsm@gmail.com>
parents:
58472
diff
changeset
|
273 # accuracy as td / timedelta(seconds=1). |
|
6a2d0cc7d604
Issue #8644: Improve accuracy of timedelta.total_seconds method.
Mark Dickinson <dickinsm@gmail.com>
parents:
58472
diff
changeset
|
274 for ms in [-1, -2, -123]: |
|
6a2d0cc7d604
Issue #8644: Improve accuracy of timedelta.total_seconds method.
Mark Dickinson <dickinsm@gmail.com>
parents:
58472
diff
changeset
|
275 td = timedelta(microseconds=ms) |
|
6a2d0cc7d604
Issue #8644: Improve accuracy of timedelta.total_seconds method.
Mark Dickinson <dickinsm@gmail.com>
parents:
58472
diff
changeset
|
276 self.assertEqual(td.total_seconds(), |
|
6a2d0cc7d604
Issue #8644: Improve accuracy of timedelta.total_seconds method.
Mark Dickinson <dickinsm@gmail.com>
parents:
58472
diff
changeset
|
277 ((24*3600*td.days + td.seconds)*10**6 |
|
6a2d0cc7d604
Issue #8644: Improve accuracy of timedelta.total_seconds method.
Mark Dickinson <dickinsm@gmail.com>
parents:
58472
diff
changeset
|
278 + td.microseconds)/10**6) |
|
57461
a6981f4a74a1
Issue #5788: `datetime.timedelta` objects get a new `total_seconds()` method returning
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
279 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
280 def test_carries(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
281 t1 = timedelta(days=100, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
282 weeks=-7, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
283 hours=-24*(100-49), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
284 minutes=-3, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
285 seconds=12, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
286 microseconds=(3*60 - 12) * 1e6 + 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
287 t2 = timedelta(microseconds=1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
288 self.assertEqual(t1, t2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
289 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
290 def test_hash_equality(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
291 t1 = timedelta(days=100, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
292 weeks=-7, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
293 hours=-24*(100-49), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
294 minutes=-3, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
295 seconds=12, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
296 microseconds=(3*60 - 12) * 1000000) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
297 t2 = timedelta() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
298 self.assertEqual(hash(t1), hash(t2)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
299 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
300 t1 += timedelta(weeks=7) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
301 t2 += timedelta(days=7*7) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
302 self.assertEqual(t1, t2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
303 self.assertEqual(hash(t1), hash(t2)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
304 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
305 d = {t1: 1} |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
306 d[t2] = 2 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
307 self.assertEqual(len(d), 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
308 self.assertEqual(d[t1], 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
309 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
310 def test_pickling(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
311 args = 12, 34, 56 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
312 orig = timedelta(*args) |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
313 for pickler, unpickler, proto in pickle_choices: |
|
27568
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
314 green = pickler.dumps(orig, proto) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
315 derived = unpickler.loads(green) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
316 self.assertEqual(orig, derived) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
317 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
318 def test_compare(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
319 t1 = timedelta(2, 3, 4) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
320 t2 = timedelta(2, 3, 4) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
321 self.assertTrue(t1 == t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
322 self.assertTrue(t1 <= t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
323 self.assertTrue(t1 >= t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
324 self.assertTrue(not t1 != t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
325 self.assertTrue(not t1 < t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
326 self.assertTrue(not t1 > t2) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
327 self.assertEqual(cmp(t1, t2), 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
328 self.assertEqual(cmp(t2, t1), 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
329 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
330 for args in (3, 3, 3), (2, 4, 4), (2, 3, 5): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
331 t2 = timedelta(*args) # this is larger than t1 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
332 self.assertTrue(t1 < t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
333 self.assertTrue(t2 > t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
334 self.assertTrue(t1 <= t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
335 self.assertTrue(t2 >= t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
336 self.assertTrue(t1 != t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
337 self.assertTrue(t2 != t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
338 self.assertTrue(not t1 == t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
339 self.assertTrue(not t2 == t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
340 self.assertTrue(not t1 > t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
341 self.assertTrue(not t2 < t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
342 self.assertTrue(not t1 >= t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
343 self.assertTrue(not t2 <= t1) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
344 self.assertEqual(cmp(t1, t2), -1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
345 self.assertEqual(cmp(t2, t1), 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
346 |
|
27766
c5ad0a2489b8
The Python implementation of datetime was changed in ways that no longer
Tim Peters <tim.peters@gmail.com>
parents:
27763
diff
changeset
|
347 for badarg in OTHERSTUFF: |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
348 self.assertEqual(t1 == badarg, False) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
349 self.assertEqual(t1 != badarg, True) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
350 self.assertEqual(badarg == t1, False) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
351 self.assertEqual(badarg != t1, True) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
352 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
353 self.assertRaises(TypeError, lambda: t1 <= badarg) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
354 self.assertRaises(TypeError, lambda: t1 < badarg) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
355 self.assertRaises(TypeError, lambda: t1 > badarg) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
356 self.assertRaises(TypeError, lambda: t1 >= badarg) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
357 self.assertRaises(TypeError, lambda: badarg <= t1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
358 self.assertRaises(TypeError, lambda: badarg < t1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
359 self.assertRaises(TypeError, lambda: badarg > t1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
360 self.assertRaises(TypeError, lambda: badarg >= t1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
361 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
362 def test_str(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
363 td = timedelta |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
364 eq = self.assertEqual |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
365 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
366 eq(str(td(1)), "1 day, 0:00:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
367 eq(str(td(-1)), "-1 day, 0:00:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
368 eq(str(td(2)), "2 days, 0:00:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
369 eq(str(td(-2)), "-2 days, 0:00:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
370 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
371 eq(str(td(hours=12, minutes=58, seconds=59)), "12:58:59") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
372 eq(str(td(hours=2, minutes=3, seconds=4)), "2:03:04") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
373 eq(str(td(weeks=-30, hours=23, minutes=12, seconds=34)), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
374 "-210 days, 23:12:34") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
375 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
376 eq(str(td(milliseconds=1)), "0:00:00.001000") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
377 eq(str(td(microseconds=3)), "0:00:00.000003") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
378 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
379 eq(str(td(days=999999999, hours=23, minutes=59, seconds=59, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
380 microseconds=999999)), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
381 "999999999 days, 23:59:59.999999") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
382 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
383 def test_roundtrip(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
384 for td in (timedelta(days=999999999, hours=23, minutes=59, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
385 seconds=59, microseconds=999999), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
386 timedelta(days=-999999999), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
387 timedelta(days=1, seconds=2, microseconds=3)): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
388 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
389 # Verify td -> string -> td identity. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
390 s = repr(td) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
391 self.assertTrue(s.startswith('datetime.')) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
392 s = s[9:] |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
393 td2 = eval(s) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
394 self.assertEqual(td, td2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
395 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
396 # Verify identity via reconstructing from pieces. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
397 td2 = timedelta(td.days, td.seconds, td.microseconds) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
398 self.assertEqual(td, td2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
399 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
400 def test_resolution_info(self): |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
401 self.assertIsInstance(timedelta.min, timedelta) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
402 self.assertIsInstance(timedelta.max, timedelta) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
403 self.assertIsInstance(timedelta.resolution, timedelta) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
404 self.assertTrue(timedelta.max > timedelta.min) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
405 self.assertEqual(timedelta.min, timedelta(-999999999)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
406 self.assertEqual(timedelta.max, timedelta(999999999, 24*3600-1, 1e6-1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
407 self.assertEqual(timedelta.resolution, timedelta(0, 0, 1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
408 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
409 def test_overflow(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
410 tiny = timedelta.resolution |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
411 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
412 td = timedelta.min + tiny |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
413 td -= tiny # no problem |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
414 self.assertRaises(OverflowError, td.__sub__, tiny) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
415 self.assertRaises(OverflowError, td.__add__, -tiny) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
416 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
417 td = timedelta.max - tiny |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
418 td += tiny # no problem |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
419 self.assertRaises(OverflowError, td.__add__, tiny) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
420 self.assertRaises(OverflowError, td.__sub__, -tiny) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
421 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
422 self.assertRaises(OverflowError, lambda: -timedelta.max) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
423 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
424 def test_microsecond_rounding(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
425 td = timedelta |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
426 eq = self.assertEqual |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
427 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
428 # Single-field rounding. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
429 eq(td(milliseconds=0.4/1000), td(0)) # rounds to 0 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
430 eq(td(milliseconds=-0.4/1000), td(0)) # rounds to 0 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
431 eq(td(milliseconds=0.6/1000), td(microseconds=1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
432 eq(td(milliseconds=-0.6/1000), td(microseconds=-1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
433 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
434 # Rounding due to contributions from more than one field. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
435 us_per_hour = 3600e6 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
436 us_per_day = us_per_hour * 24 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
437 eq(td(days=.4/us_per_day), td(0)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
438 eq(td(hours=.2/us_per_hour), td(0)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
439 eq(td(days=.4/us_per_day, hours=.2/us_per_hour), td(microseconds=1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
440 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
441 eq(td(days=-.4/us_per_day), td(0)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
442 eq(td(hours=-.2/us_per_hour), td(0)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
443 eq(td(days=-.4/us_per_day, hours=-.2/us_per_hour), td(microseconds=-1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
444 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
445 def test_massive_normalization(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
446 td = timedelta(microseconds=-1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
447 self.assertEqual((td.days, td.seconds, td.microseconds), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
448 (-1, 24*3600-1, 999999)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
449 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
450 def test_bool(self): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
451 self.assertTrue(timedelta(1)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
452 self.assertTrue(timedelta(0, 1)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
453 self.assertTrue(timedelta(0, 0, 1)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
454 self.assertTrue(timedelta(microseconds=1)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
455 self.assertTrue(not timedelta(0)) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
456 |
|
29153
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
457 def test_subclass_timedelta(self): |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
458 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
459 class T(timedelta): |
| 34423 | 460 @staticmethod |
|
29153
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
461 def from_td(td): |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
462 return T(td.days, td.seconds, td.microseconds) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
463 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
464 def as_hours(self): |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
465 sum = (self.days * 24 + |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
466 self.seconds / 3600.0 + |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
467 self.microseconds / 3600e6) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
468 return round(sum) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
469 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
470 t1 = T(days=1) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
471 self.assertTrue(type(t1) is T) |
|
29153
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
472 self.assertEqual(t1.as_hours(), 24) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
473 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
474 t2 = T(days=-1, seconds=-3600) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
475 self.assertTrue(type(t2) is T) |
|
29153
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
476 self.assertEqual(t2.as_hours(), -25) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
477 |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
478 t3 = t1 + t2 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
479 self.assertTrue(type(t3) is timedelta) |
|
29153
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
480 t4 = T.from_td(t3) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
481 self.assertTrue(type(t4) is T) |
|
29153
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
482 self.assertEqual(t3.days, t4.days) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
483 self.assertEqual(t3.seconds, t4.seconds) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
484 self.assertEqual(t3.microseconds, t4.microseconds) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
485 self.assertEqual(str(t3), str(t4)) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
486 self.assertEqual(t4.as_hours(), -1) |
|
16721d73ad29
datetime.timedelta is now subclassable in Python. The new test shows
Tim Peters <tim.peters@gmail.com>
parents:
29149
diff
changeset
|
487 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
488 ############################################################################# |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
489 # date tests |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
490 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
491 class TestDateOnly(unittest.TestCase): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
492 # Tests here won't pass if also run on datetime objects, so don't |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
493 # subclass this to test datetimes too. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
494 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
495 def test_delta_non_days_ignored(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
496 dt = date(2000, 1, 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
497 delta = timedelta(days=1, hours=2, minutes=3, seconds=4, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
498 microseconds=5) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
499 days = timedelta(delta.days) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
500 self.assertEqual(days, timedelta(1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
501 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
502 dt2 = dt + delta |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
503 self.assertEqual(dt2, dt + days) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
504 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
505 dt2 = delta + dt |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
506 self.assertEqual(dt2, dt + days) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
507 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
508 dt2 = dt - delta |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
509 self.assertEqual(dt2, dt - days) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
510 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
511 delta = -delta |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
512 days = timedelta(delta.days) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
513 self.assertEqual(days, timedelta(-2)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
514 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
515 dt2 = dt + delta |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
516 self.assertEqual(dt2, dt + days) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
517 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
518 dt2 = delta + dt |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
519 self.assertEqual(dt2, dt + days) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
520 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
521 dt2 = dt - delta |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
522 self.assertEqual(dt2, dt - days) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
523 |
|
32298
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
524 class SubclassDate(date): |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
525 sub_var = 1 |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
526 |
|
41534
6c4d7678af5e
Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents:
41061
diff
changeset
|
527 class TestDate(HarmlessMixedComparison, unittest.TestCase): |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
528 # Tests here should pass for both dates and datetimes, except for a |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
529 # few tests that TestDateTime overrides. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
530 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
531 theclass = date |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
532 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
533 def test_basic_attributes(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
534 dt = self.theclass(2002, 3, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
535 self.assertEqual(dt.year, 2002) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
536 self.assertEqual(dt.month, 3) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
537 self.assertEqual(dt.day, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
538 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
539 def test_roundtrip(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
540 for dt in (self.theclass(1, 2, 3), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
541 self.theclass.today()): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
542 # Verify dt -> string -> date identity. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
543 s = repr(dt) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
544 self.assertTrue(s.startswith('datetime.')) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
545 s = s[9:] |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
546 dt2 = eval(s) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
547 self.assertEqual(dt, dt2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
548 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
549 # Verify identity via reconstructing from pieces. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
550 dt2 = self.theclass(dt.year, dt.month, dt.day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
551 self.assertEqual(dt, dt2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
552 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
553 def test_ordinal_conversions(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
554 # Check some fixed values. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
555 for y, m, d, n in [(1, 1, 1, 1), # calendar origin |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
556 (1, 12, 31, 365), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
557 (2, 1, 1, 366), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
558 # first example from "Calendrical Calculations" |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
559 (1945, 11, 12, 710347)]: |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
560 d = self.theclass(y, m, d) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
561 self.assertEqual(n, d.toordinal()) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
562 fromord = self.theclass.fromordinal(n) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
563 self.assertEqual(d, fromord) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
564 if hasattr(fromord, "hour"): |
|
27963
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27766
diff
changeset
|
565 # if we're checking something fancier than a date, verify |
|
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27766
diff
changeset
|
566 # the extra fields have been zeroed out |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
567 self.assertEqual(fromord.hour, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
568 self.assertEqual(fromord.minute, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
569 self.assertEqual(fromord.second, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
570 self.assertEqual(fromord.microsecond, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
571 |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
572 # Check first and last days of year spottily across the whole |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
573 # range of years supported. |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
574 for year in xrange(MINYEAR, MAXYEAR+1, 7): |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
575 # Verify (year, 1, 1) -> ordinal -> y, m, d is identity. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
576 d = self.theclass(year, 1, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
577 n = d.toordinal() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
578 d2 = self.theclass.fromordinal(n) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
579 self.assertEqual(d, d2) |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
580 # Verify that moving back a day gets to the end of year-1. |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
581 if year > 1: |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
582 d = self.theclass.fromordinal(n-1) |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
583 d2 = self.theclass(year-1, 12, 31) |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
584 self.assertEqual(d, d2) |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
585 self.assertEqual(d2.toordinal(), n-1) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
586 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
587 # Test every day in a leap-year and a non-leap year. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
588 dim = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
589 for year, isleap in (2000, True), (2002, False): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
590 n = self.theclass(year, 1, 1).toordinal() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
591 for month, maxday in zip(range(1, 13), dim): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
592 if month == 2 and isleap: |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
593 maxday += 1 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
594 for day in range(1, maxday+1): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
595 d = self.theclass(year, month, day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
596 self.assertEqual(d.toordinal(), n) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
597 self.assertEqual(d, self.theclass.fromordinal(n)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
598 n += 1 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
599 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
600 def test_extreme_ordinals(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
601 a = self.theclass.min |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
602 a = self.theclass(a.year, a.month, a.day) # get rid of time parts |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
603 aord = a.toordinal() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
604 b = a.fromordinal(aord) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
605 self.assertEqual(a, b) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
606 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
607 self.assertRaises(ValueError, lambda: a.fromordinal(aord - 1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
608 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
609 b = a + timedelta(days=1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
610 self.assertEqual(b.toordinal(), aord + 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
611 self.assertEqual(b, self.theclass.fromordinal(aord + 1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
612 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
613 a = self.theclass.max |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
614 a = self.theclass(a.year, a.month, a.day) # get rid of time parts |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
615 aord = a.toordinal() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
616 b = a.fromordinal(aord) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
617 self.assertEqual(a, b) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
618 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
619 self.assertRaises(ValueError, lambda: a.fromordinal(aord + 1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
620 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
621 b = a - timedelta(days=1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
622 self.assertEqual(b.toordinal(), aord - 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
623 self.assertEqual(b, self.theclass.fromordinal(aord - 1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
624 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
625 def test_bad_constructor_arguments(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
626 # bad years |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
627 self.theclass(MINYEAR, 1, 1) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
628 self.theclass(MAXYEAR, 1, 1) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
629 self.assertRaises(ValueError, self.theclass, MINYEAR-1, 1, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
630 self.assertRaises(ValueError, self.theclass, MAXYEAR+1, 1, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
631 # bad months |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
632 self.theclass(2000, 1, 1) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
633 self.theclass(2000, 12, 1) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
634 self.assertRaises(ValueError, self.theclass, 2000, 0, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
635 self.assertRaises(ValueError, self.theclass, 2000, 13, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
636 # bad days |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
637 self.theclass(2000, 2, 29) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
638 self.theclass(2004, 2, 29) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
639 self.theclass(2400, 2, 29) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
640 self.assertRaises(ValueError, self.theclass, 2000, 2, 30) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
641 self.assertRaises(ValueError, self.theclass, 2001, 2, 29) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
642 self.assertRaises(ValueError, self.theclass, 2100, 2, 29) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
643 self.assertRaises(ValueError, self.theclass, 1900, 2, 29) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
644 self.assertRaises(ValueError, self.theclass, 2000, 1, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
645 self.assertRaises(ValueError, self.theclass, 2000, 1, 32) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
646 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
647 def test_hash_equality(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
648 d = self.theclass(2000, 12, 31) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
649 # same thing |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
650 e = self.theclass(2000, 12, 31) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
651 self.assertEqual(d, e) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
652 self.assertEqual(hash(d), hash(e)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
653 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
654 dic = {d: 1} |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
655 dic[e] = 2 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
656 self.assertEqual(len(dic), 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
657 self.assertEqual(dic[d], 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
658 self.assertEqual(dic[e], 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
659 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
660 d = self.theclass(2001, 1, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
661 # same thing |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
662 e = self.theclass(2001, 1, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
663 self.assertEqual(d, e) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
664 self.assertEqual(hash(d), hash(e)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
665 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
666 dic = {d: 1} |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
667 dic[e] = 2 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
668 self.assertEqual(len(dic), 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
669 self.assertEqual(dic[d], 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
670 self.assertEqual(dic[e], 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
671 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
672 def test_computations(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
673 a = self.theclass(2002, 1, 31) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
674 b = self.theclass(1956, 1, 31) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
675 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
676 diff = a-b |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
677 self.assertEqual(diff.days, 46*365 + len(range(1956, 2002, 4))) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
678 self.assertEqual(diff.seconds, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
679 self.assertEqual(diff.microseconds, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
680 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
681 day = timedelta(1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
682 week = timedelta(7) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
683 a = self.theclass(2002, 3, 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
684 self.assertEqual(a + day, self.theclass(2002, 3, 3)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
685 self.assertEqual(day + a, self.theclass(2002, 3, 3)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
686 self.assertEqual(a - day, self.theclass(2002, 3, 1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
687 self.assertEqual(-day + a, self.theclass(2002, 3, 1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
688 self.assertEqual(a + week, self.theclass(2002, 3, 9)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
689 self.assertEqual(a - week, self.theclass(2002, 2, 23)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
690 self.assertEqual(a + 52*week, self.theclass(2003, 3, 1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
691 self.assertEqual(a - 52*week, self.theclass(2001, 3, 3)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
692 self.assertEqual((a + week) - a, week) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
693 self.assertEqual((a + day) - a, day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
694 self.assertEqual((a - week) - a, -week) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
695 self.assertEqual((a - day) - a, -day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
696 self.assertEqual(a - (a + week), -week) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
697 self.assertEqual(a - (a + day), -day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
698 self.assertEqual(a - (a - week), week) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
699 self.assertEqual(a - (a - day), day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
700 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
701 # Add/sub ints, longs, floats should be illegal |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
702 for i in 1, 1L, 1.0: |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
703 self.assertRaises(TypeError, lambda: a+i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
704 self.assertRaises(TypeError, lambda: a-i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
705 self.assertRaises(TypeError, lambda: i+a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
706 self.assertRaises(TypeError, lambda: i-a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
707 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
708 # delta - date is senseless. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
709 self.assertRaises(TypeError, lambda: day - a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
710 # mixing date and (delta or date) via * or // is senseless |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
711 self.assertRaises(TypeError, lambda: day * a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
712 self.assertRaises(TypeError, lambda: a * day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
713 self.assertRaises(TypeError, lambda: day // a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
714 self.assertRaises(TypeError, lambda: a // day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
715 self.assertRaises(TypeError, lambda: a * a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
716 self.assertRaises(TypeError, lambda: a // a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
717 # date + date is senseless |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
718 self.assertRaises(TypeError, lambda: a + a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
719 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
720 def test_overflow(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
721 tiny = self.theclass.resolution |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
722 |
|
61949
311a253b58b7
Issue #7150: Raise OverflowError if the result of adding or subtracting
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
61942
diff
changeset
|
723 for delta in [tiny, timedelta(1), timedelta(2)]: |
|
311a253b58b7
Issue #7150: Raise OverflowError if the result of adding or subtracting
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
61942
diff
changeset
|
724 dt = self.theclass.min + delta |
|
311a253b58b7
Issue #7150: Raise OverflowError if the result of adding or subtracting
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
61942
diff
changeset
|
725 dt -= delta # no problem |
|
311a253b58b7
Issue #7150: Raise OverflowError if the result of adding or subtracting
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
61942
diff
changeset
|
726 self.assertRaises(OverflowError, dt.__sub__, delta) |
|
311a253b58b7
Issue #7150: Raise OverflowError if the result of adding or subtracting
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
61942
diff
changeset
|
727 self.assertRaises(OverflowError, dt.__add__, -delta) |
|
311a253b58b7
Issue #7150: Raise OverflowError if the result of adding or subtracting
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
61942
diff
changeset
|
728 |
|
311a253b58b7
Issue #7150: Raise OverflowError if the result of adding or subtracting
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
61942
diff
changeset
|
729 dt = self.theclass.max - delta |
|
311a253b58b7
Issue #7150: Raise OverflowError if the result of adding or subtracting
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
61942
diff
changeset
|
730 dt += delta # no problem |
|
311a253b58b7
Issue #7150: Raise OverflowError if the result of adding or subtracting
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
61942
diff
changeset
|
731 self.assertRaises(OverflowError, dt.__add__, delta) |
|
311a253b58b7
Issue #7150: Raise OverflowError if the result of adding or subtracting
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
61942
diff
changeset
|
732 self.assertRaises(OverflowError, dt.__sub__, -delta) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
733 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
734 def test_fromtimestamp(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
735 import time |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
736 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
737 # Try an arbitrary fixed value. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
738 year, month, day = 1999, 9, 19 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
739 ts = time.mktime((year, month, day, 0, 0, 0, 0, 0, -1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
740 d = self.theclass.fromtimestamp(ts) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
741 self.assertEqual(d.year, year) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
742 self.assertEqual(d.month, month) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
743 self.assertEqual(d.day, day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
744 |
|
32388
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
745 def test_insane_fromtimestamp(self): |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
746 # It's possible that some platform maps time_t to double, |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
747 # and that this test will fail there. This test should |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
748 # exempt such platforms (provided they return reasonable |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
749 # results!). |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
750 for insane in -1e200, 1e200: |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
751 self.assertRaises(ValueError, self.theclass.fromtimestamp, |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
752 insane) |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
753 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
754 def test_today(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
755 import time |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
756 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
757 # We claim that today() is like fromtimestamp(time.time()), so |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
758 # prove it. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
759 for dummy in range(3): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
760 today = self.theclass.today() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
761 ts = time.time() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
762 todayagain = self.theclass.fromtimestamp(ts) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
763 if today == todayagain: |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
764 break |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
765 # There are several legit reasons that could fail: |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
766 # 1. It recently became midnight, between the today() and the |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
767 # time() calls. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
768 # 2. The platform time() has such fine resolution that we'll |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
769 # never get the same value twice. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
770 # 3. The platform time() has poor resolution, and we just |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
771 # happened to call today() right before a resolution quantum |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
772 # boundary. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
773 # 4. The system clock got fiddled between calls. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
774 # In any case, wait a little while and try again. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
775 time.sleep(0.1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
776 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
777 # It worked or it didn't. If it didn't, assume it's reason #2, and |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
778 # let the test pass if they're within half a second of each other. |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
779 self.assertTrue(today == todayagain or |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
780 abs(todayagain - today) < timedelta(seconds=0.5)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
781 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
782 def test_weekday(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
783 for i in range(7): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
784 # March 4, 2002 is a Monday |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
785 self.assertEqual(self.theclass(2002, 3, 4+i).weekday(), i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
786 self.assertEqual(self.theclass(2002, 3, 4+i).isoweekday(), i+1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
787 # January 2, 1956 is a Monday |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
788 self.assertEqual(self.theclass(1956, 1, 2+i).weekday(), i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
789 self.assertEqual(self.theclass(1956, 1, 2+i).isoweekday(), i+1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
790 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
791 def test_isocalendar(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
792 # Check examples from |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
793 # http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
794 for i in range(7): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
795 d = self.theclass(2003, 12, 22+i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
796 self.assertEqual(d.isocalendar(), (2003, 52, i+1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
797 d = self.theclass(2003, 12, 29) + timedelta(i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
798 self.assertEqual(d.isocalendar(), (2004, 1, i+1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
799 d = self.theclass(2004, 1, 5+i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
800 self.assertEqual(d.isocalendar(), (2004, 2, i+1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
801 d = self.theclass(2009, 12, 21+i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
802 self.assertEqual(d.isocalendar(), (2009, 52, i+1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
803 d = self.theclass(2009, 12, 28) + timedelta(i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
804 self.assertEqual(d.isocalendar(), (2009, 53, i+1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
805 d = self.theclass(2010, 1, 4+i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
806 self.assertEqual(d.isocalendar(), (2010, 1, i+1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
807 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
808 def test_iso_long_years(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
809 # Calculate long ISO years and compare to table from |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
810 # http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
811 ISO_LONG_YEARS_TABLE = """ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
812 4 32 60 88 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
813 9 37 65 93 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
814 15 43 71 99 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
815 20 48 76 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
816 26 54 82 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
817 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
818 105 133 161 189 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
819 111 139 167 195 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
820 116 144 172 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
821 122 150 178 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
822 128 156 184 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
823 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
824 201 229 257 285 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
825 207 235 263 291 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
826 212 240 268 296 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
827 218 246 274 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
828 224 252 280 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
829 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
830 303 331 359 387 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
831 308 336 364 392 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
832 314 342 370 398 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
833 320 348 376 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
834 325 353 381 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
835 """ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
836 iso_long_years = map(int, ISO_LONG_YEARS_TABLE.split()) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
837 iso_long_years.sort() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
838 L = [] |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
839 for i in range(400): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
840 d = self.theclass(2000+i, 12, 31) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
841 d1 = self.theclass(1600+i, 12, 31) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
842 self.assertEqual(d.isocalendar()[1:], d1.isocalendar()[1:]) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
843 if d.isocalendar()[1] == 53: |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
844 L.append(i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
845 self.assertEqual(L, iso_long_years) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
846 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
847 def test_isoformat(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
848 t = self.theclass(2, 3, 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
849 self.assertEqual(t.isoformat(), "0002-03-02") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
850 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
851 def test_ctime(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
852 t = self.theclass(2002, 3, 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
853 self.assertEqual(t.ctime(), "Sat Mar 2 00:00:00 2002") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
854 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
855 def test_strftime(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
856 t = self.theclass(2005, 3, 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
857 self.assertEqual(t.strftime("m:%m d:%d y:%y"), "m:03 d:02 y:05") |
|
29601
7b519ea7501a
SF bug #761337: datetime.strftime fails on trivial format string
Raymond Hettinger <python@rcn.com>
parents:
29153
diff
changeset
|
858 self.assertEqual(t.strftime(""), "") # SF bug #761337 |
|
39935
b7ae8977d06e
Bug #1556784: allow format strings longer than 127 characters in
Georg Brandl <georg@python.org>
parents:
38036
diff
changeset
|
859 self.assertEqual(t.strftime('x'*1000), 'x'*1000) # SF bug #1556784 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
860 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
861 self.assertRaises(TypeError, t.strftime) # needs an arg |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
862 self.assertRaises(TypeError, t.strftime, "one", "two") # too many args |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
863 self.assertRaises(TypeError, t.strftime, 42) # arg wrong type |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
864 |
|
47440
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45767
diff
changeset
|
865 # test that unicode input is allowed (issue 2782) |
|
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45767
diff
changeset
|
866 self.assertEqual(t.strftime(u"%m"), "03") |
|
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45767
diff
changeset
|
867 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
868 # A naive object replaces %z and %Z w/ empty strings. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
869 self.assertEqual(t.strftime("'%z' '%Z'"), "'' ''") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
870 |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49431
diff
changeset
|
871 #make sure that invalid format specifiers are handled correctly |
|
50897
18441b8f7000
Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50788
diff
changeset
|
872 #self.assertRaises(ValueError, t.strftime, "%e") |
|
18441b8f7000
Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50788
diff
changeset
|
873 #self.assertRaises(ValueError, t.strftime, "%") |
|
18441b8f7000
Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50788
diff
changeset
|
874 #self.assertRaises(ValueError, t.strftime, "%#") |
|
18441b8f7000
Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50788
diff
changeset
|
875 |
|
18441b8f7000
Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50788
diff
changeset
|
876 #oh well, some systems just ignore those invalid ones. |
|
18441b8f7000
Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50788
diff
changeset
|
877 #at least, excercise them to make sure that no crashes |
|
18441b8f7000
Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50788
diff
changeset
|
878 #are generated |
|
18441b8f7000
Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50788
diff
changeset
|
879 for f in ["%e", "%", "%#"]: |
|
18441b8f7000
Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50788
diff
changeset
|
880 try: |
|
18441b8f7000
Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50788
diff
changeset
|
881 t.strftime(f) |
|
18441b8f7000
Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50788
diff
changeset
|
882 except ValueError: |
|
18441b8f7000
Merged revisions 68610,68621-68622,68649 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50788
diff
changeset
|
883 pass |
|
50788
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49431
diff
changeset
|
884 |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49431
diff
changeset
|
885 #check that this standard extension works |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49431
diff
changeset
|
886 t.strftime("%f") |
|
e11c99e1e6e7
Merged revisions 68521,68527,68534-68536,68540,68547,68552,68563,68570,68572,68575,68579-68580,68584 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49431
diff
changeset
|
887 |
|
47440
100e32509b57
Fix issue 2782: be less strict about the format string type in strftime.
Gregory P. Smith <greg@mad-scientist.com>
parents:
45767
diff
changeset
|
888 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
889 def test_format(self): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
890 dt = self.theclass(2007, 9, 10) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
891 self.assertEqual(dt.__format__(''), str(dt)) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
892 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
893 # check that a derived class's __str__() gets called |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
894 class A(self.theclass): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
895 def __str__(self): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
896 return 'A' |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
897 a = A(2007, 9, 10) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
898 self.assertEqual(a.__format__(''), 'A') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
899 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
900 # check that a derived class's strftime gets called |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
901 class B(self.theclass): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
902 def strftime(self, format_spec): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
903 return 'B' |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
904 b = B(2007, 9, 10) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
905 self.assertEqual(b.__format__(''), str(dt)) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
906 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
907 for fmt in ["m:%m d:%d y:%y", |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
908 "m:%m d:%d y:%y H:%H M:%M S:%S", |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
909 "%z %Z", |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
910 ]: |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
911 self.assertEqual(dt.__format__(fmt), dt.strftime(fmt)) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
912 self.assertEqual(a.__format__(fmt), dt.strftime(fmt)) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
913 self.assertEqual(b.__format__(fmt), 'B') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
914 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
915 def test_resolution_info(self): |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
916 self.assertIsInstance(self.theclass.min, self.theclass) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
917 self.assertIsInstance(self.theclass.max, self.theclass) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
918 self.assertIsInstance(self.theclass.resolution, timedelta) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
919 self.assertTrue(self.theclass.max > self.theclass.min) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
920 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
921 def test_extreme_timedelta(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
922 big = self.theclass.max - self.theclass.min |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
923 # 3652058 days, 23 hours, 59 minutes, 59 seconds, 999999 microseconds |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
924 n = (big.days*24*3600 + big.seconds)*1000000 + big.microseconds |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
925 # n == 315537897599999999 ~= 2**58.13 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
926 justasbig = timedelta(0, 0, n) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
927 self.assertEqual(big, justasbig) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
928 self.assertEqual(self.theclass.min + big, self.theclass.max) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
929 self.assertEqual(self.theclass.max - big, self.theclass.min) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
930 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
931 def test_timetuple(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
932 for i in range(7): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
933 # January 2, 1956 is a Monday (0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
934 d = self.theclass(1956, 1, 2+i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
935 t = d.timetuple() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
936 self.assertEqual(t, (1956, 1, 2+i, 0, 0, 0, i, 2+i, -1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
937 # February 1, 1956 is a Wednesday (2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
938 d = self.theclass(1956, 2, 1+i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
939 t = d.timetuple() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
940 self.assertEqual(t, (1956, 2, 1+i, 0, 0, 0, (2+i)%7, 32+i, -1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
941 # March 1, 1956 is a Thursday (3), and is the 31+29+1 = 61st day |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
942 # of the year. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
943 d = self.theclass(1956, 3, 1+i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
944 t = d.timetuple() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
945 self.assertEqual(t, (1956, 3, 1+i, 0, 0, 0, (3+i)%7, 61+i, -1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
946 self.assertEqual(t.tm_year, 1956) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
947 self.assertEqual(t.tm_mon, 3) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
948 self.assertEqual(t.tm_mday, 1+i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
949 self.assertEqual(t.tm_hour, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
950 self.assertEqual(t.tm_min, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
951 self.assertEqual(t.tm_sec, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
952 self.assertEqual(t.tm_wday, (3+i)%7) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
953 self.assertEqual(t.tm_yday, 61+i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
954 self.assertEqual(t.tm_isdst, -1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
955 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
956 def test_pickling(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
957 args = 6, 7, 23 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
958 orig = self.theclass(*args) |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
959 for pickler, unpickler, proto in pickle_choices: |
|
27568
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
960 green = pickler.dumps(orig, proto) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
961 derived = unpickler.loads(green) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
962 self.assertEqual(orig, derived) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
963 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
964 def test_compare(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
965 t1 = self.theclass(2, 3, 4) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
966 t2 = self.theclass(2, 3, 4) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
967 self.assertTrue(t1 == t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
968 self.assertTrue(t1 <= t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
969 self.assertTrue(t1 >= t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
970 self.assertTrue(not t1 != t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
971 self.assertTrue(not t1 < t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
972 self.assertTrue(not t1 > t2) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
973 self.assertEqual(cmp(t1, t2), 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
974 self.assertEqual(cmp(t2, t1), 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
975 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
976 for args in (3, 3, 3), (2, 4, 4), (2, 3, 5): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
977 t2 = self.theclass(*args) # this is larger than t1 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
978 self.assertTrue(t1 < t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
979 self.assertTrue(t2 > t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
980 self.assertTrue(t1 <= t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
981 self.assertTrue(t2 >= t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
982 self.assertTrue(t1 != t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
983 self.assertTrue(t2 != t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
984 self.assertTrue(not t1 == t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
985 self.assertTrue(not t2 == t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
986 self.assertTrue(not t1 > t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
987 self.assertTrue(not t2 < t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
988 self.assertTrue(not t1 >= t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
989 self.assertTrue(not t2 <= t1) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
990 self.assertEqual(cmp(t1, t2), -1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
991 self.assertEqual(cmp(t2, t1), 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
992 |
|
27766
c5ad0a2489b8
The Python implementation of datetime was changed in ways that no longer
Tim Peters <tim.peters@gmail.com>
parents:
27763
diff
changeset
|
993 for badarg in OTHERSTUFF: |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
994 self.assertEqual(t1 == badarg, False) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
995 self.assertEqual(t1 != badarg, True) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
996 self.assertEqual(badarg == t1, False) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
997 self.assertEqual(badarg != t1, True) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
998 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
999 self.assertRaises(TypeError, lambda: t1 < badarg) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1000 self.assertRaises(TypeError, lambda: t1 > badarg) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1001 self.assertRaises(TypeError, lambda: t1 >= badarg) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1002 self.assertRaises(TypeError, lambda: badarg <= t1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1003 self.assertRaises(TypeError, lambda: badarg < t1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1004 self.assertRaises(TypeError, lambda: badarg > t1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1005 self.assertRaises(TypeError, lambda: badarg >= t1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1006 |
|
27365
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1007 def test_mixed_compare(self): |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1008 our = self.theclass(2000, 4, 5) |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1009 self.assertRaises(TypeError, cmp, our, 1) |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1010 self.assertRaises(TypeError, cmp, 1, our) |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1011 |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1012 class AnotherDateTimeClass(object): |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1013 def __cmp__(self, other): |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1014 # Return "equal" so calling this can't be confused with |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1015 # compare-by-address (which never says "equal" for distinct |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1016 # objects). |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1017 return 0 |
|
48213
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47440
diff
changeset
|
1018 __hash__ = None # Silence Py3k warning |
|
27365
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1019 |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1020 # This still errors, because date and datetime comparison raise |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1021 # TypeError instead of NotImplemented when they don't know what to |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1022 # do, in order to stop comparison from falling back to the default |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1023 # compare-by-address. |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1024 their = AnotherDateTimeClass() |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1025 self.assertRaises(TypeError, cmp, our, their) |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1026 # Oops: The next stab raises TypeError in the C implementation, |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1027 # but not in the Python implementation of datetime. The difference |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1028 # is due to that the Python implementation defines __cmp__ but |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1029 # the C implementation defines tp_richcompare. This is more pain |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1030 # to fix than it's worth, so commenting out the test. |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1031 # self.assertEqual(cmp(their, our), 0) |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1032 |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1033 # But date and datetime comparison return NotImplemented instead if the |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1034 # other object has a timetuple attr. This gives the other object a |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1035 # chance to do the comparison. |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1036 class Comparable(AnotherDateTimeClass): |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1037 def timetuple(self): |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1038 return () |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1039 |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1040 their = Comparable() |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1041 self.assertEqual(cmp(our, their), 0) |
|
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1042 self.assertEqual(cmp(their, our), 0) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1043 self.assertTrue(our == their) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1044 self.assertTrue(their == our) |
|
27365
00f294bf8299
date and datetime comparison: when we don't know how to
Tim Peters <tim.peters@gmail.com>
parents:
27351
diff
changeset
|
1045 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1046 def test_bool(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1047 # All dates are considered true. |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1048 self.assertTrue(self.theclass.min) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1049 self.assertTrue(self.theclass.max) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1050 |
|
42774
2c422a4972f1
Fix silly typo in test name.
Guido van Rossum <guido@python.org>
parents:
41535
diff
changeset
|
1051 def test_strftime_out_of_range(self): |
|
26746
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26743
diff
changeset
|
1052 # For nasty technical reasons, we can't handle years before 1900. |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26743
diff
changeset
|
1053 cls = self.theclass |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26743
diff
changeset
|
1054 self.assertEqual(cls(1900, 1, 1).strftime("%Y"), "1900") |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26743
diff
changeset
|
1055 for y in 1, 49, 51, 99, 100, 1000, 1899: |
|
06586404ec4c
I give up: unless I write my own strftime by hand, datetime just can't
Tim Peters <tim.peters@gmail.com>
parents:
26743
diff
changeset
|
1056 self.assertRaises(ValueError, cls(y, 1, 1).strftime, "%Y") |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1057 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1058 def test_replace(self): |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1059 cls = self.theclass |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1060 args = [1, 2, 3] |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1061 base = cls(*args) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1062 self.assertEqual(base, base.replace()) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1063 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1064 i = 0 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1065 for name, newval in (("year", 2), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1066 ("month", 3), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1067 ("day", 4)): |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1068 newargs = args[:] |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1069 newargs[i] = newval |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1070 expected = cls(*newargs) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1071 got = base.replace(**{name: newval}) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1072 self.assertEqual(expected, got) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1073 i += 1 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1074 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1075 # Out of bounds. |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1076 base = cls(2000, 2, 29) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1077 self.assertRaises(ValueError, base.replace, year=2001) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1078 |
|
29149
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1079 def test_subclass_date(self): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1080 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1081 class C(self.theclass): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1082 theAnswer = 42 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1083 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1084 def __new__(cls, *args, **kws): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1085 temp = kws.copy() |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1086 extra = temp.pop('extra') |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1087 result = self.theclass.__new__(cls, *args, **temp) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1088 result.extra = extra |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1089 return result |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1090 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1091 def newmeth(self, start): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1092 return start + self.year + self.month |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1093 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1094 args = 2003, 4, 14 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1095 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1096 dt1 = self.theclass(*args) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1097 dt2 = C(*args, **{'extra': 7}) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1098 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1099 self.assertEqual(dt2.__class__, C) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1100 self.assertEqual(dt2.theAnswer, 42) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1101 self.assertEqual(dt2.extra, 7) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1102 self.assertEqual(dt1.toordinal(), dt2.toordinal()) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1103 self.assertEqual(dt2.newmeth(-7), dt1.year + dt1.month - 7) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1104 |
|
32298
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1105 def test_pickling_subclass_date(self): |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1106 |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1107 args = 6, 7, 23 |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1108 orig = SubclassDate(*args) |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1109 for pickler, unpickler, proto in pickle_choices: |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1110 green = pickler.dumps(orig, proto) |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1111 derived = unpickler.loads(green) |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1112 self.assertEqual(orig, derived) |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1113 |
|
31760
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1114 def test_backdoor_resistance(self): |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1115 # For fast unpickling, the constructor accepts a pickle string. |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1116 # This is a low-overhead backdoor. A user can (by intent or |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1117 # mistake) pass a string directly, which (if it's the right length) |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1118 # will get treated like a pickle, and bypass the normal sanity |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1119 # checks in the constructor. This can create insane objects. |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1120 # The constructor doesn't want to burn the time to validate all |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1121 # fields, but does check the month field. This stops, e.g., |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1122 # datetime.datetime('1995-03-25') from yielding an insane object. |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1123 base = '1995-03-25' |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1124 if not issubclass(self.theclass, datetime): |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1125 base = base[:4] |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1126 for month_byte in '9', chr(0), chr(13), '\xff': |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1127 self.assertRaises(TypeError, self.theclass, |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1128 base[:2] + month_byte + base[3:]) |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1129 for ord_byte in range(1, 13): |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1130 # This shouldn't blow up because of the month byte alone. If |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1131 # the implementation changes to do more-careful checking, it may |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1132 # blow up because other fields are insane. |
|
9536b297d83c
SF bug 847019 datetime.datetime initialization needs more strict checking
Tim Peters <tim.peters@gmail.com>
parents:
29601
diff
changeset
|
1133 self.theclass(base[:2] + chr(ord_byte) + base[3:]) |
|
29143
4332f20533fc
test_subclass_date(): Beefed this up, to check that new instance
Tim Peters <tim.peters@gmail.com>
parents:
28609
diff
changeset
|
1134 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1135 ############################################################################# |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1136 # datetime tests |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1137 |
|
32298
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1138 class SubclassDatetime(datetime): |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1139 sub_var = 1 |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1140 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1141 class TestDateTime(TestDate): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1142 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1143 theclass = datetime |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1144 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1145 def test_basic_attributes(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1146 dt = self.theclass(2002, 3, 1, 12, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1147 self.assertEqual(dt.year, 2002) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1148 self.assertEqual(dt.month, 3) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1149 self.assertEqual(dt.day, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1150 self.assertEqual(dt.hour, 12) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1151 self.assertEqual(dt.minute, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1152 self.assertEqual(dt.second, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1153 self.assertEqual(dt.microsecond, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1154 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1155 def test_basic_attributes_nonzero(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1156 # Make sure all attributes are non-zero so bugs in |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1157 # bit-shifting access show up. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1158 dt = self.theclass(2002, 3, 1, 12, 59, 59, 8000) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1159 self.assertEqual(dt.year, 2002) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1160 self.assertEqual(dt.month, 3) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1161 self.assertEqual(dt.day, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1162 self.assertEqual(dt.hour, 12) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1163 self.assertEqual(dt.minute, 59) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1164 self.assertEqual(dt.second, 59) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1165 self.assertEqual(dt.microsecond, 8000) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1166 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1167 def test_roundtrip(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1168 for dt in (self.theclass(1, 2, 3, 4, 5, 6, 7), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1169 self.theclass.now()): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1170 # Verify dt -> string -> datetime identity. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1171 s = repr(dt) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1172 self.assertTrue(s.startswith('datetime.')) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1173 s = s[9:] |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1174 dt2 = eval(s) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1175 self.assertEqual(dt, dt2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1176 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1177 # Verify identity via reconstructing from pieces. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1178 dt2 = self.theclass(dt.year, dt.month, dt.day, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1179 dt.hour, dt.minute, dt.second, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1180 dt.microsecond) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1181 self.assertEqual(dt, dt2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1182 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1183 def test_isoformat(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1184 t = self.theclass(2, 3, 2, 4, 5, 1, 123) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1185 self.assertEqual(t.isoformat(), "0002-03-02T04:05:01.000123") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1186 self.assertEqual(t.isoformat('T'), "0002-03-02T04:05:01.000123") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1187 self.assertEqual(t.isoformat(' '), "0002-03-02 04:05:01.000123") |
|
57987
8c60a955d02c
#7413: Passing '\0' as the separator to datetime.datetime.isoformat()
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
57461
diff
changeset
|
1188 self.assertEqual(t.isoformat('\x00'), "0002-03-02\x0004:05:01.000123") |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1189 # str is ISO format with the separator forced to a blank. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1190 self.assertEqual(str(t), "0002-03-02 04:05:01.000123") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1191 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1192 t = self.theclass(2, 3, 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1193 self.assertEqual(t.isoformat(), "0002-03-02T00:00:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1194 self.assertEqual(t.isoformat('T'), "0002-03-02T00:00:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1195 self.assertEqual(t.isoformat(' '), "0002-03-02 00:00:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1196 # str is ISO format with the separator forced to a blank. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1197 self.assertEqual(str(t), "0002-03-02 00:00:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1198 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1199 def test_format(self): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1200 dt = self.theclass(2007, 9, 10, 4, 5, 1, 123) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1201 self.assertEqual(dt.__format__(''), str(dt)) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1202 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1203 # check that a derived class's __str__() gets called |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1204 class A(self.theclass): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1205 def __str__(self): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1206 return 'A' |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1207 a = A(2007, 9, 10, 4, 5, 1, 123) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1208 self.assertEqual(a.__format__(''), 'A') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1209 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1210 # check that a derived class's strftime gets called |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1211 class B(self.theclass): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1212 def strftime(self, format_spec): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1213 return 'B' |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1214 b = B(2007, 9, 10, 4, 5, 1, 123) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1215 self.assertEqual(b.__format__(''), str(dt)) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1216 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1217 for fmt in ["m:%m d:%d y:%y", |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1218 "m:%m d:%d y:%y H:%H M:%M S:%S", |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1219 "%z %Z", |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1220 ]: |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1221 self.assertEqual(dt.__format__(fmt), dt.strftime(fmt)) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1222 self.assertEqual(a.__format__(fmt), dt.strftime(fmt)) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1223 self.assertEqual(b.__format__(fmt), 'B') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1224 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1225 def test_more_ctime(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1226 # Test fields that TestDate doesn't touch. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1227 import time |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1228 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1229 t = self.theclass(2002, 3, 2, 18, 3, 5, 123) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1230 self.assertEqual(t.ctime(), "Sat Mar 2 18:03:05 2002") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1231 # Oops! The next line fails on Win2K under MSVC 6, so it's commented |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1232 # out. The difference is that t.ctime() produces " 2" for the day, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1233 # but platform ctime() produces "02" for the day. According to |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1234 # C99, t.ctime() is correct here. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1235 # self.assertEqual(t.ctime(), time.ctime(time.mktime(t.timetuple()))) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1236 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1237 # So test a case where that difference doesn't matter. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1238 t = self.theclass(2002, 3, 22, 18, 3, 5, 123) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1239 self.assertEqual(t.ctime(), time.ctime(time.mktime(t.timetuple()))) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1240 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1241 def test_tz_independent_comparing(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1242 dt1 = self.theclass(2002, 3, 1, 9, 0, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1243 dt2 = self.theclass(2002, 3, 1, 10, 0, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1244 dt3 = self.theclass(2002, 3, 1, 9, 0, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1245 self.assertEqual(dt1, dt3) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1246 self.assertTrue(dt2 > dt3) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1247 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1248 # Make sure comparison doesn't forget microseconds, and isn't done |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1249 # via comparing a float timestamp (an IEEE double doesn't have enough |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1250 # precision to span microsecond resolution across years 1 thru 9999, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1251 # so comparing via timestamp necessarily calls some distinct values |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1252 # equal). |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1253 dt1 = self.theclass(MAXYEAR, 12, 31, 23, 59, 59, 999998) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1254 us = timedelta(microseconds=1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1255 dt2 = dt1 + us |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1256 self.assertEqual(dt2 - dt1, us) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1257 self.assertTrue(dt1 < dt2) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1258 |
|
37088
07ab557625b9
Fix problem spotted by Coverity that occurs if tzinfo.tzname().replace()
Neal Norwitz <nnorwitz@gmail.com>
parents:
35839
diff
changeset
|
1259 def test_strftime_with_bad_tzname_replace(self): |
|
07ab557625b9
Fix problem spotted by Coverity that occurs if tzinfo.tzname().replace()
Neal Norwitz <nnorwitz@gmail.com>
parents:
35839
diff
changeset
|
1260 # verify ok if tzinfo.tzname().replace() returns a non-string |
|
07ab557625b9
Fix problem spotted by Coverity that occurs if tzinfo.tzname().replace()
Neal Norwitz <nnorwitz@gmail.com>
parents:
35839
diff
changeset
|
1261 class MyTzInfo(FixedOffset): |
|
07ab557625b9
Fix problem spotted by Coverity that occurs if tzinfo.tzname().replace()
Neal Norwitz <nnorwitz@gmail.com>
parents:
35839
diff
changeset
|
1262 def tzname(self, dt): |
|
07ab557625b9
Fix problem spotted by Coverity that occurs if tzinfo.tzname().replace()
Neal Norwitz <nnorwitz@gmail.com>
parents:
35839
diff
changeset
|
1263 class MyStr(str): |
|
07ab557625b9
Fix problem spotted by Coverity that occurs if tzinfo.tzname().replace()
Neal Norwitz <nnorwitz@gmail.com>
parents:
35839
diff
changeset
|
1264 def replace(self, *args): |
|
07ab557625b9
Fix problem spotted by Coverity that occurs if tzinfo.tzname().replace()
Neal Norwitz <nnorwitz@gmail.com>
parents:
35839
diff
changeset
|
1265 return None |
|
07ab557625b9
Fix problem spotted by Coverity that occurs if tzinfo.tzname().replace()
Neal Norwitz <nnorwitz@gmail.com>
parents:
35839
diff
changeset
|
1266 return MyStr('name') |
|
07ab557625b9
Fix problem spotted by Coverity that occurs if tzinfo.tzname().replace()
Neal Norwitz <nnorwitz@gmail.com>
parents:
35839
diff
changeset
|
1267 t = self.theclass(2005, 3, 2, 0, 0, 0, 0, MyTzInfo(3, 'name')) |
|
07ab557625b9
Fix problem spotted by Coverity that occurs if tzinfo.tzname().replace()
Neal Norwitz <nnorwitz@gmail.com>
parents:
35839
diff
changeset
|
1268 self.assertRaises(TypeError, t.strftime, '%Z') |
|
07ab557625b9
Fix problem spotted by Coverity that occurs if tzinfo.tzname().replace()
Neal Norwitz <nnorwitz@gmail.com>
parents:
35839
diff
changeset
|
1269 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1270 def test_bad_constructor_arguments(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1271 # bad years |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1272 self.theclass(MINYEAR, 1, 1) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1273 self.theclass(MAXYEAR, 1, 1) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1274 self.assertRaises(ValueError, self.theclass, MINYEAR-1, 1, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1275 self.assertRaises(ValueError, self.theclass, MAXYEAR+1, 1, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1276 # bad months |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1277 self.theclass(2000, 1, 1) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1278 self.theclass(2000, 12, 1) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1279 self.assertRaises(ValueError, self.theclass, 2000, 0, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1280 self.assertRaises(ValueError, self.theclass, 2000, 13, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1281 # bad days |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1282 self.theclass(2000, 2, 29) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1283 self.theclass(2004, 2, 29) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1284 self.theclass(2400, 2, 29) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1285 self.assertRaises(ValueError, self.theclass, 2000, 2, 30) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1286 self.assertRaises(ValueError, self.theclass, 2001, 2, 29) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1287 self.assertRaises(ValueError, self.theclass, 2100, 2, 29) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1288 self.assertRaises(ValueError, self.theclass, 1900, 2, 29) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1289 self.assertRaises(ValueError, self.theclass, 2000, 1, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1290 self.assertRaises(ValueError, self.theclass, 2000, 1, 32) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1291 # bad hours |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1292 self.theclass(2000, 1, 31, 0) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1293 self.theclass(2000, 1, 31, 23) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1294 self.assertRaises(ValueError, self.theclass, 2000, 1, 31, -1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1295 self.assertRaises(ValueError, self.theclass, 2000, 1, 31, 24) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1296 # bad minutes |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1297 self.theclass(2000, 1, 31, 23, 0) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1298 self.theclass(2000, 1, 31, 23, 59) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1299 self.assertRaises(ValueError, self.theclass, 2000, 1, 31, 23, -1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1300 self.assertRaises(ValueError, self.theclass, 2000, 1, 31, 23, 60) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1301 # bad seconds |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1302 self.theclass(2000, 1, 31, 23, 59, 0) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1303 self.theclass(2000, 1, 31, 23, 59, 59) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1304 self.assertRaises(ValueError, self.theclass, 2000, 1, 31, 23, 59, -1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1305 self.assertRaises(ValueError, self.theclass, 2000, 1, 31, 23, 59, 60) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1306 # bad microseconds |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1307 self.theclass(2000, 1, 31, 23, 59, 59, 0) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1308 self.theclass(2000, 1, 31, 23, 59, 59, 999999) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1309 self.assertRaises(ValueError, self.theclass, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1310 2000, 1, 31, 23, 59, 59, -1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1311 self.assertRaises(ValueError, self.theclass, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1312 2000, 1, 31, 23, 59, 59, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1313 1000000) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1314 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1315 def test_hash_equality(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1316 d = self.theclass(2000, 12, 31, 23, 30, 17) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1317 e = self.theclass(2000, 12, 31, 23, 30, 17) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1318 self.assertEqual(d, e) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1319 self.assertEqual(hash(d), hash(e)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1320 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1321 dic = {d: 1} |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1322 dic[e] = 2 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1323 self.assertEqual(len(dic), 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1324 self.assertEqual(dic[d], 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1325 self.assertEqual(dic[e], 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1326 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1327 d = self.theclass(2001, 1, 1, 0, 5, 17) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1328 e = self.theclass(2001, 1, 1, 0, 5, 17) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1329 self.assertEqual(d, e) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1330 self.assertEqual(hash(d), hash(e)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1331 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1332 dic = {d: 1} |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1333 dic[e] = 2 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1334 self.assertEqual(len(dic), 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1335 self.assertEqual(dic[d], 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1336 self.assertEqual(dic[e], 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1337 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1338 def test_computations(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1339 a = self.theclass(2002, 1, 31) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1340 b = self.theclass(1956, 1, 31) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1341 diff = a-b |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1342 self.assertEqual(diff.days, 46*365 + len(range(1956, 2002, 4))) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1343 self.assertEqual(diff.seconds, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1344 self.assertEqual(diff.microseconds, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1345 a = self.theclass(2002, 3, 2, 17, 6) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1346 millisec = timedelta(0, 0, 1000) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1347 hour = timedelta(0, 3600) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1348 day = timedelta(1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1349 week = timedelta(7) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1350 self.assertEqual(a + hour, self.theclass(2002, 3, 2, 18, 6)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1351 self.assertEqual(hour + a, self.theclass(2002, 3, 2, 18, 6)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1352 self.assertEqual(a + 10*hour, self.theclass(2002, 3, 3, 3, 6)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1353 self.assertEqual(a - hour, self.theclass(2002, 3, 2, 16, 6)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1354 self.assertEqual(-hour + a, self.theclass(2002, 3, 2, 16, 6)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1355 self.assertEqual(a - hour, a + -hour) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1356 self.assertEqual(a - 20*hour, self.theclass(2002, 3, 1, 21, 6)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1357 self.assertEqual(a + day, self.theclass(2002, 3, 3, 17, 6)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1358 self.assertEqual(a - day, self.theclass(2002, 3, 1, 17, 6)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1359 self.assertEqual(a + week, self.theclass(2002, 3, 9, 17, 6)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1360 self.assertEqual(a - week, self.theclass(2002, 2, 23, 17, 6)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1361 self.assertEqual(a + 52*week, self.theclass(2003, 3, 1, 17, 6)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1362 self.assertEqual(a - 52*week, self.theclass(2001, 3, 3, 17, 6)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1363 self.assertEqual((a + week) - a, week) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1364 self.assertEqual((a + day) - a, day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1365 self.assertEqual((a + hour) - a, hour) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1366 self.assertEqual((a + millisec) - a, millisec) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1367 self.assertEqual((a - week) - a, -week) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1368 self.assertEqual((a - day) - a, -day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1369 self.assertEqual((a - hour) - a, -hour) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1370 self.assertEqual((a - millisec) - a, -millisec) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1371 self.assertEqual(a - (a + week), -week) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1372 self.assertEqual(a - (a + day), -day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1373 self.assertEqual(a - (a + hour), -hour) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1374 self.assertEqual(a - (a + millisec), -millisec) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1375 self.assertEqual(a - (a - week), week) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1376 self.assertEqual(a - (a - day), day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1377 self.assertEqual(a - (a - hour), hour) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1378 self.assertEqual(a - (a - millisec), millisec) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1379 self.assertEqual(a + (week + day + hour + millisec), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1380 self.theclass(2002, 3, 10, 18, 6, 0, 1000)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1381 self.assertEqual(a + (week + day + hour + millisec), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1382 (((a + week) + day) + hour) + millisec) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1383 self.assertEqual(a - (week + day + hour + millisec), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1384 self.theclass(2002, 2, 22, 16, 5, 59, 999000)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1385 self.assertEqual(a - (week + day + hour + millisec), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1386 (((a - week) - day) - hour) - millisec) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1387 # Add/sub ints, longs, floats should be illegal |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1388 for i in 1, 1L, 1.0: |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1389 self.assertRaises(TypeError, lambda: a+i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1390 self.assertRaises(TypeError, lambda: a-i) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1391 self.assertRaises(TypeError, lambda: i+a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1392 self.assertRaises(TypeError, lambda: i-a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1393 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1394 # delta - datetime is senseless. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1395 self.assertRaises(TypeError, lambda: day - a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1396 # mixing datetime and (delta or datetime) via * or // is senseless |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1397 self.assertRaises(TypeError, lambda: day * a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1398 self.assertRaises(TypeError, lambda: a * day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1399 self.assertRaises(TypeError, lambda: day // a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1400 self.assertRaises(TypeError, lambda: a // day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1401 self.assertRaises(TypeError, lambda: a * a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1402 self.assertRaises(TypeError, lambda: a // a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1403 # datetime + datetime is senseless |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1404 self.assertRaises(TypeError, lambda: a + a) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1405 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1406 def test_pickling(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1407 args = 6, 7, 23, 20, 59, 1, 64**2 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1408 orig = self.theclass(*args) |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
1409 for pickler, unpickler, proto in pickle_choices: |
|
27568
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
1410 green = pickler.dumps(orig, proto) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
1411 derived = unpickler.loads(green) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
1412 self.assertEqual(orig, derived) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1413 |
|
27762
f4cdba83be56
Merge the test part of the below checkin to the sandbox and Zope3, so
Guido van Rossum <guido@python.org>
parents:
27722
diff
changeset
|
1414 def test_more_pickling(self): |
|
f4cdba83be56
Merge the test part of the below checkin to the sandbox and Zope3, so
Guido van Rossum <guido@python.org>
parents:
27722
diff
changeset
|
1415 a = self.theclass(2003, 2, 7, 16, 48, 37, 444116) |
|
f4cdba83be56
Merge the test part of the below checkin to the sandbox and Zope3, so
Guido van Rossum <guido@python.org>
parents:
27722
diff
changeset
|
1416 s = pickle.dumps(a) |
|
f4cdba83be56
Merge the test part of the below checkin to the sandbox and Zope3, so
Guido van Rossum <guido@python.org>
parents:
27722
diff
changeset
|
1417 b = pickle.loads(s) |
|
f4cdba83be56
Merge the test part of the below checkin to the sandbox and Zope3, so
Guido van Rossum <guido@python.org>
parents:
27722
diff
changeset
|
1418 self.assertEqual(b.year, 2003) |
|
f4cdba83be56
Merge the test part of the below checkin to the sandbox and Zope3, so
Guido van Rossum <guido@python.org>
parents:
27722
diff
changeset
|
1419 self.assertEqual(b.month, 2) |
|
f4cdba83be56
Merge the test part of the below checkin to the sandbox and Zope3, so
Guido van Rossum <guido@python.org>
parents:
27722
diff
changeset
|
1420 self.assertEqual(b.day, 7) |
|
f4cdba83be56
Merge the test part of the below checkin to the sandbox and Zope3, so
Guido van Rossum <guido@python.org>
parents:
27722
diff
changeset
|
1421 |
|
32298
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1422 def test_pickling_subclass_datetime(self): |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1423 args = 6, 7, 23, 20, 59, 1, 64**2 |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1424 orig = SubclassDatetime(*args) |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1425 for pickler, unpickler, proto in pickle_choices: |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1426 green = pickler.dumps(orig, proto) |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1427 derived = unpickler.loads(green) |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1428 self.assertEqual(orig, derived) |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1429 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1430 def test_more_compare(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1431 # The test_compare() inherited from TestDate covers the error cases. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1432 # We just want to test lexicographic ordering on the members datetime |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1433 # has that date lacks. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1434 args = [2000, 11, 29, 20, 58, 16, 999998] |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1435 t1 = self.theclass(*args) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1436 t2 = self.theclass(*args) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1437 self.assertTrue(t1 == t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1438 self.assertTrue(t1 <= t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1439 self.assertTrue(t1 >= t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1440 self.assertTrue(not t1 != t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1441 self.assertTrue(not t1 < t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1442 self.assertTrue(not t1 > t2) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1443 self.assertEqual(cmp(t1, t2), 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1444 self.assertEqual(cmp(t2, t1), 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1445 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1446 for i in range(len(args)): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1447 newargs = args[:] |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1448 newargs[i] = args[i] + 1 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1449 t2 = self.theclass(*newargs) # this is larger than t1 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1450 self.assertTrue(t1 < t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1451 self.assertTrue(t2 > t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1452 self.assertTrue(t1 <= t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1453 self.assertTrue(t2 >= t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1454 self.assertTrue(t1 != t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1455 self.assertTrue(t2 != t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1456 self.assertTrue(not t1 == t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1457 self.assertTrue(not t2 == t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1458 self.assertTrue(not t1 > t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1459 self.assertTrue(not t2 < t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1460 self.assertTrue(not t1 >= t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1461 self.assertTrue(not t2 <= t1) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1462 self.assertEqual(cmp(t1, t2), -1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1463 self.assertEqual(cmp(t2, t1), 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1464 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1465 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1466 # A helper for timestamp constructor tests. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1467 def verify_field_equality(self, expected, got): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1468 self.assertEqual(expected.tm_year, got.year) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1469 self.assertEqual(expected.tm_mon, got.month) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1470 self.assertEqual(expected.tm_mday, got.day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1471 self.assertEqual(expected.tm_hour, got.hour) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1472 self.assertEqual(expected.tm_min, got.minute) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1473 self.assertEqual(expected.tm_sec, got.second) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1474 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1475 def test_fromtimestamp(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1476 import time |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1477 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1478 ts = time.time() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1479 expected = time.localtime(ts) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1480 got = self.theclass.fromtimestamp(ts) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1481 self.verify_field_equality(expected, got) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1482 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1483 def test_utcfromtimestamp(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1484 import time |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1485 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1486 ts = time.time() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1487 expected = time.gmtime(ts) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1488 got = self.theclass.utcfromtimestamp(ts) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1489 self.verify_field_equality(expected, got) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1490 |
|
38036
389e922a647c
Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
Georg Brandl <georg@python.org>
parents:
37088
diff
changeset
|
1491 def test_microsecond_rounding(self): |
|
389e922a647c
Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
Georg Brandl <georg@python.org>
parents:
37088
diff
changeset
|
1492 # Test whether fromtimestamp "rounds up" floats that are less |
|
389e922a647c
Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
Georg Brandl <georg@python.org>
parents:
37088
diff
changeset
|
1493 # than one microsecond smaller than an integer. |
|
389e922a647c
Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
Georg Brandl <georg@python.org>
parents:
37088
diff
changeset
|
1494 self.assertEquals(self.theclass.fromtimestamp(0.9999999), |
|
389e922a647c
Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
Georg Brandl <georg@python.org>
parents:
37088
diff
changeset
|
1495 self.theclass.fromtimestamp(1)) |
|
389e922a647c
Bug #1478429: make datetime.datetime.fromtimestamp accept every float,
Georg Brandl <georg@python.org>
parents:
37088
diff
changeset
|
1496 |
|
32388
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1497 def test_insane_fromtimestamp(self): |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1498 # It's possible that some platform maps time_t to double, |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1499 # and that this test will fail there. This test should |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1500 # exempt such platforms (provided they return reasonable |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1501 # results!). |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1502 for insane in -1e200, 1e200: |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1503 self.assertRaises(ValueError, self.theclass.fromtimestamp, |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1504 insane) |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1505 |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1506 def test_insane_utcfromtimestamp(self): |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1507 # It's possible that some platform maps time_t to double, |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1508 # and that this test will fail there. This test should |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1509 # exempt such platforms (provided they return reasonable |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1510 # results!). |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1511 for insane in -1e200, 1e200: |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1512 self.assertRaises(ValueError, self.theclass.utcfromtimestamp, |
|
5cf507b64f31
Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents:
32298
diff
changeset
|
1513 insane) |
|
61942
1307d978789f
Issue #7879: Skip negative timestamps test on any Windows platform
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
61938
diff
changeset
|
1514 @unittest.skipIf(sys.platform == "win32", "Windows doesn't accept negative timestamps") |
|
41026
3cfdb80bc68e
Patch #1646728: datetime.fromtimestamp fails with negative
Guido van Rossum <guido@python.org>
parents:
40771
diff
changeset
|
1515 def test_negative_float_fromtimestamp(self): |
|
3cfdb80bc68e
Patch #1646728: datetime.fromtimestamp fails with negative
Guido van Rossum <guido@python.org>
parents:
40771
diff
changeset
|
1516 # The result is tz-dependent; at least test that this doesn't |
|
3cfdb80bc68e
Patch #1646728: datetime.fromtimestamp fails with negative
Guido van Rossum <guido@python.org>
parents:
40771
diff
changeset
|
1517 # fail (like it did before bug 1646728 was fixed). |
|
3cfdb80bc68e
Patch #1646728: datetime.fromtimestamp fails with negative
Guido van Rossum <guido@python.org>
parents:
40771
diff
changeset
|
1518 self.theclass.fromtimestamp(-1.05) |
|
3cfdb80bc68e
Patch #1646728: datetime.fromtimestamp fails with negative
Guido van Rossum <guido@python.org>
parents:
40771
diff
changeset
|
1519 |
|
61942
1307d978789f
Issue #7879: Skip negative timestamps test on any Windows platform
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
61938
diff
changeset
|
1520 @unittest.skipIf(sys.platform == "win32", "Windows doesn't accept negative timestamps") |
|
41026
3cfdb80bc68e
Patch #1646728: datetime.fromtimestamp fails with negative
Guido van Rossum <guido@python.org>
parents:
40771
diff
changeset
|
1521 def test_negative_float_utcfromtimestamp(self): |
|
3cfdb80bc68e
Patch #1646728: datetime.fromtimestamp fails with negative
Guido van Rossum <guido@python.org>
parents:
40771
diff
changeset
|
1522 d = self.theclass.utcfromtimestamp(-1.05) |
|
3cfdb80bc68e
Patch #1646728: datetime.fromtimestamp fails with negative
Guido van Rossum <guido@python.org>
parents:
40771
diff
changeset
|
1523 self.assertEquals(d, self.theclass(1969, 12, 31, 23, 59, 58, 950000)) |
|
3cfdb80bc68e
Patch #1646728: datetime.fromtimestamp fails with negative
Guido van Rossum <guido@python.org>
parents:
40771
diff
changeset
|
1524 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1525 def test_utcnow(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1526 import time |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1527 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1528 # Call it a success if utcnow() and utcfromtimestamp() are within |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1529 # a second of each other. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1530 tolerance = timedelta(seconds=1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1531 for dummy in range(3): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1532 from_now = self.theclass.utcnow() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1533 from_timestamp = self.theclass.utcfromtimestamp(time.time()) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1534 if abs(from_timestamp - from_now) <= tolerance: |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1535 break |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1536 # Else try again a few times. |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1537 self.assertTrue(abs(from_timestamp - from_now) <= tolerance) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1538 |
|
34416
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
33498
diff
changeset
|
1539 def test_strptime(self): |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45468
diff
changeset
|
1540 import _strptime |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45468
diff
changeset
|
1541 |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45468
diff
changeset
|
1542 string = '2004-12-01 13:02:47.197' |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45468
diff
changeset
|
1543 format = '%Y-%m-%d %H:%M:%S.%f' |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45468
diff
changeset
|
1544 result, frac = _strptime._strptime(string, format) |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45468
diff
changeset
|
1545 expected = self.theclass(*(result[0:6]+(frac,))) |
|
34416
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
33498
diff
changeset
|
1546 got = self.theclass.strptime(string, format) |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
33498
diff
changeset
|
1547 self.assertEqual(expected, got) |
|
563cbf4d6be4
Add strptime() constructor to datetime class. Thanks to Josh Spoerri for
Skip Montanaro <skip@pobox.com>
parents:
33498
diff
changeset
|
1548 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1549 def test_more_timetuple(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1550 # This tests fields beyond those tested by the TestDate.test_timetuple. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1551 t = self.theclass(2004, 12, 31, 6, 22, 33) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1552 self.assertEqual(t.timetuple(), (2004, 12, 31, 6, 22, 33, 4, 366, -1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1553 self.assertEqual(t.timetuple(), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1554 (t.year, t.month, t.day, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1555 t.hour, t.minute, t.second, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1556 t.weekday(), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1557 t.toordinal() - date(t.year, 1, 1).toordinal() + 1, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1558 -1)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1559 tt = t.timetuple() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1560 self.assertEqual(tt.tm_year, t.year) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1561 self.assertEqual(tt.tm_mon, t.month) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1562 self.assertEqual(tt.tm_mday, t.day) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1563 self.assertEqual(tt.tm_hour, t.hour) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1564 self.assertEqual(tt.tm_min, t.minute) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1565 self.assertEqual(tt.tm_sec, t.second) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1566 self.assertEqual(tt.tm_wday, t.weekday()) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1567 self.assertEqual(tt.tm_yday, t.toordinal() - |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1568 date(t.year, 1, 1).toordinal() + 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1569 self.assertEqual(tt.tm_isdst, -1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1570 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1571 def test_more_strftime(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1572 # This tests fields beyond those tested by the TestDate.test_strftime. |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45468
diff
changeset
|
1573 t = self.theclass(2004, 12, 31, 6, 22, 33, 47) |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45468
diff
changeset
|
1574 self.assertEqual(t.strftime("%m %d %y %f %S %M %H %j"), |
|
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45468
diff
changeset
|
1575 "12 31 04 000047 33 22 06 366") |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1576 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1577 def test_extract(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1578 dt = self.theclass(2002, 3, 4, 18, 45, 3, 1234) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1579 self.assertEqual(dt.date(), date(2002, 3, 4)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1580 self.assertEqual(dt.time(), time(18, 45, 3, 1234)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1581 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1582 def test_combine(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1583 d = date(2002, 3, 4) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1584 t = time(18, 45, 3, 1234) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1585 expected = self.theclass(2002, 3, 4, 18, 45, 3, 1234) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1586 combine = self.theclass.combine |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1587 dt = combine(d, t) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1588 self.assertEqual(dt, expected) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1589 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1590 dt = combine(time=t, date=d) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1591 self.assertEqual(dt, expected) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1592 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1593 self.assertEqual(d, dt.date()) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1594 self.assertEqual(t, dt.time()) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1595 self.assertEqual(dt, combine(dt.date(), dt.time())) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1596 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1597 self.assertRaises(TypeError, combine) # need an arg |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1598 self.assertRaises(TypeError, combine, d) # need two args |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1599 self.assertRaises(TypeError, combine, t, d) # args reversed |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1600 self.assertRaises(TypeError, combine, d, t, 1) # too many args |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1601 self.assertRaises(TypeError, combine, "date", "time") # wrong types |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1602 |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1603 def test_replace(self): |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1604 cls = self.theclass |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1605 args = [1, 2, 3, 4, 5, 6, 7] |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1606 base = cls(*args) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1607 self.assertEqual(base, base.replace()) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1608 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1609 i = 0 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1610 for name, newval in (("year", 2), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1611 ("month", 3), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1612 ("day", 4), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1613 ("hour", 5), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1614 ("minute", 6), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1615 ("second", 7), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1616 ("microsecond", 8)): |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1617 newargs = args[:] |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1618 newargs[i] = newval |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1619 expected = cls(*newargs) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1620 got = base.replace(**{name: newval}) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1621 self.assertEqual(expected, got) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1622 i += 1 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1623 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1624 # Out of bounds. |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1625 base = cls(2000, 2, 29) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1626 self.assertRaises(ValueError, base.replace, year=2001) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1627 |
|
26792
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
1628 def test_astimezone(self): |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
1629 # Pretty boring! The TZ test is more interesting here. astimezone() |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
1630 # simply can't be applied to a naive object. |
|
26792
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
1631 dt = self.theclass.now() |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
1632 f = FixedOffset(44, "") |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
1633 self.assertRaises(TypeError, dt.astimezone) # not enough args |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
1634 self.assertRaises(TypeError, dt.astimezone, f, f) # too many args |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
1635 self.assertRaises(TypeError, dt.astimezone, dt) # arg wrong type |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
1636 self.assertRaises(ValueError, dt.astimezone, f) # naive |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
1637 self.assertRaises(ValueError, dt.astimezone, tz=f) # naive |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
1638 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
1639 class Bogus(tzinfo): |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
1640 def utcoffset(self, dt): return None |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
1641 def dst(self, dt): return timedelta(0) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
1642 bog = Bogus() |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
1643 self.assertRaises(ValueError, dt.astimezone, bog) # naive |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
1644 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
1645 class AlsoBogus(tzinfo): |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
1646 def utcoffset(self, dt): return timedelta(0) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
1647 def dst(self, dt): return None |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
1648 alsobog = AlsoBogus() |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
1649 self.assertRaises(ValueError, dt.astimezone, alsobog) # also naive |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1650 |
|
29149
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1651 def test_subclass_datetime(self): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1652 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1653 class C(self.theclass): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1654 theAnswer = 42 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1655 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1656 def __new__(cls, *args, **kws): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1657 temp = kws.copy() |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1658 extra = temp.pop('extra') |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1659 result = self.theclass.__new__(cls, *args, **temp) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1660 result.extra = extra |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1661 return result |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1662 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1663 def newmeth(self, start): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1664 return start + self.year + self.month + self.second |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1665 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1666 args = 2003, 4, 14, 12, 13, 41 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1667 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1668 dt1 = self.theclass(*args) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1669 dt2 = C(*args, **{'extra': 7}) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1670 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1671 self.assertEqual(dt2.__class__, C) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1672 self.assertEqual(dt2.theAnswer, 42) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1673 self.assertEqual(dt2.extra, 7) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1674 self.assertEqual(dt1.toordinal(), dt2.toordinal()) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1675 self.assertEqual(dt2.newmeth(-7), dt1.year + dt1.month + |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1676 dt1.second - 7) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1677 |
|
32298
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1678 class SubclassTime(time): |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1679 sub_var = 1 |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1680 |
|
41534
6c4d7678af5e
Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents:
41061
diff
changeset
|
1681 class TestTime(HarmlessMixedComparison, unittest.TestCase): |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1682 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1683 theclass = time |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1684 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1685 def test_basic_attributes(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1686 t = self.theclass(12, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1687 self.assertEqual(t.hour, 12) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1688 self.assertEqual(t.minute, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1689 self.assertEqual(t.second, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1690 self.assertEqual(t.microsecond, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1691 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1692 def test_basic_attributes_nonzero(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1693 # Make sure all attributes are non-zero so bugs in |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1694 # bit-shifting access show up. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1695 t = self.theclass(12, 59, 59, 8000) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1696 self.assertEqual(t.hour, 12) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1697 self.assertEqual(t.minute, 59) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1698 self.assertEqual(t.second, 59) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1699 self.assertEqual(t.microsecond, 8000) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1700 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1701 def test_roundtrip(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1702 t = self.theclass(1, 2, 3, 4) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1703 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1704 # Verify t -> string -> time identity. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1705 s = repr(t) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1706 self.assertTrue(s.startswith('datetime.')) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1707 s = s[9:] |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1708 t2 = eval(s) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1709 self.assertEqual(t, t2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1710 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1711 # Verify identity via reconstructing from pieces. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1712 t2 = self.theclass(t.hour, t.minute, t.second, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1713 t.microsecond) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1714 self.assertEqual(t, t2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1715 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1716 def test_comparing(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1717 args = [1, 2, 3, 4] |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1718 t1 = self.theclass(*args) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1719 t2 = self.theclass(*args) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1720 self.assertTrue(t1 == t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1721 self.assertTrue(t1 <= t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1722 self.assertTrue(t1 >= t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1723 self.assertTrue(not t1 != t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1724 self.assertTrue(not t1 < t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1725 self.assertTrue(not t1 > t2) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1726 self.assertEqual(cmp(t1, t2), 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1727 self.assertEqual(cmp(t2, t1), 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1728 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1729 for i in range(len(args)): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1730 newargs = args[:] |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1731 newargs[i] = args[i] + 1 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1732 t2 = self.theclass(*newargs) # this is larger than t1 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1733 self.assertTrue(t1 < t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1734 self.assertTrue(t2 > t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1735 self.assertTrue(t1 <= t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1736 self.assertTrue(t2 >= t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1737 self.assertTrue(t1 != t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1738 self.assertTrue(t2 != t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1739 self.assertTrue(not t1 == t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1740 self.assertTrue(not t2 == t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1741 self.assertTrue(not t1 > t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1742 self.assertTrue(not t2 < t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1743 self.assertTrue(not t1 >= t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1744 self.assertTrue(not t2 <= t1) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1745 self.assertEqual(cmp(t1, t2), -1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1746 self.assertEqual(cmp(t2, t1), 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1747 |
|
27766
c5ad0a2489b8
The Python implementation of datetime was changed in ways that no longer
Tim Peters <tim.peters@gmail.com>
parents:
27763
diff
changeset
|
1748 for badarg in OTHERSTUFF: |
|
27763
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
1749 self.assertEqual(t1 == badarg, False) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
1750 self.assertEqual(t1 != badarg, True) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
1751 self.assertEqual(badarg == t1, False) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
1752 self.assertEqual(badarg != t1, True) |
|
fc88174c0c0a
Comparison for timedelta, time, date and datetime objects: __eq__ and
Tim Peters <tim.peters@gmail.com>
parents:
27762
diff
changeset
|
1753 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1754 self.assertRaises(TypeError, lambda: t1 <= badarg) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1755 self.assertRaises(TypeError, lambda: t1 < badarg) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1756 self.assertRaises(TypeError, lambda: t1 > badarg) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1757 self.assertRaises(TypeError, lambda: t1 >= badarg) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1758 self.assertRaises(TypeError, lambda: badarg <= t1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1759 self.assertRaises(TypeError, lambda: badarg < t1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1760 self.assertRaises(TypeError, lambda: badarg > t1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1761 self.assertRaises(TypeError, lambda: badarg >= t1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1762 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1763 def test_bad_constructor_arguments(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1764 # bad hours |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1765 self.theclass(0, 0) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1766 self.theclass(23, 0) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1767 self.assertRaises(ValueError, self.theclass, -1, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1768 self.assertRaises(ValueError, self.theclass, 24, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1769 # bad minutes |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1770 self.theclass(23, 0) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1771 self.theclass(23, 59) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1772 self.assertRaises(ValueError, self.theclass, 23, -1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1773 self.assertRaises(ValueError, self.theclass, 23, 60) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1774 # bad seconds |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1775 self.theclass(23, 59, 0) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1776 self.theclass(23, 59, 59) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1777 self.assertRaises(ValueError, self.theclass, 23, 59, -1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1778 self.assertRaises(ValueError, self.theclass, 23, 59, 60) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1779 # bad microseconds |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1780 self.theclass(23, 59, 59, 0) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1781 self.theclass(23, 59, 59, 999999) # no exception |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1782 self.assertRaises(ValueError, self.theclass, 23, 59, 59, -1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1783 self.assertRaises(ValueError, self.theclass, 23, 59, 59, 1000000) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1784 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1785 def test_hash_equality(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1786 d = self.theclass(23, 30, 17) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1787 e = self.theclass(23, 30, 17) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1788 self.assertEqual(d, e) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1789 self.assertEqual(hash(d), hash(e)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1790 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1791 dic = {d: 1} |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1792 dic[e] = 2 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1793 self.assertEqual(len(dic), 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1794 self.assertEqual(dic[d], 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1795 self.assertEqual(dic[e], 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1796 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1797 d = self.theclass(0, 5, 17) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1798 e = self.theclass(0, 5, 17) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1799 self.assertEqual(d, e) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1800 self.assertEqual(hash(d), hash(e)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1801 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1802 dic = {d: 1} |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1803 dic[e] = 2 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1804 self.assertEqual(len(dic), 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1805 self.assertEqual(dic[d], 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1806 self.assertEqual(dic[e], 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1807 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1808 def test_isoformat(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1809 t = self.theclass(4, 5, 1, 123) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1810 self.assertEqual(t.isoformat(), "04:05:01.000123") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1811 self.assertEqual(t.isoformat(), str(t)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1812 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1813 t = self.theclass() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1814 self.assertEqual(t.isoformat(), "00:00:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1815 self.assertEqual(t.isoformat(), str(t)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1816 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1817 t = self.theclass(microsecond=1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1818 self.assertEqual(t.isoformat(), "00:00:00.000001") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1819 self.assertEqual(t.isoformat(), str(t)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1820 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1821 t = self.theclass(microsecond=10) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1822 self.assertEqual(t.isoformat(), "00:00:00.000010") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1823 self.assertEqual(t.isoformat(), str(t)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1824 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1825 t = self.theclass(microsecond=100) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1826 self.assertEqual(t.isoformat(), "00:00:00.000100") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1827 self.assertEqual(t.isoformat(), str(t)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1828 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1829 t = self.theclass(microsecond=1000) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1830 self.assertEqual(t.isoformat(), "00:00:00.001000") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1831 self.assertEqual(t.isoformat(), str(t)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1832 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1833 t = self.theclass(microsecond=10000) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1834 self.assertEqual(t.isoformat(), "00:00:00.010000") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1835 self.assertEqual(t.isoformat(), str(t)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1836 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1837 t = self.theclass(microsecond=100000) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1838 self.assertEqual(t.isoformat(), "00:00:00.100000") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1839 self.assertEqual(t.isoformat(), str(t)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1840 |
|
40771
e776c83a0c19
Bug #1653736: Complain about keyword arguments to time.isoformat.
Martin v. Löwis <martin@v.loewis.de>
parents:
39935
diff
changeset
|
1841 def test_1653736(self): |
|
e776c83a0c19
Bug #1653736: Complain about keyword arguments to time.isoformat.
Martin v. Löwis <martin@v.loewis.de>
parents:
39935
diff
changeset
|
1842 # verify it doesn't accept extra keyword arguments |
|
e776c83a0c19
Bug #1653736: Complain about keyword arguments to time.isoformat.
Martin v. Löwis <martin@v.loewis.de>
parents:
39935
diff
changeset
|
1843 t = self.theclass(second=1) |
|
e776c83a0c19
Bug #1653736: Complain about keyword arguments to time.isoformat.
Martin v. Löwis <martin@v.loewis.de>
parents:
39935
diff
changeset
|
1844 self.assertRaises(TypeError, t.isoformat, foo=3) |
|
e776c83a0c19
Bug #1653736: Complain about keyword arguments to time.isoformat.
Martin v. Löwis <martin@v.loewis.de>
parents:
39935
diff
changeset
|
1845 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1846 def test_strftime(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1847 t = self.theclass(1, 2, 3, 4) |
|
45767
212e9c8ad087
add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents:
45468
diff
changeset
|
1848 self.assertEqual(t.strftime('%H %M %S %f'), "01 02 03 000004") |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1849 # A naive object replaces %z and %Z with empty strings. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1850 self.assertEqual(t.strftime("'%z' '%Z'"), "'' ''") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1851 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1852 def test_format(self): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1853 t = self.theclass(1, 2, 3, 4) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1854 self.assertEqual(t.__format__(''), str(t)) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1855 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1856 # check that a derived class's __str__() gets called |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1857 class A(self.theclass): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1858 def __str__(self): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1859 return 'A' |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1860 a = A(1, 2, 3, 4) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1861 self.assertEqual(a.__format__(''), 'A') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1862 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1863 # check that a derived class's strftime gets called |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1864 class B(self.theclass): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1865 def strftime(self, format_spec): |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1866 return 'B' |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1867 b = B(1, 2, 3, 4) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1868 self.assertEqual(b.__format__(''), str(t)) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1869 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1870 for fmt in ['%H %M %S', |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1871 ]: |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1872 self.assertEqual(t.__format__(fmt), t.strftime(fmt)) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1873 self.assertEqual(a.__format__(fmt), t.strftime(fmt)) |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1874 self.assertEqual(b.__format__(fmt), 'B') |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
42774
diff
changeset
|
1875 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1876 def test_str(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1877 self.assertEqual(str(self.theclass(1, 2, 3, 4)), "01:02:03.000004") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1878 self.assertEqual(str(self.theclass(10, 2, 3, 4000)), "10:02:03.004000") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1879 self.assertEqual(str(self.theclass(0, 2, 3, 400000)), "00:02:03.400000") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1880 self.assertEqual(str(self.theclass(12, 2, 3, 0)), "12:02:03") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1881 self.assertEqual(str(self.theclass(23, 15, 0, 0)), "23:15:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1882 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1883 def test_repr(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1884 name = 'datetime.' + self.theclass.__name__ |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1885 self.assertEqual(repr(self.theclass(1, 2, 3, 4)), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1886 "%s(1, 2, 3, 4)" % name) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1887 self.assertEqual(repr(self.theclass(10, 2, 3, 4000)), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1888 "%s(10, 2, 3, 4000)" % name) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1889 self.assertEqual(repr(self.theclass(0, 2, 3, 400000)), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1890 "%s(0, 2, 3, 400000)" % name) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1891 self.assertEqual(repr(self.theclass(12, 2, 3, 0)), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1892 "%s(12, 2, 3)" % name) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1893 self.assertEqual(repr(self.theclass(23, 15, 0, 0)), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1894 "%s(23, 15)" % name) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1895 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1896 def test_resolution_info(self): |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
1897 self.assertIsInstance(self.theclass.min, self.theclass) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
1898 self.assertIsInstance(self.theclass.max, self.theclass) |
|
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
1899 self.assertIsInstance(self.theclass.resolution, timedelta) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1900 self.assertTrue(self.theclass.max > self.theclass.min) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1901 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1902 def test_pickling(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1903 args = 20, 59, 16, 64**2 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1904 orig = self.theclass(*args) |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
1905 for pickler, unpickler, proto in pickle_choices: |
|
27568
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
1906 green = pickler.dumps(orig, proto) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
1907 derived = unpickler.loads(green) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
1908 self.assertEqual(orig, derived) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1909 |
|
32298
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1910 def test_pickling_subclass_time(self): |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1911 args = 20, 59, 16, 64**2 |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1912 orig = SubclassTime(*args) |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1913 for pickler, unpickler, proto in pickle_choices: |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1914 green = pickler.dumps(orig, proto) |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1915 derived = unpickler.loads(green) |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1916 self.assertEqual(orig, derived) |
|
29d761c5103e
SF 952807: Unpickling pickled instances of subclasses of datetime.date,
Tim Peters <tim.peters@gmail.com>
parents:
31760
diff
changeset
|
1917 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1918 def test_bool(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1919 cls = self.theclass |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1920 self.assertTrue(cls(1)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1921 self.assertTrue(cls(0, 1)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1922 self.assertTrue(cls(0, 0, 1)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1923 self.assertTrue(cls(0, 0, 0, 1)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1924 self.assertTrue(not cls(0)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
1925 self.assertTrue(not cls()) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1926 |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1927 def test_replace(self): |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1928 cls = self.theclass |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1929 args = [1, 2, 3, 4] |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1930 base = cls(*args) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1931 self.assertEqual(base, base.replace()) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1932 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1933 i = 0 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1934 for name, newval in (("hour", 5), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1935 ("minute", 6), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1936 ("second", 7), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1937 ("microsecond", 8)): |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1938 newargs = args[:] |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1939 newargs[i] = newval |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1940 expected = cls(*newargs) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1941 got = base.replace(**{name: newval}) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1942 self.assertEqual(expected, got) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1943 i += 1 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1944 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1945 # Out of bounds. |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1946 base = cls(1) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1947 self.assertRaises(ValueError, base.replace, hour=24) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1948 self.assertRaises(ValueError, base.replace, minute=-1) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1949 self.assertRaises(ValueError, base.replace, second=100) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1950 self.assertRaises(ValueError, base.replace, microsecond=1000000) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
1951 |
|
29149
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1952 def test_subclass_time(self): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1953 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1954 class C(self.theclass): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1955 theAnswer = 42 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1956 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1957 def __new__(cls, *args, **kws): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1958 temp = kws.copy() |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1959 extra = temp.pop('extra') |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1960 result = self.theclass.__new__(cls, *args, **temp) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1961 result.extra = extra |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1962 return result |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1963 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1964 def newmeth(self, start): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1965 return start + self.hour + self.second |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1966 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1967 args = 4, 5, 6 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1968 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1969 dt1 = self.theclass(*args) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1970 dt2 = C(*args, **{'extra': 7}) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1971 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1972 self.assertEqual(dt2.__class__, C) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1973 self.assertEqual(dt2.theAnswer, 42) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1974 self.assertEqual(dt2.extra, 7) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1975 self.assertEqual(dt1.isoformat(), dt2.isoformat()) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1976 self.assertEqual(dt2.newmeth(-7), dt1.hour + dt1.second - 7) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
1977 |
|
35839
de090bf67a59
similar to SF bug 847019: a quick check in the time() constructor, which
Armin Rigo <arigo@tunes.org>
parents:
34423
diff
changeset
|
1978 def test_backdoor_resistance(self): |
|
de090bf67a59
similar to SF bug 847019: a quick check in the time() constructor, which
Armin Rigo <arigo@tunes.org>
parents:
34423
diff
changeset
|
1979 # see TestDate.test_backdoor_resistance(). |
|
de090bf67a59
similar to SF bug 847019: a quick check in the time() constructor, which
Armin Rigo <arigo@tunes.org>
parents:
34423
diff
changeset
|
1980 base = '2:59.0' |
|
de090bf67a59
similar to SF bug 847019: a quick check in the time() constructor, which
Armin Rigo <arigo@tunes.org>
parents:
34423
diff
changeset
|
1981 for hour_byte in ' ', '9', chr(24), '\xff': |
|
de090bf67a59
similar to SF bug 847019: a quick check in the time() constructor, which
Armin Rigo <arigo@tunes.org>
parents:
34423
diff
changeset
|
1982 self.assertRaises(TypeError, self.theclass, |
|
de090bf67a59
similar to SF bug 847019: a quick check in the time() constructor, which
Armin Rigo <arigo@tunes.org>
parents:
34423
diff
changeset
|
1983 hour_byte + base[1:]) |
|
de090bf67a59
similar to SF bug 847019: a quick check in the time() constructor, which
Armin Rigo <arigo@tunes.org>
parents:
34423
diff
changeset
|
1984 |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
1985 # A mixin for classes with a tzinfo= argument. Subclasses must define |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
1986 # theclass as a class atribute, and theclass(1, 1, 1, tzinfo=whatever) |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
1987 # must be legit (which is true for time and datetime). |
|
41534
6c4d7678af5e
Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents:
41061
diff
changeset
|
1988 class TZInfoBase: |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1989 |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
1990 def test_argument_passing(self): |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
1991 cls = self.theclass |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
1992 # A datetime passes itself on, a time passes None. |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
1993 class introspective(tzinfo): |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
1994 def tzname(self, dt): return dt and "real" or "none" |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
1995 def utcoffset(self, dt): |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
1996 return timedelta(minutes = dt and 42 or -42) |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
1997 dst = utcoffset |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
1998 |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
1999 obj = cls(1, 2, 3, tzinfo=introspective()) |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2000 |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2001 expected = cls is time and "none" or "real" |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2002 self.assertEqual(obj.tzname(), expected) |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2003 |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2004 expected = timedelta(minutes=(cls is time and -42 or 42)) |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2005 self.assertEqual(obj.utcoffset(), expected) |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2006 self.assertEqual(obj.dst(), expected) |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2007 |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2008 def test_bad_tzinfo_classes(self): |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2009 cls = self.theclass |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2010 self.assertRaises(TypeError, cls, 1, 1, 1, tzinfo=12) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2011 |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2012 class NiceTry(object): |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2013 def __init__(self): pass |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2014 def utcoffset(self, dt): pass |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2015 self.assertRaises(TypeError, cls, 1, 1, 1, tzinfo=NiceTry) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2016 |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2017 class BetterTry(tzinfo): |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2018 def __init__(self): pass |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2019 def utcoffset(self, dt): pass |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2020 b = BetterTry() |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2021 t = cls(1, 1, 1, tzinfo=b) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2022 self.assertTrue(t.tzinfo is b) |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2023 |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2024 def test_utc_offset_out_of_bounds(self): |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2025 class Edgy(tzinfo): |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2026 def __init__(self, offset): |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2027 self.offset = timedelta(minutes=offset) |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2028 def utcoffset(self, dt): |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2029 return self.offset |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2030 |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2031 cls = self.theclass |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2032 for offset, legit in ((-1440, False), |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2033 (-1439, True), |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2034 (1439, True), |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2035 (1440, False)): |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2036 if cls is time: |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2037 t = cls(1, 2, 3, tzinfo=Edgy(offset)) |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2038 elif cls is datetime: |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2039 t = cls(6, 6, 6, 1, 2, 3, tzinfo=Edgy(offset)) |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2040 else: |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2041 assert 0, "impossible" |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2042 if legit: |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2043 aofs = abs(offset) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2044 h, m = divmod(aofs, 60) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2045 tag = "%c%02d:%02d" % (offset < 0 and '-' or '+', h, m) |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2046 if isinstance(t, datetime): |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2047 t = t.timetz() |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2048 self.assertEqual(str(t), "01:02:03" + tag) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2049 else: |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2050 self.assertRaises(ValueError, str, t) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2051 |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2052 def test_tzinfo_classes(self): |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2053 cls = self.theclass |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2054 class C1(tzinfo): |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2055 def utcoffset(self, dt): return None |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2056 def dst(self, dt): return None |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2057 def tzname(self, dt): return None |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2058 for t in (cls(1, 1, 1), |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2059 cls(1, 1, 1, tzinfo=None), |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2060 cls(1, 1, 1, tzinfo=C1())): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2061 self.assertTrue(t.utcoffset() is None) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2062 self.assertTrue(t.dst() is None) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2063 self.assertTrue(t.tzname() is None) |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2064 |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2065 class C3(tzinfo): |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2066 def utcoffset(self, dt): return timedelta(minutes=-1439) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2067 def dst(self, dt): return timedelta(minutes=1439) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2068 def tzname(self, dt): return "aname" |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2069 t = cls(1, 1, 1, tzinfo=C3()) |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2070 self.assertEqual(t.utcoffset(), timedelta(minutes=-1439)) |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2071 self.assertEqual(t.dst(), timedelta(minutes=1439)) |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2072 self.assertEqual(t.tzname(), "aname") |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2073 |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2074 # Wrong types. |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2075 class C4(tzinfo): |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2076 def utcoffset(self, dt): return "aname" |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2077 def dst(self, dt): return 7 |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2078 def tzname(self, dt): return 0 |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2079 t = cls(1, 1, 1, tzinfo=C4()) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2080 self.assertRaises(TypeError, t.utcoffset) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2081 self.assertRaises(TypeError, t.dst) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2082 self.assertRaises(TypeError, t.tzname) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2083 |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2084 # Offset out of range. |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2085 class C6(tzinfo): |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2086 def utcoffset(self, dt): return timedelta(hours=-24) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2087 def dst(self, dt): return timedelta(hours=24) |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2088 t = cls(1, 1, 1, tzinfo=C6()) |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2089 self.assertRaises(ValueError, t.utcoffset) |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2090 self.assertRaises(ValueError, t.dst) |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2091 |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2092 # Not a whole number of minutes. |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2093 class C7(tzinfo): |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2094 def utcoffset(self, dt): return timedelta(seconds=61) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2095 def dst(self, dt): return timedelta(microseconds=-81) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2096 t = cls(1, 1, 1, tzinfo=C7()) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2097 self.assertRaises(ValueError, t.utcoffset) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2098 self.assertRaises(ValueError, t.dst) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2099 |
|
26797
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2100 def test_aware_compare(self): |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2101 cls = self.theclass |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2102 |
|
26807
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2103 # Ensure that utcoffset() gets ignored if the comparands have |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2104 # the same tzinfo member. |
|
26797
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2105 class OperandDependentOffset(tzinfo): |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2106 def utcoffset(self, t): |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2107 if t.minute < 10: |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2108 # d0 and d1 equal after adjustment |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2109 return timedelta(minutes=t.minute) |
|
26797
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2110 else: |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2111 # d2 off in the weeds |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2112 return timedelta(minutes=59) |
|
26797
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2113 |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2114 base = cls(8, 9, 10, tzinfo=OperandDependentOffset()) |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2115 d0 = base.replace(minute=3) |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2116 d1 = base.replace(minute=9) |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2117 d2 = base.replace(minute=11) |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2118 for x in d0, d1, d2: |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2119 for y in d0, d1, d2: |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2120 got = cmp(x, y) |
|
26807
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2121 expected = cmp(x.minute, y.minute) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2122 self.assertEqual(got, expected) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2123 |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2124 # However, if they're different members, uctoffset is not ignored. |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2125 # Note that a time can't actually have an operand-depedent offset, |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2126 # though (and time.utcoffset() passes None to tzinfo.utcoffset()), |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2127 # so skip this test for time. |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2128 if cls is not time: |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2129 d0 = base.replace(minute=3, tzinfo=OperandDependentOffset()) |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2130 d1 = base.replace(minute=9, tzinfo=OperandDependentOffset()) |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2131 d2 = base.replace(minute=11, tzinfo=OperandDependentOffset()) |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2132 for x in d0, d1, d2: |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2133 for y in d0, d1, d2: |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2134 got = cmp(x, y) |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2135 if (x is d0 or x is d1) and (y is d0 or y is d1): |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2136 expected = 0 |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2137 elif x is y is d2: |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2138 expected = 0 |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2139 elif x is d2: |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2140 expected = -1 |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2141 else: |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2142 assert y is d2 |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2143 expected = 1 |
|
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
2144 self.assertEqual(got, expected) |
|
26797
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2145 |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2146 |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2147 # Testing time objects with a non-None tzinfo. |
|
41534
6c4d7678af5e
Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents:
41061
diff
changeset
|
2148 class TestTimeTZ(TestTime, TZInfoBase, unittest.TestCase): |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2149 theclass = time |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2150 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2151 def test_empty(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2152 t = self.theclass() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2153 self.assertEqual(t.hour, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2154 self.assertEqual(t.minute, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2155 self.assertEqual(t.second, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2156 self.assertEqual(t.microsecond, 0) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2157 self.assertTrue(t.tzinfo is None) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2158 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2159 def test_zones(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2160 est = FixedOffset(-300, "EST", 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2161 utc = FixedOffset(0, "UTC", -2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2162 met = FixedOffset(60, "MET", 3) |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2163 t1 = time( 7, 47, tzinfo=est) |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2164 t2 = time(12, 47, tzinfo=utc) |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2165 t3 = time(13, 47, tzinfo=met) |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2166 t4 = time(microsecond=40) |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2167 t5 = time(microsecond=40, tzinfo=utc) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2168 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2169 self.assertEqual(t1.tzinfo, est) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2170 self.assertEqual(t2.tzinfo, utc) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2171 self.assertEqual(t3.tzinfo, met) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2172 self.assertTrue(t4.tzinfo is None) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2173 self.assertEqual(t5.tzinfo, utc) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2174 |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2175 self.assertEqual(t1.utcoffset(), timedelta(minutes=-300)) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2176 self.assertEqual(t2.utcoffset(), timedelta(minutes=0)) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2177 self.assertEqual(t3.utcoffset(), timedelta(minutes=60)) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2178 self.assertTrue(t4.utcoffset() is None) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2179 self.assertRaises(TypeError, t1.utcoffset, "no args") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2180 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2181 self.assertEqual(t1.tzname(), "EST") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2182 self.assertEqual(t2.tzname(), "UTC") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2183 self.assertEqual(t3.tzname(), "MET") |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2184 self.assertTrue(t4.tzname() is None) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2185 self.assertRaises(TypeError, t1.tzname, "no args") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2186 |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2187 self.assertEqual(t1.dst(), timedelta(minutes=1)) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2188 self.assertEqual(t2.dst(), timedelta(minutes=-2)) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2189 self.assertEqual(t3.dst(), timedelta(minutes=3)) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2190 self.assertTrue(t4.dst() is None) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2191 self.assertRaises(TypeError, t1.dst, "no args") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2192 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2193 self.assertEqual(hash(t1), hash(t2)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2194 self.assertEqual(hash(t1), hash(t3)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2195 self.assertEqual(hash(t2), hash(t3)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2196 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2197 self.assertEqual(t1, t2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2198 self.assertEqual(t1, t3) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2199 self.assertEqual(t2, t3) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2200 self.assertRaises(TypeError, lambda: t4 == t5) # mixed tz-aware & naive |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2201 self.assertRaises(TypeError, lambda: t4 < t5) # mixed tz-aware & naive |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2202 self.assertRaises(TypeError, lambda: t5 < t4) # mixed tz-aware & naive |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2203 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2204 self.assertEqual(str(t1), "07:47:00-05:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2205 self.assertEqual(str(t2), "12:47:00+00:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2206 self.assertEqual(str(t3), "13:47:00+01:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2207 self.assertEqual(str(t4), "00:00:00.000040") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2208 self.assertEqual(str(t5), "00:00:00.000040+00:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2209 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2210 self.assertEqual(t1.isoformat(), "07:47:00-05:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2211 self.assertEqual(t2.isoformat(), "12:47:00+00:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2212 self.assertEqual(t3.isoformat(), "13:47:00+01:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2213 self.assertEqual(t4.isoformat(), "00:00:00.000040") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2214 self.assertEqual(t5.isoformat(), "00:00:00.000040+00:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2215 |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2216 d = 'datetime.time' |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2217 self.assertEqual(repr(t1), d + "(7, 47, tzinfo=est)") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2218 self.assertEqual(repr(t2), d + "(12, 47, tzinfo=utc)") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2219 self.assertEqual(repr(t3), d + "(13, 47, tzinfo=met)") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2220 self.assertEqual(repr(t4), d + "(0, 0, 0, 40)") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2221 self.assertEqual(repr(t5), d + "(0, 0, 0, 40, tzinfo=utc)") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2222 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2223 self.assertEqual(t1.strftime("%H:%M:%S %%Z=%Z %%z=%z"), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2224 "07:47:00 %Z=EST %z=-0500") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2225 self.assertEqual(t2.strftime("%H:%M:%S %Z %z"), "12:47:00 UTC +0000") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2226 self.assertEqual(t3.strftime("%H:%M:%S %Z %z"), "13:47:00 MET +0100") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2227 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2228 yuck = FixedOffset(-1439, "%z %Z %%z%%Z") |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2229 t1 = time(23, 59, tzinfo=yuck) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2230 self.assertEqual(t1.strftime("%H:%M %%Z='%Z' %%z='%z'"), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2231 "23:59 %Z='%z %Z %%z%%Z' %z='-2359'") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2232 |
|
26738
78e857e9dd47
Added test to ensure that non-string result from dst() raises TypeError.
Tim Peters <tim.peters@gmail.com>
parents:
26737
diff
changeset
|
2233 # Check that an invalid tzname result raises an exception. |
|
78e857e9dd47
Added test to ensure that non-string result from dst() raises TypeError.
Tim Peters <tim.peters@gmail.com>
parents:
26737
diff
changeset
|
2234 class Badtzname(tzinfo): |
|
78e857e9dd47
Added test to ensure that non-string result from dst() raises TypeError.
Tim Peters <tim.peters@gmail.com>
parents:
26737
diff
changeset
|
2235 def tzname(self, dt): return 42 |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2236 t = time(2, 3, 4, tzinfo=Badtzname()) |
|
26738
78e857e9dd47
Added test to ensure that non-string result from dst() raises TypeError.
Tim Peters <tim.peters@gmail.com>
parents:
26737
diff
changeset
|
2237 self.assertEqual(t.strftime("%H:%M:%S"), "02:03:04") |
|
78e857e9dd47
Added test to ensure that non-string result from dst() raises TypeError.
Tim Peters <tim.peters@gmail.com>
parents:
26737
diff
changeset
|
2238 self.assertRaises(TypeError, t.strftime, "%Z") |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2239 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2240 def test_hash_edge_cases(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2241 # Offsets that overflow a basic time. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2242 t1 = self.theclass(0, 1, 2, 3, tzinfo=FixedOffset(1439, "")) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2243 t2 = self.theclass(0, 0, 2, 3, tzinfo=FixedOffset(1438, "")) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2244 self.assertEqual(hash(t1), hash(t2)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2245 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2246 t1 = self.theclass(23, 58, 6, 100, tzinfo=FixedOffset(-1000, "")) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2247 t2 = self.theclass(23, 48, 6, 100, tzinfo=FixedOffset(-1010, "")) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2248 self.assertEqual(hash(t1), hash(t2)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2249 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2250 def test_pickling(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2251 # Try one without a tzinfo. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2252 args = 20, 59, 16, 64**2 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2253 orig = self.theclass(*args) |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2254 for pickler, unpickler, proto in pickle_choices: |
|
27568
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
2255 green = pickler.dumps(orig, proto) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
2256 derived = unpickler.loads(green) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
2257 self.assertEqual(orig, derived) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2258 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2259 # Try one with a tzinfo. |
|
26737
6f1f84203ecf
Changes sufficient so that pickles written by the Python implementation
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
2260 tinfo = PicklableFixedOffset(-300, 'cookie') |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2261 orig = self.theclass(5, 6, 7, tzinfo=tinfo) |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2262 for pickler, unpickler, proto in pickle_choices: |
|
27568
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
2263 green = pickler.dumps(orig, proto) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
2264 derived = unpickler.loads(green) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
2265 self.assertEqual(orig, derived) |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
2266 self.assertIsInstance(derived.tzinfo, PicklableFixedOffset) |
|
27568
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
2267 self.assertEqual(derived.utcoffset(), timedelta(minutes=-300)) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
2268 self.assertEqual(derived.tzname(), 'cookie') |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2269 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2270 def test_more_bool(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2271 # Test cases with non-None tzinfo. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2272 cls = self.theclass |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2273 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2274 t = cls(0, tzinfo=FixedOffset(-300, "")) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2275 self.assertTrue(t) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2276 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2277 t = cls(5, tzinfo=FixedOffset(-300, "")) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2278 self.assertTrue(t) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2279 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2280 t = cls(5, tzinfo=FixedOffset(300, "")) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2281 self.assertTrue(not t) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2282 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2283 t = cls(23, 59, tzinfo=FixedOffset(23*60 + 59, "")) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2284 self.assertTrue(not t) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2285 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2286 # Mostly ensuring this doesn't overflow internally. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2287 t = cls(0, tzinfo=FixedOffset(23*60 + 59, "")) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2288 self.assertTrue(t) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2289 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2290 # But this should yield a value error -- the utcoffset is bogus. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2291 t = cls(0, tzinfo=FixedOffset(24*60, "")) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2292 self.assertRaises(ValueError, lambda: bool(t)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2293 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2294 # Likewise. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2295 t = cls(0, tzinfo=FixedOffset(-24*60, "")) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2296 self.assertRaises(ValueError, lambda: bool(t)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2297 |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2298 def test_replace(self): |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2299 cls = self.theclass |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2300 z100 = FixedOffset(100, "+100") |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2301 zm200 = FixedOffset(timedelta(minutes=-200), "-200") |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2302 args = [1, 2, 3, 4, z100] |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2303 base = cls(*args) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2304 self.assertEqual(base, base.replace()) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2305 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2306 i = 0 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2307 for name, newval in (("hour", 5), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2308 ("minute", 6), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2309 ("second", 7), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2310 ("microsecond", 8), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2311 ("tzinfo", zm200)): |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2312 newargs = args[:] |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2313 newargs[i] = newval |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2314 expected = cls(*newargs) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2315 got = base.replace(**{name: newval}) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2316 self.assertEqual(expected, got) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2317 i += 1 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2318 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2319 # Ensure we can get rid of a tzinfo. |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2320 self.assertEqual(base.tzname(), "+100") |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2321 base2 = base.replace(tzinfo=None) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2322 self.assertTrue(base2.tzinfo is None) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2323 self.assertTrue(base2.tzname() is None) |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2324 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2325 # Ensure we can add one. |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2326 base3 = base2.replace(tzinfo=z100) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2327 self.assertEqual(base, base3) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2328 self.assertTrue(base.tzinfo is base3.tzinfo) |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2329 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2330 # Out of bounds. |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2331 base = cls(1) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2332 self.assertRaises(ValueError, base.replace, hour=24) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2333 self.assertRaises(ValueError, base.replace, minute=-1) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2334 self.assertRaises(ValueError, base.replace, second=100) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2335 self.assertRaises(ValueError, base.replace, microsecond=1000000) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2336 |
|
26807
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2337 def test_mixed_compare(self): |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2338 t1 = time(1, 2, 3) |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2339 t2 = time(1, 2, 3) |
|
26807
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2340 self.assertEqual(t1, t2) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2341 t2 = t2.replace(tzinfo=None) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2342 self.assertEqual(t1, t2) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2343 t2 = t2.replace(tzinfo=FixedOffset(None, "")) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2344 self.assertEqual(t1, t2) |
|
27766
c5ad0a2489b8
The Python implementation of datetime was changed in ways that no longer
Tim Peters <tim.peters@gmail.com>
parents:
27763
diff
changeset
|
2345 t2 = t2.replace(tzinfo=FixedOffset(0, "")) |
|
c5ad0a2489b8
The Python implementation of datetime was changed in ways that no longer
Tim Peters <tim.peters@gmail.com>
parents:
27763
diff
changeset
|
2346 self.assertRaises(TypeError, lambda: t1 == t2) |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2347 |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2348 # In time w/ identical tzinfo objects, utcoffset is ignored. |
|
26807
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2349 class Varies(tzinfo): |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2350 def __init__(self): |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2351 self.offset = timedelta(minutes=22) |
|
26807
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2352 def utcoffset(self, t): |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2353 self.offset += timedelta(minutes=1) |
|
26807
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2354 return self.offset |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2355 |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2356 v = Varies() |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2357 t1 = t2.replace(tzinfo=v) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2358 t2 = t2.replace(tzinfo=v) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2359 self.assertEqual(t1.utcoffset(), timedelta(minutes=23)) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2360 self.assertEqual(t2.utcoffset(), timedelta(minutes=24)) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2361 self.assertEqual(t1, t2) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2362 |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2363 # But if they're not identical, it isn't ignored. |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2364 t2 = t2.replace(tzinfo=Varies()) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2365 self.assertTrue(t1 < t2) # t1's offset counter still going up |
|
26807
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2366 |
|
29149
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2367 def test_subclass_timetz(self): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2368 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2369 class C(self.theclass): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2370 theAnswer = 42 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2371 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2372 def __new__(cls, *args, **kws): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2373 temp = kws.copy() |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2374 extra = temp.pop('extra') |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2375 result = self.theclass.__new__(cls, *args, **temp) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2376 result.extra = extra |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2377 return result |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2378 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2379 def newmeth(self, start): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2380 return start + self.hour + self.second |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2381 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2382 args = 4, 5, 6, 500, FixedOffset(-300, "EST", 1) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2383 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2384 dt1 = self.theclass(*args) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2385 dt2 = C(*args, **{'extra': 7}) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2386 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2387 self.assertEqual(dt2.__class__, C) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2388 self.assertEqual(dt2.theAnswer, 42) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2389 self.assertEqual(dt2.extra, 7) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2390 self.assertEqual(dt1.utcoffset(), dt2.utcoffset()) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2391 self.assertEqual(dt2.newmeth(-7), dt1.hour + dt1.second - 7) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2392 |
|
26797
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2393 |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2394 # Testing datetime objects with a non-None tzinfo. |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2395 |
|
41534
6c4d7678af5e
Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents:
41061
diff
changeset
|
2396 class TestDateTimeTZ(TestDateTime, TZInfoBase, unittest.TestCase): |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2397 theclass = datetime |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2398 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2399 def test_trivial(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2400 dt = self.theclass(1, 2, 3, 4, 5, 6, 7) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2401 self.assertEqual(dt.year, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2402 self.assertEqual(dt.month, 2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2403 self.assertEqual(dt.day, 3) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2404 self.assertEqual(dt.hour, 4) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2405 self.assertEqual(dt.minute, 5) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2406 self.assertEqual(dt.second, 6) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2407 self.assertEqual(dt.microsecond, 7) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2408 self.assertEqual(dt.tzinfo, None) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2409 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2410 def test_even_more_compare(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2411 # The test_compare() and test_more_compare() inherited from TestDate |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2412 # and TestDateTime covered non-tzinfo cases. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2413 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2414 # Smallest possible after UTC adjustment. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2415 t1 = self.theclass(1, 1, 1, tzinfo=FixedOffset(1439, "")) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2416 # Largest possible after UTC adjustment. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2417 t2 = self.theclass(MAXYEAR, 12, 31, 23, 59, 59, 999999, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2418 tzinfo=FixedOffset(-1439, "")) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2419 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2420 # Make sure those compare correctly, and w/o overflow. |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2421 self.assertTrue(t1 < t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2422 self.assertTrue(t1 != t2) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2423 self.assertTrue(t2 > t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2424 |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2425 self.assertTrue(t1 == t1) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2426 self.assertTrue(t2 == t2) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2427 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2428 # Equal afer adjustment. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2429 t1 = self.theclass(1, 12, 31, 23, 59, tzinfo=FixedOffset(1, "")) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2430 t2 = self.theclass(2, 1, 1, 3, 13, tzinfo=FixedOffset(3*60+13+2, "")) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2431 self.assertEqual(t1, t2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2432 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2433 # Change t1 not to subtract a minute, and t1 should be larger. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2434 t1 = self.theclass(1, 12, 31, 23, 59, tzinfo=FixedOffset(0, "")) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2435 self.assertTrue(t1 > t2) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2436 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2437 # Change t1 to subtract 2 minutes, and t1 should be smaller. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2438 t1 = self.theclass(1, 12, 31, 23, 59, tzinfo=FixedOffset(2, "")) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2439 self.assertTrue(t1 < t2) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2440 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2441 # Back to the original t1, but make seconds resolve it. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2442 t1 = self.theclass(1, 12, 31, 23, 59, tzinfo=FixedOffset(1, ""), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2443 second=1) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2444 self.assertTrue(t1 > t2) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2445 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2446 # Likewise, but make microseconds resolve it. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2447 t1 = self.theclass(1, 12, 31, 23, 59, tzinfo=FixedOffset(1, ""), |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2448 microsecond=1) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2449 self.assertTrue(t1 > t2) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2450 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2451 # Make t2 naive and it should fail. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2452 t2 = self.theclass.min |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2453 self.assertRaises(TypeError, lambda: t1 == t2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2454 self.assertEqual(t2, t2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2455 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2456 # It's also naive if it has tzinfo but tzinfo.utcoffset() is None. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2457 class Naive(tzinfo): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2458 def utcoffset(self, dt): return None |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2459 t2 = self.theclass(5, 6, 7, tzinfo=Naive()) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2460 self.assertRaises(TypeError, lambda: t1 == t2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2461 self.assertEqual(t2, t2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2462 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2463 # OTOH, it's OK to compare two of these mixing the two ways of being |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2464 # naive. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2465 t1 = self.theclass(5, 6, 7) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2466 self.assertEqual(t1, t2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2467 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2468 # Try a bogus uctoffset. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2469 class Bogus(tzinfo): |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2470 def utcoffset(self, dt): |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2471 return timedelta(minutes=1440) # out of bounds |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2472 t1 = self.theclass(2, 2, 2, tzinfo=Bogus()) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2473 t2 = self.theclass(2, 2, 2, tzinfo=FixedOffset(0, "")) |
|
26807
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2474 self.assertRaises(ValueError, lambda: t1 == t2) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2475 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2476 def test_pickling(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2477 # Try one without a tzinfo. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2478 args = 6, 7, 23, 20, 59, 1, 64**2 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2479 orig = self.theclass(*args) |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2480 for pickler, unpickler, proto in pickle_choices: |
|
27568
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
2481 green = pickler.dumps(orig, proto) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
2482 derived = unpickler.loads(green) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
2483 self.assertEqual(orig, derived) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2484 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2485 # Try one with a tzinfo. |
|
26737
6f1f84203ecf
Changes sufficient so that pickles written by the Python implementation
Tim Peters <tim.peters@gmail.com>
parents:
26728
diff
changeset
|
2486 tinfo = PicklableFixedOffset(-300, 'cookie') |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2487 orig = self.theclass(*args, **{'tzinfo': tinfo}) |
|
27218
38d2238c795e
Got rid of the internal datetimetz type.
Tim Peters <tim.peters@gmail.com>
parents:
27195
diff
changeset
|
2488 derived = self.theclass(1, 1, 1, tzinfo=FixedOffset(0, "", 0)) |
|
27538
87943faf4fd2
Change the approach to pickling to use __reduce__ everywhere. Most
Guido van Rossum <guido@python.org>
parents:
27365
diff
changeset
|
2489 for pickler, unpickler, proto in pickle_choices: |
|
27568
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
2490 green = pickler.dumps(orig, proto) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
2491 derived = unpickler.loads(green) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
2492 self.assertEqual(orig, derived) |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
2493 self.assertIsInstance(derived.tzinfo, PicklableFixedOffset) |
|
27568
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
2494 self.assertEqual(derived.utcoffset(), timedelta(minutes=-300)) |
|
f33e64484620
Changed the tests to stop using __setstate__(). __setstate__() no
Tim Peters <tim.peters@gmail.com>
parents:
27538
diff
changeset
|
2495 self.assertEqual(derived.tzname(), 'cookie') |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2496 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2497 def test_extreme_hashes(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2498 # If an attempt is made to hash these via subtracting the offset |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2499 # then hashing a datetime object, OverflowError results. The |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2500 # Python implementation used to blow up here. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2501 t = self.theclass(1, 1, 1, tzinfo=FixedOffset(1439, "")) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2502 hash(t) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2503 t = self.theclass(MAXYEAR, 12, 31, 23, 59, 59, 999999, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2504 tzinfo=FixedOffset(-1439, "")) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2505 hash(t) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2506 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2507 # OTOH, an OOB offset should blow up. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2508 t = self.theclass(5, 5, 5, tzinfo=FixedOffset(-1440, "")) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2509 self.assertRaises(ValueError, hash, t) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2510 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2511 def test_zones(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2512 est = FixedOffset(-300, "EST") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2513 utc = FixedOffset(0, "UTC") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2514 met = FixedOffset(60, "MET") |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2515 t1 = datetime(2002, 3, 19, 7, 47, tzinfo=est) |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2516 t2 = datetime(2002, 3, 19, 12, 47, tzinfo=utc) |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2517 t3 = datetime(2002, 3, 19, 13, 47, tzinfo=met) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2518 self.assertEqual(t1.tzinfo, est) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2519 self.assertEqual(t2.tzinfo, utc) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2520 self.assertEqual(t3.tzinfo, met) |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2521 self.assertEqual(t1.utcoffset(), timedelta(minutes=-300)) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2522 self.assertEqual(t2.utcoffset(), timedelta(minutes=0)) |
|
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2523 self.assertEqual(t3.utcoffset(), timedelta(minutes=60)) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2524 self.assertEqual(t1.tzname(), "EST") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2525 self.assertEqual(t2.tzname(), "UTC") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2526 self.assertEqual(t3.tzname(), "MET") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2527 self.assertEqual(hash(t1), hash(t2)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2528 self.assertEqual(hash(t1), hash(t3)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2529 self.assertEqual(hash(t2), hash(t3)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2530 self.assertEqual(t1, t2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2531 self.assertEqual(t1, t3) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2532 self.assertEqual(t2, t3) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2533 self.assertEqual(str(t1), "2002-03-19 07:47:00-05:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2534 self.assertEqual(str(t2), "2002-03-19 12:47:00+00:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2535 self.assertEqual(str(t3), "2002-03-19 13:47:00+01:00") |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2536 d = 'datetime.datetime(2002, 3, 19, ' |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2537 self.assertEqual(repr(t1), d + "7, 47, tzinfo=est)") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2538 self.assertEqual(repr(t2), d + "12, 47, tzinfo=utc)") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2539 self.assertEqual(repr(t3), d + "13, 47, tzinfo=met)") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2540 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2541 def test_combine(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2542 met = FixedOffset(60, "MET") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2543 d = date(2002, 3, 4) |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2544 tz = time(18, 45, 3, 1234, tzinfo=met) |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2545 dt = datetime.combine(d, tz) |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2546 self.assertEqual(dt, datetime(2002, 3, 4, 18, 45, 3, 1234, |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2547 tzinfo=met)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2548 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2549 def test_extract(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2550 met = FixedOffset(60, "MET") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2551 dt = self.theclass(2002, 3, 4, 18, 45, 3, 1234, tzinfo=met) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2552 self.assertEqual(dt.date(), date(2002, 3, 4)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2553 self.assertEqual(dt.time(), time(18, 45, 3, 1234)) |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2554 self.assertEqual(dt.timetz(), time(18, 45, 3, 1234, tzinfo=met)) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2555 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2556 def test_tz_aware_arithmetic(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2557 import random |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2558 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2559 now = self.theclass.now() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2560 tz55 = FixedOffset(-330, "west 5:30") |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2561 timeaware = now.time().replace(tzinfo=tz55) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2562 nowaware = self.theclass.combine(now.date(), timeaware) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2563 self.assertTrue(nowaware.tzinfo is tz55) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2564 self.assertEqual(nowaware.timetz(), timeaware) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2565 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2566 # Can't mix aware and non-aware. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2567 self.assertRaises(TypeError, lambda: now - nowaware) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2568 self.assertRaises(TypeError, lambda: nowaware - now) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2569 |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2570 # And adding datetime's doesn't make sense, aware or not. |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2571 self.assertRaises(TypeError, lambda: now + nowaware) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2572 self.assertRaises(TypeError, lambda: nowaware + now) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2573 self.assertRaises(TypeError, lambda: nowaware + nowaware) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2574 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2575 # Subtracting should yield 0. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2576 self.assertEqual(now - now, timedelta(0)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2577 self.assertEqual(nowaware - nowaware, timedelta(0)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2578 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2579 # Adding a delta should preserve tzinfo. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2580 delta = timedelta(weeks=1, minutes=12, microseconds=5678) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2581 nowawareplus = nowaware + delta |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2582 self.assertTrue(nowaware.tzinfo is tz55) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2583 nowawareplus2 = delta + nowaware |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2584 self.assertTrue(nowawareplus2.tzinfo is tz55) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2585 self.assertEqual(nowawareplus, nowawareplus2) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2586 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2587 # that - delta should be what we started with, and that - what we |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2588 # started with should be delta. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2589 diff = nowawareplus - delta |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2590 self.assertTrue(diff.tzinfo is tz55) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2591 self.assertEqual(nowaware, diff) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2592 self.assertRaises(TypeError, lambda: delta - nowawareplus) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2593 self.assertEqual(nowawareplus - nowaware, delta) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2594 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2595 # Make up a random timezone. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2596 tzr = FixedOffset(random.randrange(-1439, 1440), "randomtimezone") |
|
26797
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2597 # Attach it to nowawareplus. |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2598 nowawareplus = nowawareplus.replace(tzinfo=tzr) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2599 self.assertTrue(nowawareplus.tzinfo is tzr) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2600 # Make sure the difference takes the timezone adjustments into account. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2601 got = nowaware - nowawareplus |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2602 # Expected: (nowaware base - nowaware offset) - |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2603 # (nowawareplus base - nowawareplus offset) = |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2604 # (nowaware base - nowawareplus base) + |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2605 # (nowawareplus offset - nowaware offset) = |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2606 # -delta + nowawareplus offset - nowaware offset |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2607 expected = nowawareplus.utcoffset() - nowaware.utcoffset() - delta |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2608 self.assertEqual(got, expected) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2609 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2610 # Try max possible difference. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2611 min = self.theclass(1, 1, 1, tzinfo=FixedOffset(1439, "min")) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2612 max = self.theclass(MAXYEAR, 12, 31, 23, 59, 59, 999999, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2613 tzinfo=FixedOffset(-1439, "max")) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2614 maxdiff = max - min |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2615 self.assertEqual(maxdiff, self.theclass.max - self.theclass.min + |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2616 timedelta(minutes=2*1439)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2617 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2618 def test_tzinfo_now(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2619 meth = self.theclass.now |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2620 # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up). |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2621 base = meth() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2622 # Try with and without naming the keyword. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2623 off42 = FixedOffset(42, "42") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2624 another = meth(off42) |
|
27350
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2625 again = meth(tz=off42) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2626 self.assertTrue(another.tzinfo is again.tzinfo) |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2627 self.assertEqual(another.utcoffset(), timedelta(minutes=42)) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2628 # Bad argument with and w/o naming the keyword. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2629 self.assertRaises(TypeError, meth, 16) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2630 self.assertRaises(TypeError, meth, tzinfo=16) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2631 # Bad keyword name. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2632 self.assertRaises(TypeError, meth, tinfo=off42) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2633 # Too many args. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2634 self.assertRaises(TypeError, meth, off42, off42) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2635 |
|
27350
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2636 # We don't know which time zone we're in, and don't have a tzinfo |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2637 # class to represent it, so seeing whether a tz argument actually |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2638 # does a conversion is tricky. |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2639 weirdtz = FixedOffset(timedelta(hours=15, minutes=58), "weirdtz", 0) |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2640 utc = FixedOffset(0, "utc", 0) |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2641 for dummy in range(3): |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2642 now = datetime.now(weirdtz) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2643 self.assertTrue(now.tzinfo is weirdtz) |
|
27350
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2644 utcnow = datetime.utcnow().replace(tzinfo=utc) |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2645 now2 = utcnow.astimezone(weirdtz) |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2646 if abs(now - now2) < timedelta(seconds=30): |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2647 break |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2648 # Else the code is broken, or more than 30 seconds passed between |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2649 # calls; assuming the latter, just try again. |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2650 else: |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2651 # Three strikes and we're out. |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2652 self.fail("utcnow(), now(tz), or astimezone() may be broken") |
|
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2653 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2654 def test_tzinfo_fromtimestamp(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2655 import time |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2656 meth = self.theclass.fromtimestamp |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2657 ts = time.time() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2658 # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up). |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2659 base = meth(ts) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2660 # Try with and without naming the keyword. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2661 off42 = FixedOffset(42, "42") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2662 another = meth(ts, off42) |
|
27351
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
2663 again = meth(ts, tz=off42) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2664 self.assertTrue(another.tzinfo is again.tzinfo) |
|
26743
d052112731a8
Implemented a Wiki suggestion:
Tim Peters <tim.peters@gmail.com>
parents:
26738
diff
changeset
|
2665 self.assertEqual(another.utcoffset(), timedelta(minutes=42)) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2666 # Bad argument with and w/o naming the keyword. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2667 self.assertRaises(TypeError, meth, ts, 16) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2668 self.assertRaises(TypeError, meth, ts, tzinfo=16) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2669 # Bad keyword name. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2670 self.assertRaises(TypeError, meth, ts, tinfo=off42) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2671 # Too many args. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2672 self.assertRaises(TypeError, meth, ts, off42, off42) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2673 # Too few args. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2674 self.assertRaises(TypeError, meth) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2675 |
|
27351
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
2676 # Try to make sure tz= actually does some conversion. |
|
27722
cad1185eeef7
SF bug 680864: test_datetime fails for non-unix epoch
Tim Peters <tim.peters@gmail.com>
parents:
27689
diff
changeset
|
2677 timestamp = 1000000000 |
|
cad1185eeef7
SF bug 680864: test_datetime fails for non-unix epoch
Tim Peters <tim.peters@gmail.com>
parents:
27689
diff
changeset
|
2678 utcdatetime = datetime.utcfromtimestamp(timestamp) |
|
cad1185eeef7
SF bug 680864: test_datetime fails for non-unix epoch
Tim Peters <tim.peters@gmail.com>
parents:
27689
diff
changeset
|
2679 # In POSIX (epoch 1970), that's 2001-09-09 01:46:40 UTC, give or take. |
|
cad1185eeef7
SF bug 680864: test_datetime fails for non-unix epoch
Tim Peters <tim.peters@gmail.com>
parents:
27689
diff
changeset
|
2680 # But on some flavor of Mac, it's nowhere near that. So we can't have |
|
cad1185eeef7
SF bug 680864: test_datetime fails for non-unix epoch
Tim Peters <tim.peters@gmail.com>
parents:
27689
diff
changeset
|
2681 # any idea here what time that actually is, we can only test that |
|
cad1185eeef7
SF bug 680864: test_datetime fails for non-unix epoch
Tim Peters <tim.peters@gmail.com>
parents:
27689
diff
changeset
|
2682 # relative changes match. |
|
cad1185eeef7
SF bug 680864: test_datetime fails for non-unix epoch
Tim Peters <tim.peters@gmail.com>
parents:
27689
diff
changeset
|
2683 utcoffset = timedelta(hours=-15, minutes=39) # arbitrary, but not zero |
|
cad1185eeef7
SF bug 680864: test_datetime fails for non-unix epoch
Tim Peters <tim.peters@gmail.com>
parents:
27689
diff
changeset
|
2684 tz = FixedOffset(utcoffset, "tz", 0) |
|
cad1185eeef7
SF bug 680864: test_datetime fails for non-unix epoch
Tim Peters <tim.peters@gmail.com>
parents:
27689
diff
changeset
|
2685 expected = utcdatetime + utcoffset |
|
cad1185eeef7
SF bug 680864: test_datetime fails for non-unix epoch
Tim Peters <tim.peters@gmail.com>
parents:
27689
diff
changeset
|
2686 got = datetime.fromtimestamp(timestamp, tz) |
|
cad1185eeef7
SF bug 680864: test_datetime fails for non-unix epoch
Tim Peters <tim.peters@gmail.com>
parents:
27689
diff
changeset
|
2687 self.assertEqual(expected, got.replace(tzinfo=None)) |
|
27351
bbf14185bf1e
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1).
Tim Peters <tim.peters@gmail.com>
parents:
27350
diff
changeset
|
2688 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2689 def test_tzinfo_utcnow(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2690 meth = self.theclass.utcnow |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2691 # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up). |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2692 base = meth() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2693 # Try with and without naming the keyword; for whatever reason, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2694 # utcnow() doesn't accept a tzinfo argument. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2695 off42 = FixedOffset(42, "42") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2696 self.assertRaises(TypeError, meth, off42) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2697 self.assertRaises(TypeError, meth, tzinfo=off42) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2698 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2699 def test_tzinfo_utcfromtimestamp(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2700 import time |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2701 meth = self.theclass.utcfromtimestamp |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2702 ts = time.time() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2703 # Ensure it doesn't require tzinfo (i.e., that this doesn't blow up). |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2704 base = meth(ts) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2705 # Try with and without naming the keyword; for whatever reason, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2706 # utcfromtimestamp() doesn't accept a tzinfo argument. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2707 off42 = FixedOffset(42, "42") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2708 self.assertRaises(TypeError, meth, ts, off42) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2709 self.assertRaises(TypeError, meth, ts, tzinfo=off42) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2710 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2711 def test_tzinfo_timetuple(self): |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2712 # TestDateTime tested most of this. datetime adds a twist to the |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2713 # DST flag. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2714 class DST(tzinfo): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2715 def __init__(self, dstvalue): |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2716 if isinstance(dstvalue, int): |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2717 dstvalue = timedelta(minutes=dstvalue) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2718 self.dstvalue = dstvalue |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2719 def dst(self, dt): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2720 return self.dstvalue |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2721 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2722 cls = self.theclass |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2723 for dstvalue, flag in (-33, 1), (33, 1), (0, 0), (None, -1): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2724 d = cls(1, 1, 1, 10, 20, 30, 40, tzinfo=DST(dstvalue)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2725 t = d.timetuple() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2726 self.assertEqual(1, t.tm_year) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2727 self.assertEqual(1, t.tm_mon) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2728 self.assertEqual(1, t.tm_mday) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2729 self.assertEqual(10, t.tm_hour) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2730 self.assertEqual(20, t.tm_min) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2731 self.assertEqual(30, t.tm_sec) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2732 self.assertEqual(0, t.tm_wday) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2733 self.assertEqual(1, t.tm_yday) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2734 self.assertEqual(flag, t.tm_isdst) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2735 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2736 # dst() returns wrong type. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2737 self.assertRaises(TypeError, cls(1, 1, 1, tzinfo=DST("x")).timetuple) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2738 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2739 # dst() at the edge. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2740 self.assertEqual(cls(1,1,1, tzinfo=DST(1439)).timetuple().tm_isdst, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2741 self.assertEqual(cls(1,1,1, tzinfo=DST(-1439)).timetuple().tm_isdst, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2742 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2743 # dst() out of range. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2744 self.assertRaises(ValueError, cls(1,1,1, tzinfo=DST(1440)).timetuple) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2745 self.assertRaises(ValueError, cls(1,1,1, tzinfo=DST(-1440)).timetuple) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2746 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2747 def test_utctimetuple(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2748 class DST(tzinfo): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2749 def __init__(self, dstvalue): |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2750 if isinstance(dstvalue, int): |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2751 dstvalue = timedelta(minutes=dstvalue) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2752 self.dstvalue = dstvalue |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2753 def dst(self, dt): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2754 return self.dstvalue |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2755 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2756 cls = self.theclass |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2757 # This can't work: DST didn't implement utcoffset. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2758 self.assertRaises(NotImplementedError, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2759 cls(1, 1, 1, tzinfo=DST(0)).utcoffset) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2760 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2761 class UOFS(DST): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2762 def __init__(self, uofs, dofs=None): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2763 DST.__init__(self, dofs) |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2764 self.uofs = timedelta(minutes=uofs) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2765 def utcoffset(self, dt): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2766 return self.uofs |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2767 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2768 # Ensure tm_isdst is 0 regardless of what dst() says: DST is never |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2769 # in effect for a UTC time. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2770 for dstvalue in -33, 33, 0, None: |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2771 d = cls(1, 2, 3, 10, 20, 30, 40, tzinfo=UOFS(-53, dstvalue)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2772 t = d.utctimetuple() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2773 self.assertEqual(d.year, t.tm_year) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2774 self.assertEqual(d.month, t.tm_mon) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2775 self.assertEqual(d.day, t.tm_mday) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2776 self.assertEqual(11, t.tm_hour) # 20mm + 53mm = 1hn + 13mm |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2777 self.assertEqual(13, t.tm_min) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2778 self.assertEqual(d.second, t.tm_sec) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2779 self.assertEqual(d.weekday(), t.tm_wday) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2780 self.assertEqual(d.toordinal() - date(1, 1, 1).toordinal() + 1, |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2781 t.tm_yday) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2782 self.assertEqual(0, t.tm_isdst) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2783 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2784 # At the edges, UTC adjustment can normalize into years out-of-range |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2785 # for a datetime object. Ensure that a correct timetuple is |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2786 # created anyway. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2787 tiny = cls(MINYEAR, 1, 1, 0, 0, 37, tzinfo=UOFS(1439)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2788 # That goes back 1 minute less than a full day. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2789 t = tiny.utctimetuple() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2790 self.assertEqual(t.tm_year, MINYEAR-1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2791 self.assertEqual(t.tm_mon, 12) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2792 self.assertEqual(t.tm_mday, 31) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2793 self.assertEqual(t.tm_hour, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2794 self.assertEqual(t.tm_min, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2795 self.assertEqual(t.tm_sec, 37) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2796 self.assertEqual(t.tm_yday, 366) # "year 0" is a leap year |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2797 self.assertEqual(t.tm_isdst, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2798 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2799 huge = cls(MAXYEAR, 12, 31, 23, 59, 37, 999999, tzinfo=UOFS(-1439)) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2800 # That goes forward 1 minute less than a full day. |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2801 t = huge.utctimetuple() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2802 self.assertEqual(t.tm_year, MAXYEAR+1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2803 self.assertEqual(t.tm_mon, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2804 self.assertEqual(t.tm_mday, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2805 self.assertEqual(t.tm_hour, 23) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2806 self.assertEqual(t.tm_min, 58) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2807 self.assertEqual(t.tm_sec, 37) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2808 self.assertEqual(t.tm_yday, 1) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2809 self.assertEqual(t.tm_isdst, 0) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2810 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2811 def test_tzinfo_isoformat(self): |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2812 zero = FixedOffset(0, "+00:00") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2813 plus = FixedOffset(220, "+03:40") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2814 minus = FixedOffset(-231, "-03:51") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2815 unknown = FixedOffset(None, "") |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2816 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2817 cls = self.theclass |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2818 datestr = '0001-02-03' |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2819 for ofs in None, zero, plus, minus, unknown: |
|
26791
58bd7100ee15
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
26773
diff
changeset
|
2820 for us in 0, 987001: |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2821 d = cls(1, 2, 3, 4, 5, 59, us, tzinfo=ofs) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2822 timestr = '04:05:59' + (us and '.987001' or '') |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2823 ofsstr = ofs is not None and d.tzname() or '' |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2824 tailstr = timestr + ofsstr |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2825 iso = d.isoformat() |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2826 self.assertEqual(iso, datestr + 'T' + tailstr) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2827 self.assertEqual(iso, d.isoformat('T')) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2828 self.assertEqual(d.isoformat('k'), datestr + 'k' + tailstr) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2829 self.assertEqual(str(d), datestr + ' ' + tailstr) |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2830 |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2831 def test_replace(self): |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2832 cls = self.theclass |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2833 z100 = FixedOffset(100, "+100") |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2834 zm200 = FixedOffset(timedelta(minutes=-200), "-200") |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2835 args = [1, 2, 3, 4, 5, 6, 7, z100] |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2836 base = cls(*args) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2837 self.assertEqual(base, base.replace()) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2838 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2839 i = 0 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2840 for name, newval in (("year", 2), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2841 ("month", 3), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2842 ("day", 4), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2843 ("hour", 5), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2844 ("minute", 6), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2845 ("second", 7), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2846 ("microsecond", 8), |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2847 ("tzinfo", zm200)): |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2848 newargs = args[:] |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2849 newargs[i] = newval |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2850 expected = cls(*newargs) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2851 got = base.replace(**{name: newval}) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2852 self.assertEqual(expected, got) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2853 i += 1 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2854 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2855 # Ensure we can get rid of a tzinfo. |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2856 self.assertEqual(base.tzname(), "+100") |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2857 base2 = base.replace(tzinfo=None) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2858 self.assertTrue(base2.tzinfo is None) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2859 self.assertTrue(base2.tzname() is None) |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2860 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2861 # Ensure we can add one. |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2862 base3 = base2.replace(tzinfo=z100) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2863 self.assertEqual(base, base3) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2864 self.assertTrue(base.tzinfo is base3.tzinfo) |
|
26773
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2865 |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2866 # Out of bounds. |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2867 base = cls(2000, 2, 29) |
|
0f1405ace437
Implemented .replace() methods for date, datetime, datetimetz, time and
Tim Peters <tim.peters@gmail.com>
parents:
26746
diff
changeset
|
2868 self.assertRaises(ValueError, base.replace, year=2001) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2869 |
|
26792
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2870 def test_more_astimezone(self): |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2871 # The inherited test_astimezone covered some trivial and error cases. |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2872 fnone = FixedOffset(None, "None") |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2873 f44m = FixedOffset(44, "44") |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2874 fm5h = FixedOffset(-timedelta(hours=5), "m300") |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2875 |
|
27350
22992fa51831
Reimplemented datetime.now() to be useful.
Tim Peters <tim.peters@gmail.com>
parents:
27346
diff
changeset
|
2876 dt = self.theclass.now(tz=f44m) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2877 self.assertTrue(dt.tzinfo is f44m) |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2878 # Replacing with degenerate tzinfo raises an exception. |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2879 self.assertRaises(ValueError, dt.astimezone, fnone) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2880 # Ditto with None tz. |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2881 self.assertRaises(TypeError, dt.astimezone, None) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
2882 # Replacing with same tzinfo makes no change. |
|
26792
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2883 x = dt.astimezone(dt.tzinfo) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2884 self.assertTrue(x.tzinfo is f44m) |
|
26792
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2885 self.assertEqual(x.date(), dt.date()) |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2886 self.assertEqual(x.time(), dt.time()) |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2887 |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2888 # Replacing with different tzinfo does adjust. |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2889 got = dt.astimezone(fm5h) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2890 self.assertTrue(got.tzinfo is fm5h) |
|
26792
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2891 self.assertEqual(got.utcoffset(), timedelta(hours=-5)) |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2892 expected = dt - dt.utcoffset() # in effect, convert to UTC |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2893 expected += fm5h.utcoffset(dt) # and from there to local time |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2894 expected = expected.replace(tzinfo=fm5h) # and attach new tzinfo |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2895 self.assertEqual(got.date(), expected.date()) |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2896 self.assertEqual(got.time(), expected.time()) |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2897 self.assertEqual(got.timetz(), expected.timetz()) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2898 self.assertTrue(got.tzinfo is expected.tzinfo) |
|
26792
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2899 self.assertEqual(got, expected) |
|
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2900 |
|
26797
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2901 def test_aware_subtract(self): |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2902 cls = self.theclass |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2903 |
|
26807
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2904 # Ensure that utcoffset() is ignored when the operands have the |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2905 # same tzinfo member. |
|
26797
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2906 class OperandDependentOffset(tzinfo): |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2907 def utcoffset(self, t): |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2908 if t.minute < 10: |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2909 # d0 and d1 equal after adjustment |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2910 return timedelta(minutes=t.minute) |
|
26797
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2911 else: |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2912 # d2 off in the weeds |
|
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2913 return timedelta(minutes=59) |
|
26797
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2914 |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2915 base = cls(8, 9, 10, 11, 12, 13, 14, tzinfo=OperandDependentOffset()) |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2916 d0 = base.replace(minute=3) |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2917 d1 = base.replace(minute=9) |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2918 d2 = base.replace(minute=11) |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2919 for x in d0, d1, d2: |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2920 for y in d0, d1, d2: |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2921 got = x - y |
|
26807
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2922 expected = timedelta(minutes=x.minute - y.minute) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2923 self.assertEqual(got, expected) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2924 |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2925 # OTOH, if the tzinfo members are distinct, utcoffsets aren't |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2926 # ignored. |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2927 base = cls(8, 9, 10, 11, 12, 13, 14) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2928 d0 = base.replace(minute=3, tzinfo=OperandDependentOffset()) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2929 d1 = base.replace(minute=9, tzinfo=OperandDependentOffset()) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2930 d2 = base.replace(minute=11, tzinfo=OperandDependentOffset()) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2931 for x in d0, d1, d2: |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2932 for y in d0, d1, d2: |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2933 got = x - y |
|
26797
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2934 if (x is d0 or x is d1) and (y is d0 or y is d1): |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2935 expected = timedelta(0) |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2936 elif x is y is d2: |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2937 expected = timedelta(0) |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2938 elif x is d2: |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2939 expected = timedelta(minutes=(11-59)-0) |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2940 else: |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2941 assert y is d2 |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2942 expected = timedelta(minutes=0-(11-59)) |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2943 self.assertEqual(got, expected) |
|
47e7c70e841f
Added tests to ensure that timetz comparison, and datetimetz
Tim Peters <tim.peters@gmail.com>
parents:
26792
diff
changeset
|
2944 |
|
26807
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2945 def test_mixed_compare(self): |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2946 t1 = datetime(1, 2, 3, 4, 5, 6, 7) |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2947 t2 = datetime(1, 2, 3, 4, 5, 6, 7) |
|
26807
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2948 self.assertEqual(t1, t2) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2949 t2 = t2.replace(tzinfo=None) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2950 self.assertEqual(t1, t2) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2951 t2 = t2.replace(tzinfo=FixedOffset(None, "")) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2952 self.assertEqual(t1, t2) |
|
27766
c5ad0a2489b8
The Python implementation of datetime was changed in ways that no longer
Tim Peters <tim.peters@gmail.com>
parents:
27763
diff
changeset
|
2953 t2 = t2.replace(tzinfo=FixedOffset(0, "")) |
|
c5ad0a2489b8
The Python implementation of datetime was changed in ways that no longer
Tim Peters <tim.peters@gmail.com>
parents:
27763
diff
changeset
|
2954 self.assertRaises(TypeError, lambda: t1 == t2) |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2955 |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
2956 # In datetime w/ identical tzinfo objects, utcoffset is ignored. |
|
26807
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2957 class Varies(tzinfo): |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2958 def __init__(self): |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2959 self.offset = timedelta(minutes=22) |
|
26807
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2960 def utcoffset(self, t): |
|
27037
99df0e52f634
The tzinfo methods utcoffset() and dst() must return a timedelta object
Tim Peters <tim.peters@gmail.com>
parents:
27034
diff
changeset
|
2961 self.offset += timedelta(minutes=1) |
|
26807
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2962 return self.offset |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2963 |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2964 v = Varies() |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2965 t1 = t2.replace(tzinfo=v) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2966 t2 = t2.replace(tzinfo=v) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2967 self.assertEqual(t1.utcoffset(), timedelta(minutes=23)) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2968 self.assertEqual(t2.utcoffset(), timedelta(minutes=24)) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2969 self.assertEqual(t1, t2) |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2970 |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2971 # But if they're not identical, it isn't ignored. |
|
2319d13d54f5
Make comparison and subtraction of aware objects ignore tzinfo if the
Tim Peters <tim.peters@gmail.com>
parents:
26797
diff
changeset
|
2972 t2 = t2.replace(tzinfo=Varies()) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
2973 self.assertTrue(t1 < t2) # t1's offset counter still going up |
|
26792
816b8ddccfe1
Implemented datetime.astimezone() and datetimetz.astimezone().
Tim Peters <tim.peters@gmail.com>
parents:
26791
diff
changeset
|
2974 |
|
29149
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2975 def test_subclass_datetimetz(self): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2976 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2977 class C(self.theclass): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2978 theAnswer = 42 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2979 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2980 def __new__(cls, *args, **kws): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2981 temp = kws.copy() |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2982 extra = temp.pop('extra') |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2983 result = self.theclass.__new__(cls, *args, **temp) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2984 result.extra = extra |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2985 return result |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2986 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2987 def newmeth(self, start): |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2988 return start + self.hour + self.year |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2989 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2990 args = 2002, 12, 31, 4, 5, 6, 500, FixedOffset(-300, "EST", 1) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2991 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2992 dt1 = self.theclass(*args) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2993 dt2 = C(*args, **{'extra': 7}) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2994 |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2995 self.assertEqual(dt2.__class__, C) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2996 self.assertEqual(dt2.theAnswer, 42) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2997 self.assertEqual(dt2.extra, 7) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2998 self.assertEqual(dt1.utcoffset(), dt2.utcoffset()) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
2999 self.assertEqual(dt2.newmeth(-7), dt1.hour + dt1.year - 7) |
|
ef3ef88e3614
datetime.datetime and datetime.time can now be subclassed in Python. Brr.
Tim Peters <tim.peters@gmail.com>
parents:
29143
diff
changeset
|
3000 |
|
26830
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3001 # Pain to set up DST-aware tzinfo classes. |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3002 |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3003 def first_sunday_on_or_after(dt): |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3004 days_to_go = 6 - dt.weekday() |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3005 if days_to_go: |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3006 dt += timedelta(days_to_go) |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3007 return dt |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3008 |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3009 ZERO = timedelta(0) |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3010 HOUR = timedelta(hours=1) |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3011 DAY = timedelta(days=1) |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3012 # In the US, DST starts at 2am (standard time) on the first Sunday in April. |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3013 DSTSTART = datetime(1, 4, 1, 2) |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3014 # and ends at 2am (DST time; 1am standard time) on the last Sunday of Oct, |
|
27315
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3015 # which is the first Sunday on or after Oct 25. Because we view 1:MM as |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3016 # being standard time on that day, there is no spelling in local time of |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3017 # the last hour of DST (that's 1:MM DST, but 1:MM is taken as standard time). |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3018 DSTEND = datetime(1, 10, 25, 1) |
|
26830
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3019 |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3020 class USTimeZone(tzinfo): |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3021 |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3022 def __init__(self, hours, reprname, stdname, dstname): |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3023 self.stdoffset = timedelta(hours=hours) |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3024 self.reprname = reprname |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3025 self.stdname = stdname |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3026 self.dstname = dstname |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3027 |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3028 def __repr__(self): |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3029 return self.reprname |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3030 |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3031 def tzname(self, dt): |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3032 if self.dst(dt): |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3033 return self.dstname |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3034 else: |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3035 return self.stdname |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3036 |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3037 def utcoffset(self, dt): |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3038 return self.stdoffset + self.dst(dt) |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3039 |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3040 def dst(self, dt): |
|
26876
35468ad5022a
A step on the way to making tzinfo classes writable by mortals: get rid
Tim Peters <tim.peters@gmail.com>
parents:
26854
diff
changeset
|
3041 if dt is None or dt.tzinfo is None: |
|
26830
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3042 # An exception instead may be sensible here, in one or more of |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3043 # the cases. |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3044 return ZERO |
|
26972
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3045 assert dt.tzinfo is self |
|
26830
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3046 |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3047 # Find first Sunday in April. |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3048 start = first_sunday_on_or_after(DSTSTART.replace(year=dt.year)) |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3049 assert start.weekday() == 6 and start.month == 4 and start.day <= 7 |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3050 |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3051 # Find last Sunday in October. |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3052 end = first_sunday_on_or_after(DSTEND.replace(year=dt.year)) |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3053 assert end.weekday() == 6 and end.month == 10 and end.day >= 25 |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3054 |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3055 # Can't compare naive to aware objects, so strip the timezone from |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3056 # dt first. |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3057 if start <= dt.replace(tzinfo=None) < end: |
|
26830
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3058 return HOUR |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3059 else: |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3060 return ZERO |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3061 |
|
26972
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3062 Eastern = USTimeZone(-5, "Eastern", "EST", "EDT") |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3063 Central = USTimeZone(-6, "Central", "CST", "CDT") |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3064 Mountain = USTimeZone(-7, "Mountain", "MST", "MDT") |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3065 Pacific = USTimeZone(-8, "Pacific", "PST", "PDT") |
|
26851
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3066 utc_real = FixedOffset(0, "UTC", 0) |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3067 # For better test coverage, we want another flavor of UTC that's west of |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3068 # the Eastern and Pacific timezones. |
|
27082
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3069 utc_fake = FixedOffset(-12*60, "UTCfake", 0) |
|
26830
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3070 |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3071 class TestTimezoneConversions(unittest.TestCase): |
|
27315
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3072 # The DST switch times for 2002, in std time. |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
3073 dston = datetime(2002, 4, 7, 2) |
|
27315
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3074 dstoff = datetime(2002, 10, 27, 1) |
|
27171
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
3075 |
|
4b89d76fb205
Utterly minimal changes to collapse datetimetz into datetime, and timetz
Tim Peters <tim.peters@gmail.com>
parents:
27082
diff
changeset
|
3076 theclass = datetime |
|
27034
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27008
diff
changeset
|
3077 |
|
26972
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3078 # Check a time that's inside DST. |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3079 def checkinside(self, dt, tz, utc, dston, dstoff): |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3080 self.assertEqual(dt.dst(), HOUR) |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3081 |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3082 # Conversion to our own timezone is always an identity. |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3083 self.assertEqual(dt.astimezone(tz), dt) |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3084 |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3085 asutc = dt.astimezone(utc) |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3086 there_and_back = asutc.astimezone(tz) |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3087 |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3088 # Conversion to UTC and back isn't always an identity here, |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3089 # because there are redundant spellings (in local time) of |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3090 # UTC time when DST begins: the clock jumps from 1:59:59 |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3091 # to 3:00:00, and a local time of 2:MM:SS doesn't really |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3092 # make sense then. The classes above treat 2:MM:SS as |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3093 # daylight time then (it's "after 2am"), really an alias |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3094 # for 1:MM:SS standard time. The latter form is what |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3095 # conversion back from UTC produces. |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3096 if dt.date() == dston.date() and dt.hour == 2: |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3097 # We're in the redundant hour, and coming back from |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3098 # UTC gives the 1:MM:SS standard-time spelling. |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3099 self.assertEqual(there_and_back + HOUR, dt) |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3100 # Although during was considered to be in daylight |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3101 # time, there_and_back is not. |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3102 self.assertEqual(there_and_back.dst(), ZERO) |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3103 # They're the same times in UTC. |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3104 self.assertEqual(there_and_back.astimezone(utc), |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3105 dt.astimezone(utc)) |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3106 else: |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3107 # We're not in the redundant hour. |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3108 self.assertEqual(dt, there_and_back) |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3109 |
|
27315
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3110 # Because we have a redundant spelling when DST begins, there is |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3111 # (unforunately) an hour when DST ends that can't be spelled at all in |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3112 # local time. When DST ends, the clock jumps from 1:59 back to 1:00 |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3113 # again. The hour 1:MM DST has no spelling then: 1:MM is taken to be |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3114 # standard time. 1:MM DST == 0:MM EST, but 0:MM is taken to be |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3115 # daylight time. The hour 1:MM daylight == 0:MM standard can't be |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3116 # expressed in local time. Nevertheless, we want conversion back |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3117 # from UTC to mimic the local clock's "repeat an hour" behavior. |
|
26972
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3118 nexthour_utc = asutc + HOUR |
|
27082
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3119 nexthour_tz = nexthour_utc.astimezone(tz) |
|
27315
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3120 if dt.date() == dstoff.date() and dt.hour == 0: |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3121 # We're in the hour before the last DST hour. The last DST hour |
|
27082
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3122 # is ineffable. We want the conversion back to repeat 1:MM. |
|
27315
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3123 self.assertEqual(nexthour_tz, dt.replace(hour=1)) |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3124 nexthour_utc += HOUR |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3125 nexthour_tz = nexthour_utc.astimezone(tz) |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3126 self.assertEqual(nexthour_tz, dt.replace(hour=1)) |
|
26972
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3127 else: |
|
27315
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3128 self.assertEqual(nexthour_tz - dt, HOUR) |
|
26972
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3129 |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3130 # Check a time that's outside DST. |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3131 def checkoutside(self, dt, tz, utc): |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3132 self.assertEqual(dt.dst(), ZERO) |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3133 |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3134 # Conversion to our own timezone is always an identity. |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3135 self.assertEqual(dt.astimezone(tz), dt) |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3136 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3137 # Converting to UTC and back is an identity too. |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3138 asutc = dt.astimezone(utc) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3139 there_and_back = asutc.astimezone(tz) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3140 self.assertEqual(dt, there_and_back) |
|
26972
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3141 |
|
26851
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3142 def convert_between_tz_and_utc(self, tz, utc): |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3143 dston = self.dston.replace(tzinfo=tz) |
|
27315
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3144 # Because 1:MM on the day DST ends is taken as being standard time, |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3145 # there is no spelling in tz for the last hour of daylight time. |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3146 # For purposes of the test, the last hour of DST is 0:MM, which is |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3147 # taken as being daylight time (and 1:MM is taken as being standard |
|
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3148 # time). |
|
26851
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3149 dstoff = self.dstoff.replace(tzinfo=tz) |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3150 for delta in (timedelta(weeks=13), |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3151 DAY, |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3152 HOUR, |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3153 timedelta(minutes=1), |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3154 timedelta(microseconds=1)): |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3155 |
|
26972
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3156 self.checkinside(dston, tz, utc, dston, dstoff) |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3157 for during in dston + delta, dstoff - delta: |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3158 self.checkinside(during, tz, utc, dston, dstoff) |
|
26851
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3159 |
|
26972
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3160 self.checkoutside(dstoff, tz, utc) |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3161 for outside in dston - delta, dstoff + delta: |
|
84879e183773
A new, and much hairier, implementation of astimezone(), building on
Tim Peters <tim.peters@gmail.com>
parents:
26876
diff
changeset
|
3162 self.checkoutside(outside, tz, utc) |
|
26854
daa458b28385
Added tests that conversion to our own timezone is always an identity,
Tim Peters <tim.peters@gmail.com>
parents:
26851
diff
changeset
|
3163 |
|
26830
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3164 def test_easy(self): |
|
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3165 # Despite the name of this test, the endcases are excruciating. |
|
26851
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3166 self.convert_between_tz_and_utc(Eastern, utc_real) |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3167 self.convert_between_tz_and_utc(Pacific, utc_real) |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3168 self.convert_between_tz_and_utc(Eastern, utc_fake) |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3169 self.convert_between_tz_and_utc(Pacific, utc_fake) |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3170 # The next is really dancing near the edge. It works because |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3171 # Pacific and Eastern are far enough apart that their "problem |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3172 # hours" don't overlap. |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3173 self.convert_between_tz_and_utc(Eastern, Pacific) |
|
5f74e040012d
Beefed up the timezone conversion test by adding a phony UTC zone that's
Tim Peters <tim.peters@gmail.com>
parents:
26830
diff
changeset
|
3174 self.convert_between_tz_and_utc(Pacific, Eastern) |
|
26995
96e1ab8df883
The failure of the last-second addition to the timezone coversion test is
Tim Peters <tim.peters@gmail.com>
parents:
26972
diff
changeset
|
3175 # OTOH, these fail! Don't enable them. The difficulty is that |
|
96e1ab8df883
The failure of the last-second addition to the timezone coversion test is
Tim Peters <tim.peters@gmail.com>
parents:
26972
diff
changeset
|
3176 # the edge case tests assume that every hour is representable in |
|
96e1ab8df883
The failure of the last-second addition to the timezone coversion test is
Tim Peters <tim.peters@gmail.com>
parents:
26972
diff
changeset
|
3177 # the "utc" class. This is always true for a fixed-offset tzinfo |
|
96e1ab8df883
The failure of the last-second addition to the timezone coversion test is
Tim Peters <tim.peters@gmail.com>
parents:
26972
diff
changeset
|
3178 # class (lke utc_real and utc_fake), but not for Eastern or Central. |
|
96e1ab8df883
The failure of the last-second addition to the timezone coversion test is
Tim Peters <tim.peters@gmail.com>
parents:
26972
diff
changeset
|
3179 # For these adjacent DST-aware time zones, the range of time offsets |
|
96e1ab8df883
The failure of the last-second addition to the timezone coversion test is
Tim Peters <tim.peters@gmail.com>
parents:
26972
diff
changeset
|
3180 # tested ends up creating hours in the one that aren't representable |
|
96e1ab8df883
The failure of the last-second addition to the timezone coversion test is
Tim Peters <tim.peters@gmail.com>
parents:
26972
diff
changeset
|
3181 # in the other. For the same reason, we would see failures in the |
|
96e1ab8df883
The failure of the last-second addition to the timezone coversion test is
Tim Peters <tim.peters@gmail.com>
parents:
26972
diff
changeset
|
3182 # Eastern vs Pacific tests too if we added 3*HOUR to the list of |
|
96e1ab8df883
The failure of the last-second addition to the timezone coversion test is
Tim Peters <tim.peters@gmail.com>
parents:
26972
diff
changeset
|
3183 # offset deltas in convert_between_tz_and_utc(). |
|
96e1ab8df883
The failure of the last-second addition to the timezone coversion test is
Tim Peters <tim.peters@gmail.com>
parents:
26972
diff
changeset
|
3184 # |
|
96e1ab8df883
The failure of the last-second addition to the timezone coversion test is
Tim Peters <tim.peters@gmail.com>
parents:
26972
diff
changeset
|
3185 # self.convert_between_tz_and_utc(Eastern, Central) # can't work |
|
96e1ab8df883
The failure of the last-second addition to the timezone coversion test is
Tim Peters <tim.peters@gmail.com>
parents:
26972
diff
changeset
|
3186 # self.convert_between_tz_and_utc(Central, Eastern) # can't work |
|
26830
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3187 |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3188 def test_tricky(self): |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3189 # 22:00 on day before daylight starts. |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3190 fourback = self.dston - timedelta(hours=4) |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3191 ninewest = FixedOffset(-9*60, "-0900", 0) |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3192 fourback = fourback.replace(tzinfo=ninewest) |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3193 # 22:00-0900 is 7:00 UTC == 2:00 EST == 3:00 DST. Since it's "after |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3194 # 2", we should get the 3 spelling. |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3195 # If we plug 22:00 the day before into Eastern, it "looks like std |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3196 # time", so its offset is returned as -5, and -5 - -9 = 4. Adding 4 |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3197 # to 22:00 lands on 2:00, which makes no sense in local time (the |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3198 # local clock jumps from 1 to 3). The point here is to make sure we |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3199 # get the 3 spelling. |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3200 expected = self.dston.replace(hour=3) |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3201 got = fourback.astimezone(Eastern).replace(tzinfo=None) |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3202 self.assertEqual(expected, got) |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3203 |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3204 # Similar, but map to 6:00 UTC == 1:00 EST == 2:00 DST. In that |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3205 # case we want the 1:00 spelling. |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3206 sixutc = self.dston.replace(hour=6, tzinfo=utc_real) |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3207 # Now 6:00 "looks like daylight", so the offset wrt Eastern is -4, |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3208 # and adding -4-0 == -4 gives the 2:00 spelling. We want the 1:00 EST |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3209 # spelling. |
|
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3210 expected = self.dston.replace(hour=1) |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3211 got = sixutc.astimezone(Eastern).replace(tzinfo=None) |
|
27008
4579e83c663b
A quicker astimezone() implementation, rehabilitating an earlier
Tim Peters <tim.peters@gmail.com>
parents:
26995
diff
changeset
|
3212 self.assertEqual(expected, got) |
|
26830
f4b1beed6405
A start at non-trivial (== DST-aware) tests of timezone conversion.
Tim Peters <tim.peters@gmail.com>
parents:
26807
diff
changeset
|
3213 |
|
27082
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3214 # Now on the day DST ends, we want "repeat an hour" behavior. |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3215 # UTC 4:MM 5:MM 6:MM 7:MM checking these |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3216 # EST 23:MM 0:MM 1:MM 2:MM |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3217 # EDT 0:MM 1:MM 2:MM 3:MM |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3218 # wall 0:MM 1:MM 1:MM 2:MM against these |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3219 for utc in utc_real, utc_fake: |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3220 for tz in Eastern, Pacific: |
|
27315
efa2f8abb02c
New rule for tzinfo subclasses handling both standard and daylight time:
Tim Peters <tim.peters@gmail.com>
parents:
27218
diff
changeset
|
3221 first_std_hour = self.dstoff - timedelta(hours=2) # 23:MM |
|
27082
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3222 # Convert that to UTC. |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3223 first_std_hour -= tz.utcoffset(None) |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3224 # Adjust for possibly fake UTC. |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3225 asutc = first_std_hour + utc.utcoffset(None) |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3226 # First UTC hour to convert; this is 4:00 when utc=utc_real & |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3227 # tz=Eastern. |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3228 asutcbase = asutc.replace(tzinfo=utc) |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3229 for tzhour in (0, 1, 1, 2): |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3230 expectedbase = self.dstoff.replace(hour=tzhour) |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3231 for minute in 0, 30, 59: |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3232 expected = expectedbase.replace(minute=minute) |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3233 asutc = asutcbase.replace(minute=minute) |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3234 astz = asutc.astimezone(tz) |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3235 self.assertEqual(astz.replace(tzinfo=None), expected) |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3236 asutcbase += HOUR |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3237 |
|
64e8c6986610
A new implementation of astimezone() that does what we agreed on in all
Tim Peters <tim.peters@gmail.com>
parents:
27037
diff
changeset
|
3238 |
|
27034
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27008
diff
changeset
|
3239 def test_bogus_dst(self): |
|
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27008
diff
changeset
|
3240 class ok(tzinfo): |
|
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27008
diff
changeset
|
3241 def utcoffset(self, dt): return HOUR |
|
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27008
diff
changeset
|
3242 def dst(self, dt): return HOUR |
|
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27008
diff
changeset
|
3243 |
|
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27008
diff
changeset
|
3244 now = self.theclass.now().replace(tzinfo=utc_real) |
|
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27008
diff
changeset
|
3245 # Doesn't blow up. |
|
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27008
diff
changeset
|
3246 now.astimezone(ok()) |
|
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27008
diff
changeset
|
3247 |
|
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27008
diff
changeset
|
3248 # Does blow up. |
|
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27008
diff
changeset
|
3249 class notok(ok): |
|
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27008
diff
changeset
|
3250 def dst(self, dt): return None |
|
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27008
diff
changeset
|
3251 self.assertRaises(ValueError, now.astimezone, notok()) |
|
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27008
diff
changeset
|
3252 |
|
27346
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3253 def test_fromutc(self): |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3254 self.assertRaises(TypeError, Eastern.fromutc) # not enough args |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3255 now = datetime.utcnow().replace(tzinfo=utc_real) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3256 self.assertRaises(ValueError, Eastern.fromutc, now) # wrong tzinfo |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3257 now = now.replace(tzinfo=Eastern) # insert correct tzinfo |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3258 enow = Eastern.fromutc(now) # doesn't blow up |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3259 self.assertEqual(enow.tzinfo, Eastern) # has right tzinfo member |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3260 self.assertRaises(TypeError, Eastern.fromutc, now, now) # too many args |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3261 self.assertRaises(TypeError, Eastern.fromutc, date.today()) # wrong type |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3262 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3263 # Always converts UTC to standard time. |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3264 class FauxUSTimeZone(USTimeZone): |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3265 def fromutc(self, dt): |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3266 return dt + self.stdoffset |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3267 FEastern = FauxUSTimeZone(-5, "FEastern", "FEST", "FEDT") |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3268 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3269 # UTC 4:MM 5:MM 6:MM 7:MM 8:MM 9:MM |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3270 # EST 23:MM 0:MM 1:MM 2:MM 3:MM 4:MM |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3271 # EDT 0:MM 1:MM 2:MM 3:MM 4:MM 5:MM |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3272 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3273 # Check around DST start. |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3274 start = self.dston.replace(hour=4, tzinfo=Eastern) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3275 fstart = start.replace(tzinfo=FEastern) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3276 for wall in 23, 0, 1, 3, 4, 5: |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3277 expected = start.replace(hour=wall) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3278 if wall == 23: |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3279 expected -= timedelta(days=1) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3280 got = Eastern.fromutc(start) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3281 self.assertEqual(expected, got) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3282 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3283 expected = fstart + FEastern.stdoffset |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3284 got = FEastern.fromutc(fstart) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3285 self.assertEqual(expected, got) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3286 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3287 # Ensure astimezone() calls fromutc() too. |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3288 got = fstart.replace(tzinfo=utc_real).astimezone(FEastern) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3289 self.assertEqual(expected, got) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3290 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3291 start += HOUR |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3292 fstart += HOUR |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3293 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3294 # Check around DST end. |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3295 start = self.dstoff.replace(hour=4, tzinfo=Eastern) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3296 fstart = start.replace(tzinfo=FEastern) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3297 for wall in 0, 1, 1, 2, 3, 4: |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3298 expected = start.replace(hour=wall) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3299 got = Eastern.fromutc(start) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3300 self.assertEqual(expected, got) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3301 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3302 expected = fstart + FEastern.stdoffset |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3303 got = FEastern.fromutc(fstart) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3304 self.assertEqual(expected, got) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3305 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3306 # Ensure astimezone() calls fromutc() too. |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3307 got = fstart.replace(tzinfo=utc_real).astimezone(FEastern) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3308 self.assertEqual(expected, got) |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3309 |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3310 start += HOUR |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3311 fstart += HOUR |
|
2055d037295c
Bringing the code and test suite into line with doc and NEWS changes
Tim Peters <tim.peters@gmail.com>
parents:
27315
diff
changeset
|
3312 |
|
27034
dc33d027645a
astimezone() internals: if utcoffset() returns a duration, complain if
Tim Peters <tim.peters@gmail.com>
parents:
27008
diff
changeset
|
3313 |
|
33498
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3314 ############################################################################# |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3315 # oddballs |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3316 |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3317 class Oddballs(unittest.TestCase): |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3318 |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3319 def test_bug_1028306(self): |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3320 # Trying to compare a date to a datetime should act like a mixed- |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3321 # type comparison, despite that datetime is a subclass of date. |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3322 as_date = date.today() |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3323 as_datetime = datetime.combine(as_date, time()) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
3324 self.assertTrue(as_date != as_datetime) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
3325 self.assertTrue(as_datetime != as_date) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
3326 self.assertTrue(not as_date == as_datetime) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
3327 self.assertTrue(not as_datetime == as_date) |
|
33498
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3328 self.assertRaises(TypeError, lambda: as_date < as_datetime) |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3329 self.assertRaises(TypeError, lambda: as_datetime < as_date) |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3330 self.assertRaises(TypeError, lambda: as_date <= as_datetime) |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3331 self.assertRaises(TypeError, lambda: as_datetime <= as_date) |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3332 self.assertRaises(TypeError, lambda: as_date > as_datetime) |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3333 self.assertRaises(TypeError, lambda: as_datetime > as_date) |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3334 self.assertRaises(TypeError, lambda: as_date >= as_datetime) |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3335 self.assertRaises(TypeError, lambda: as_datetime >= as_date) |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3336 |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3337 # Neverthelss, comparison should work with the base-class (date) |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3338 # projection if use of a date method is forced. |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
3339 self.assertTrue(as_date.__eq__(as_datetime)) |
|
33498
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3340 different_day = (as_date.day + 1) % 20 + 1 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
50897
diff
changeset
|
3341 self.assertTrue(not as_date.__eq__(as_datetime.replace(day= |
|
33498
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3342 different_day))) |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3343 |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3344 # And date should compare with other subclasses of date. If a |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3345 # subclass wants to stop this, it's up to the subclass to do so. |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3346 date_sc = SubclassDate(as_date.year, as_date.month, as_date.day) |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3347 self.assertEqual(as_date, date_sc) |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3348 self.assertEqual(date_sc, as_date) |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3349 |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3350 # Ditto for datetimes. |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3351 datetime_sc = SubclassDatetime(as_datetime.year, as_datetime.month, |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3352 as_date.day, 0, 0, 0) |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3353 self.assertEqual(as_datetime, datetime_sc) |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3354 self.assertEqual(datetime_sc, as_datetime) |
|
abbdbba56a8b
SF bug #1028306: date-datetime comparison
Tim Peters <tim.peters@gmail.com>
parents:
32388
diff
changeset
|
3355 |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3356 def test_main(): |
|
41535
1002f42e10cd
Remove functionality from test_datetime.test_main() that does reference count checking; 'regrtest.py -R' is the way to do this kind of testing.
Collin Winter <collinw@gmail.com>
parents:
41534
diff
changeset
|
3357 test_support.run_unittest(__name__) |
|
26673
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3358 |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3359 if __name__ == "__main__": |
|
5b715cad34d0
datetime escapes the sandbox. The Windows build is all set. I leave it
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3360 test_main() |
