annotate Lib/test/test_time.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 2bf4741515a7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 40644
diff changeset
1 from test import support
4612
4de05cbf5ad1 test of time module. not terribly fancy, but it does touch every
Barry Warsaw <barry@python.org>
parents:
diff changeset
2 import time
17912
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
3 import unittest
54442
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 52474
diff changeset
4 import locale
67350
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
5 import sysconfig
69166
2ca1bc677a60 Issue #10762: Guard against invalid/non-supported format string '%f' on Windows. Patch Santoso Wijaya.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67949
diff changeset
6 import sys
74834
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
7 import platform
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
8 try:
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
9 import threading
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
10 except ImportError:
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
11 threading = None
17912
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
12
73281
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
13 # Max year is only limited by the size of C int.
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
14 SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
15 TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
16 TIME_MINYEAR = -TIME_MAXYEAR - 1
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
17
17912
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
18
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
19 class TimeTestCase(unittest.TestCase):
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
20
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
21 def setUp(self):
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
22 self.t = time.time()
4612
4de05cbf5ad1 test of time module. not terribly fancy, but it does touch every
Barry Warsaw <barry@python.org>
parents:
diff changeset
23
17912
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
24 def test_data_attributes(self):
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
25 time.altzone
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
26 time.daylight
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
27 time.timezone
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
28 time.tzname
4612
4de05cbf5ad1 test of time module. not terribly fancy, but it does touch every
Barry Warsaw <barry@python.org>
parents:
diff changeset
29
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
30 def test_time(self):
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
31 time.time()
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
32 info = time.get_clock_info('time')
76700
bec267540811 don't use assertEqual for test for bool equality
Benjamin Peterson <benjamin@python.org>
parents: 76695
diff changeset
33 self.assertFalse(info.monotonic)
77416
26e2ee402a0b Fix test_time for adjusted/adjustable changes
Victor Stinner <victor.stinner@gmail.com>
parents: 77415
diff changeset
34 self.assertTrue(info.adjustable)
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
35
17912
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
36 def test_clock(self):
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
37 time.clock()
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
38
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
39 info = time.get_clock_info('clock')
76700
bec267540811 don't use assertEqual for test for bool equality
Benjamin Peterson <benjamin@python.org>
parents: 76695
diff changeset
40 self.assertTrue(info.monotonic)
77415
0e46e0cd368f PEP 418: Rename adjusted attribute to adjustable in time.get_clock_info() result
Victor Stinner <victor.stinner@gmail.com>
parents: 77291
diff changeset
41 self.assertFalse(info.adjustable)
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
42
73126
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
43 @unittest.skipUnless(hasattr(time, 'clock_gettime'),
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
44 'need time.clock_gettime()')
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
45 def test_clock_realtime(self):
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
46 time.clock_gettime(time.CLOCK_REALTIME)
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
47
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
48 @unittest.skipUnless(hasattr(time, 'clock_gettime'),
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
49 'need time.clock_gettime()')
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
50 @unittest.skipUnless(hasattr(time, 'CLOCK_MONOTONIC'),
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
51 'need time.CLOCK_MONOTONIC')
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
52 def test_clock_monotonic(self):
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
53 a = time.clock_gettime(time.CLOCK_MONOTONIC)
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
54 b = time.clock_gettime(time.CLOCK_MONOTONIC)
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
55 self.assertLessEqual(a, b)
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
56
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
57 @unittest.skipUnless(hasattr(time, 'clock_getres'),
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
58 'need time.clock_getres()')
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
59 def test_clock_getres(self):
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
60 res = time.clock_getres(time.CLOCK_REALTIME)
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
61 self.assertGreater(res, 0.0)
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
62 self.assertLessEqual(res, 1.0)
35e4b7c4bafa Close #10278: Add clock_getres(), clock_gettime() and CLOCK_xxx constants to
Victor Stinner <victor.stinner@haypocalc.com>
parents: 71236
diff changeset
63
76080
8ba72c0987dc Expose clock_settime() as time.clock_settime()
Victor Stinner <victor.stinner@gmail.com>
parents: 75672
diff changeset
64 @unittest.skipUnless(hasattr(time, 'clock_settime'),
8ba72c0987dc Expose clock_settime() as time.clock_settime()
Victor Stinner <victor.stinner@gmail.com>
parents: 75672
diff changeset
65 'need time.clock_settime()')
8ba72c0987dc Expose clock_settime() as time.clock_settime()
Victor Stinner <victor.stinner@gmail.com>
parents: 75672
diff changeset
66 def test_clock_settime(self):
8ba72c0987dc Expose clock_settime() as time.clock_settime()
Victor Stinner <victor.stinner@gmail.com>
parents: 75672
diff changeset
67 t = time.clock_gettime(time.CLOCK_REALTIME)
8ba72c0987dc Expose clock_settime() as time.clock_settime()
Victor Stinner <victor.stinner@gmail.com>
parents: 75672
diff changeset
68 try:
8ba72c0987dc Expose clock_settime() as time.clock_settime()
Victor Stinner <victor.stinner@gmail.com>
parents: 75672
diff changeset
69 time.clock_settime(time.CLOCK_REALTIME, t)
8ba72c0987dc Expose clock_settime() as time.clock_settime()
Victor Stinner <victor.stinner@gmail.com>
parents: 75672
diff changeset
70 except PermissionError:
8ba72c0987dc Expose clock_settime() as time.clock_settime()
Victor Stinner <victor.stinner@gmail.com>
parents: 75672
diff changeset
71 pass
8ba72c0987dc Expose clock_settime() as time.clock_settime()
Victor Stinner <victor.stinner@gmail.com>
parents: 75672
diff changeset
72
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
73 if hasattr(time, 'CLOCK_MONOTONIC'):
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
74 self.assertRaises(OSError,
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
75 time.clock_settime, time.CLOCK_MONOTONIC, 0)
76080
8ba72c0987dc Expose clock_settime() as time.clock_settime()
Victor Stinner <victor.stinner@gmail.com>
parents: 75672
diff changeset
76
17912
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
77 def test_conversions(self):
67436
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
78 self.assertEqual(time.ctime(self.t),
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
79 time.asctime(time.localtime(self.t)))
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
80 self.assertEqual(int(time.mktime(time.localtime(self.t))),
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
81 int(self.t))
4612
4de05cbf5ad1 test of time module. not terribly fancy, but it does touch every
Barry Warsaw <barry@python.org>
parents:
diff changeset
82
17912
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
83 def test_sleep(self):
71236
0e5485634817 Issue #12459: time.sleep() now raises a ValueError if the sleep length is
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69784
diff changeset
84 self.assertRaises(ValueError, time.sleep, -2)
0e5485634817 Issue #12459: time.sleep() now raises a ValueError if the sleep length is
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69784
diff changeset
85 self.assertRaises(ValueError, time.sleep, -1)
17912
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
86 time.sleep(1.2)
4612
4de05cbf5ad1 test of time module. not terribly fancy, but it does touch every
Barry Warsaw <barry@python.org>
parents:
diff changeset
87
17912
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
88 def test_strftime(self):
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
89 tt = time.gmtime(self.t)
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
90 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
91 'j', 'm', 'M', 'p', 'S',
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
92 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
93 format = ' %' + directive
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
94 try:
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
95 time.strftime(format, tt)
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
96 except ValueError:
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
97 self.fail('conversion specifier: %r failed.' % format)
4612
4de05cbf5ad1 test of time module. not terribly fancy, but it does touch every
Barry Warsaw <barry@python.org>
parents:
diff changeset
98
69166
2ca1bc677a60 Issue #10762: Guard against invalid/non-supported format string '%f' on Windows. Patch Santoso Wijaya.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67949
diff changeset
99 # Issue #10762: Guard against invalid/non-supported format string
2ca1bc677a60 Issue #10762: Guard against invalid/non-supported format string '%f' on Windows. Patch Santoso Wijaya.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67949
diff changeset
100 # so that Python don't crash (Windows crashes when the format string
2ca1bc677a60 Issue #10762: Guard against invalid/non-supported format string '%f' on Windows. Patch Santoso Wijaya.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67949
diff changeset
101 # input to [w]strftime is not kosher.
2ca1bc677a60 Issue #10762: Guard against invalid/non-supported format string '%f' on Windows. Patch Santoso Wijaya.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67949
diff changeset
102 if sys.platform.startswith('win'):
2ca1bc677a60 Issue #10762: Guard against invalid/non-supported format string '%f' on Windows. Patch Santoso Wijaya.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67949
diff changeset
103 with self.assertRaises(ValueError):
2ca1bc677a60 Issue #10762: Guard against invalid/non-supported format string '%f' on Windows. Patch Santoso Wijaya.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67949
diff changeset
104 time.strftime('%f')
2ca1bc677a60 Issue #10762: Guard against invalid/non-supported format string '%f' on Windows. Patch Santoso Wijaya.
Senthil Kumaran <orsenthil@gmail.com>
parents: 67949
diff changeset
105
73272
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
106 def _bounds_checking(self, func):
31608
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
107 # Make sure that strftime() checks the bounds of the various parts
73272
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
108 # of the time tuple (0 is valid for *all* values).
31608
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
109
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
110 # The year field is tested by other test cases above
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
111
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
112 # Check month [1, 12] + zero support
73272
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
113 func((1900, 0, 1, 0, 0, 0, 0, 1, -1))
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
114 func((1900, 12, 1, 0, 0, 0, 0, 1, -1))
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
115 self.assertRaises(ValueError, func,
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
116 (1900, -1, 1, 0, 0, 0, 0, 1, -1))
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
117 self.assertRaises(ValueError, func,
31608
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
118 (1900, 13, 1, 0, 0, 0, 0, 1, -1))
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
119 # Check day of month [1, 31] + zero support
73272
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
120 func((1900, 1, 0, 0, 0, 0, 0, 1, -1))
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
121 func((1900, 1, 31, 0, 0, 0, 0, 1, -1))
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
122 self.assertRaises(ValueError, func,
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
123 (1900, 1, -1, 0, 0, 0, 0, 1, -1))
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
124 self.assertRaises(ValueError, func,
31608
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
125 (1900, 1, 32, 0, 0, 0, 0, 1, -1))
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
126 # Check hour [0, 23]
73272
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
127 func((1900, 1, 1, 23, 0, 0, 0, 1, -1))
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
128 self.assertRaises(ValueError, func,
31608
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
129 (1900, 1, 1, -1, 0, 0, 0, 1, -1))
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
130 self.assertRaises(ValueError, func,
31608
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
131 (1900, 1, 1, 24, 0, 0, 0, 1, -1))
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
132 # Check minute [0, 59]
73272
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
133 func((1900, 1, 1, 0, 59, 0, 0, 1, -1))
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
134 self.assertRaises(ValueError, func,
31608
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
135 (1900, 1, 1, 0, -1, 0, 0, 1, -1))
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
136 self.assertRaises(ValueError, func,
31608
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
137 (1900, 1, 1, 0, 60, 0, 0, 1, -1))
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
138 # Check second [0, 61]
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
139 self.assertRaises(ValueError, func,
31608
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
140 (1900, 1, 1, 0, 0, -1, 0, 1, -1))
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
141 # C99 only requires allowing for one leap second, but Python's docs say
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
142 # allow two leap seconds (0..61)
73272
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
143 func((1900, 1, 1, 0, 0, 60, 0, 1, -1))
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
144 func((1900, 1, 1, 0, 0, 61, 0, 1, -1))
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
145 self.assertRaises(ValueError, func,
31608
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
146 (1900, 1, 1, 0, 0, 62, 0, 1, -1))
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
147 # No check for upper-bound day of week;
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
148 # value forced into range by a ``% 7`` calculation.
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
149 # Start check at -2 since gettmarg() increments value before taking
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
150 # modulo.
73272
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
151 self.assertEqual(func((1900, 1, 1, 0, 0, 0, -1, 1, -1)),
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
152 func((1900, 1, 1, 0, 0, 0, +6, 1, -1)))
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
153 self.assertRaises(ValueError, func,
31608
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
154 (1900, 1, 1, 0, 0, 0, -2, 1, -1))
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
155 # Check day of the year [1, 366] + zero support
73272
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
156 func((1900, 1, 1, 0, 0, 0, 0, 0, -1))
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
157 func((1900, 1, 1, 0, 0, 0, 0, 366, -1))
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
158 self.assertRaises(ValueError, func,
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
159 (1900, 1, 1, 0, 0, 0, 0, -1, -1))
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
160 self.assertRaises(ValueError, func,
31608
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
161 (1900, 1, 1, 0, 0, 0, 0, 367, -1))
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
162
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
163 def test_strftime_bounding_check(self):
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
164 self._bounds_checking(lambda tup: time.strftime('', tup))
31608
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 28774
diff changeset
165
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
166 def test_default_values_for_zero(self):
69784
e6f6ac8c2502 Issue #11930: Remove deprecated time.accept2dyear.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 69167
diff changeset
167 # Make sure that using all zeros uses the proper default
e6f6ac8c2502 Issue #11930: Remove deprecated time.accept2dyear.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 69167
diff changeset
168 # values. No test for daylight savings since strftime() does
e6f6ac8c2502 Issue #11930: Remove deprecated time.accept2dyear.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 69167
diff changeset
169 # not change output based on its value and no test for year
e6f6ac8c2502 Issue #11930: Remove deprecated time.accept2dyear.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 69167
diff changeset
170 # because systems vary in their support for year 0.
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
171 expected = "2000 01 01 00 00 00 1 001"
67436
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
172 with support.check_warnings():
69784
e6f6ac8c2502 Issue #11930: Remove deprecated time.accept2dyear.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 69167
diff changeset
173 result = time.strftime("%Y %m %d %H %M %S %w %j", (2000,)+(0,)*8)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
174 self.assertEqual(expected, result)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
175
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents: 23085
diff changeset
176 def test_strptime(self):
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
177 # Should be able to go round-trip from strftime to strptime without
80928
fdf907708f49 Issue #16714: use 'raise' exceptions, don't 'throw'.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 73886
diff changeset
178 # raising an exception.
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents: 23085
diff changeset
179 tt = time.gmtime(self.t)
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents: 23085
diff changeset
180 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents: 23085
diff changeset
181 'j', 'm', 'M', 'p', 'S',
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents: 23085
diff changeset
182 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
183 format = '%' + directive
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
184 strf_output = time.strftime(format, tt)
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents: 23085
diff changeset
185 try:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
186 time.strptime(strf_output, format)
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents: 23085
diff changeset
187 except ValueError:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
188 self.fail("conversion specifier %r failed with '%s' input." %
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
189 (format, strf_output))
24322
afe479a6235e Pure Python strptime implementation by Brett Cannon. See SF patch 474274.
Guido van Rossum <guido@python.org>
parents: 23085
diff changeset
190
52474
45a717450961 Make sure time.strptime only accepts strings (and document the fact like
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
191 def test_strptime_bytes(self):
45a717450961 Make sure time.strptime only accepts strings (and document the fact like
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
192 # Make sure only strings are accepted as arguments to strptime.
45a717450961 Make sure time.strptime only accepts strings (and document the fact like
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
193 self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
45a717450961 Make sure time.strptime only accepts strings (and document the fact like
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
194 self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
45a717450961 Make sure time.strptime only accepts strings (and document the fact like
Brett Cannon <bcannon@gmail.com>
parents: 47201
diff changeset
195
83091
3203e083aa7b #17572: Avoid chained exceptions while passing bad directives to time.strptime(). Initial patch by Claudiu Popa.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80929
diff changeset
196 def test_strptime_exception_context(self):
3203e083aa7b #17572: Avoid chained exceptions while passing bad directives to time.strptime(). Initial patch by Claudiu Popa.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80929
diff changeset
197 # check that this doesn't chain exceptions needlessly (see #17572)
3203e083aa7b #17572: Avoid chained exceptions while passing bad directives to time.strptime(). Initial patch by Claudiu Popa.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80929
diff changeset
198 with self.assertRaises(ValueError) as e:
3203e083aa7b #17572: Avoid chained exceptions while passing bad directives to time.strptime(). Initial patch by Claudiu Popa.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80929
diff changeset
199 time.strptime('', '%D')
3203e083aa7b #17572: Avoid chained exceptions while passing bad directives to time.strptime(). Initial patch by Claudiu Popa.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80929
diff changeset
200 self.assertIs(e.exception.__suppress_context__, True)
87510
ce1578f4d105 Issue #19545: Avoid chained exceptions while passing stray % to
Serhiy Storchaka <storchaka@gmail.com>
parents: 84436
diff changeset
201 # additional check for IndexError branch (issue #19545)
ce1578f4d105 Issue #19545: Avoid chained exceptions while passing stray % to
Serhiy Storchaka <storchaka@gmail.com>
parents: 84436
diff changeset
202 with self.assertRaises(ValueError) as e:
ce1578f4d105 Issue #19545: Avoid chained exceptions while passing stray % to
Serhiy Storchaka <storchaka@gmail.com>
parents: 84436
diff changeset
203 time.strptime('19', '%Y %')
ce1578f4d105 Issue #19545: Avoid chained exceptions while passing stray % to
Serhiy Storchaka <storchaka@gmail.com>
parents: 84436
diff changeset
204 self.assertIs(e.exception.__suppress_context__, True)
83091
3203e083aa7b #17572: Avoid chained exceptions while passing bad directives to time.strptime(). Initial patch by Claudiu Popa.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80929
diff changeset
205
17912
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
206 def test_asctime(self):
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
207 time.asctime(time.gmtime(self.t))
67350
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
208
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
209 # Max year is only limited by the size of C int.
73281
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
210 for bigyear in TIME_MAXYEAR, TIME_MINYEAR:
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
211 asc = time.asctime((bigyear, 6, 1) + (0,) * 6)
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
212 self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
213 self.assertRaises(OverflowError, time.asctime,
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
214 (TIME_MAXYEAR + 1,) + (0,) * 8)
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
215 self.assertRaises(OverflowError, time.asctime,
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
216 (TIME_MINYEAR - 1,) + (0,) * 8)
17912
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
217 self.assertRaises(TypeError, time.asctime, 0)
67279
f89e37cd6c65 Merged revisions 87648,87656 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66883
diff changeset
218 self.assertRaises(TypeError, time.asctime, ())
67424
7e5777e1f0fd Further simplify gettmarg()
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67390
diff changeset
219 self.assertRaises(TypeError, time.asctime, (0,) * 10)
4612
4de05cbf5ad1 test of time module. not terribly fancy, but it does touch every
Barry Warsaw <barry@python.org>
parents:
diff changeset
220
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
221 def test_asctime_bounding_check(self):
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
222 self._bounds_checking(time.asctime)
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 56176
diff changeset
223
67275
d39efa54deaf #8013 follow-up:
Georg Brandl <georg@python.org>
parents: 67274
diff changeset
224 def test_ctime(self):
67350
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
225 t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1))
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
226 self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973')
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
227 t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1))
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
228 self.assertEqual(time.ctime(t), 'Sat Jan 1 00:00:00 2000')
67436
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
229 for year in [-100, 100, 1000, 2000, 10000]:
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
230 try:
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
231 testval = time.mktime((year, 1, 10) + (0,)*6)
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
232 except (ValueError, OverflowError):
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
233 # If mktime fails, ctime will fail too. This may happen
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
234 # on some platforms.
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
235 pass
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
236 else:
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
237 self.assertEqual(time.ctime(testval)[20:], str(year))
4612
4de05cbf5ad1 test of time module. not terribly fancy, but it does touch every
Barry Warsaw <barry@python.org>
parents:
diff changeset
238
73505
bcd347cd6bf2 Use unittest.skipUnless to skip the test related to the glibc bug, issue #13309.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73490
diff changeset
239 @unittest.skipUnless(hasattr(time, "tzset"),
bcd347cd6bf2 Use unittest.skipUnless to skip the test related to the glibc bug, issue #13309.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73490
diff changeset
240 "time module has no attribute tzset")
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
241 def test_tzset(self):
28300
9602c664ed7a If time.tzset doesn't exist, don't test it.
Guido van Rossum <guido@python.org>
parents: 28299
diff changeset
242
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
243 from os import environ
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
244
28774
c9bee11eaa8b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28579
diff changeset
245 # Epoch time of midnight Dec 25th 2002. Never DST in northern
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
246 # hemisphere.
28774
c9bee11eaa8b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28579
diff changeset
247 xmas2002 = 1040774400.0
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
248
28579
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
249 # These formats are correct for 2002, and possibly future years
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
250 # This format is the 'standard' as documented at:
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
251 # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
252 # They are also documented in the tzset(3) man page on most Unix
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
253 # systems.
28774
c9bee11eaa8b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28579
diff changeset
254 eastern = 'EST+05EDT,M4.1.0,M10.5.0'
28579
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
255 victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
256 utc='UTC+0'
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
257
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
258 org_TZ = environ.get('TZ',None)
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
259 try:
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
260 # Make sure we can switch to UTC time and results are correct
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
261 # Note that unknown timezones default to UTC.
28579
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
262 # Note that altzone is undefined in UTC, as there is no DST
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
263 environ['TZ'] = eastern
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
264 time.tzset()
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
265 environ['TZ'] = utc
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
266 time.tzset()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
267 self.assertEqual(
28579
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
268 time.gmtime(xmas2002), time.localtime(xmas2002)
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
269 )
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
270 self.assertEqual(time.daylight, 0)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
271 self.assertEqual(time.timezone, 0)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
272 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
273
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
274 # Make sure we can switch to US/Eastern
28579
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
275 environ['TZ'] = eastern
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
276 time.tzset()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
277 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
278 self.assertEqual(time.tzname, ('EST', 'EDT'))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
279 self.assertEqual(len(time.tzname), 2)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
280 self.assertEqual(time.daylight, 1)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
281 self.assertEqual(time.timezone, 18000)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
282 self.assertEqual(time.altzone, 14400)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
283 self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
284 self.assertEqual(len(time.tzname), 2)
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
285
28579
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
286 # Now go to the southern hemisphere.
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
287 environ['TZ'] = victoria
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
288 time.tzset()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
289 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
73886
c143e66e5efe Issue #11886: workaround an OS bug (time zone data) in test_time
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69167
diff changeset
290
c143e66e5efe Issue #11886: workaround an OS bug (time zone data) in test_time
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69167
diff changeset
291 # Issue #11886: Australian Eastern Standard Time (UTC+10) is called
73921
e37a7dc8944e Issue #11886: Fix also test_time for the non-DST timezone name (EST/AEST)
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73887
diff changeset
292 # "EST" (as Eastern Standard Time, UTC-5) instead of "AEST"
e37a7dc8944e Issue #11886: Fix also test_time for the non-DST timezone name (EST/AEST)
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73887
diff changeset
293 # (non-DST timezone), and "EDT" instead of "AEDT" (DST timezone),
e37a7dc8944e Issue #11886: Fix also test_time for the non-DST timezone name (EST/AEST)
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73887
diff changeset
294 # on some operating systems (e.g. FreeBSD), which is wrong. See for
e37a7dc8944e Issue #11886: Fix also test_time for the non-DST timezone name (EST/AEST)
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73887
diff changeset
295 # example this bug:
e37a7dc8944e Issue #11886: Fix also test_time for the non-DST timezone name (EST/AEST)
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73887
diff changeset
296 # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=93810
73886
c143e66e5efe Issue #11886: workaround an OS bug (time zone data) in test_time
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69167
diff changeset
297 self.assertIn(time.tzname[0], ('AEST' 'EST'), time.tzname[0])
73921
e37a7dc8944e Issue #11886: Fix also test_time for the non-DST timezone name (EST/AEST)
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73887
diff changeset
298 self.assertTrue(time.tzname[1] in ('AEDT', 'EDT'), str(time.tzname[1]))
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
299 self.assertEqual(len(time.tzname), 2)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
300 self.assertEqual(time.daylight, 1)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
301 self.assertEqual(time.timezone, -36000)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
302 self.assertEqual(time.altzone, -39600)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54443
diff changeset
303 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
304
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
305 finally:
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
306 # Repair TZ environment variable in case any other tests
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
307 # rely on it.
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
308 if org_TZ is not None:
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
309 environ['TZ'] = org_TZ
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
310 elif 'TZ' in environ:
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
311 del environ['TZ']
28579
981032e64736 SF patch #706707, time.tzset standards compliance update by Stuart Bishop
Neal Norwitz <nnorwitz@gmail.com>
parents: 28300
diff changeset
312 time.tzset()
28774
c9bee11eaa8b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28579
diff changeset
313
32388
5cf507b64f31 Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents: 31608
diff changeset
314 def test_insane_timestamps(self):
5cf507b64f31 Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents: 31608
diff changeset
315 # 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: 31608
diff changeset
316 # 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: 31608
diff changeset
317 # exempt such platforms (provided they return reasonable
5cf507b64f31 Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents: 31608
diff changeset
318 # results!).
5cf507b64f31 Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents: 31608
diff changeset
319 for func in time.ctime, time.gmtime, time.localtime:
5cf507b64f31 Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents: 31608
diff changeset
320 for unreasonable in -1e200, 1e200:
75590
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
321 self.assertRaises(OverflowError, func, unreasonable)
17912
421b1d660495 Convert time module tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15540
diff changeset
322
32939
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32388
diff changeset
323 def test_ctime_without_arg(self):
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32388
diff changeset
324 # Not sure how to check the values, since the clock could tick
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32388
diff changeset
325 # at any time. Make sure these are at least accepted and
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32388
diff changeset
326 # don't raise errors.
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32388
diff changeset
327 time.ctime()
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32388
diff changeset
328 time.ctime(None)
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32388
diff changeset
329
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32388
diff changeset
330 def test_gmtime_without_arg(self):
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
331 gt0 = time.gmtime()
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
332 gt1 = time.gmtime(None)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
333 t0 = time.mktime(gt0)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
334 t1 = time.mktime(gt1)
67436
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
335 self.assertAlmostEqual(t1, t0, delta=0.2)
32939
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32388
diff changeset
336
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32388
diff changeset
337 def test_localtime_without_arg(self):
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
338 lt0 = time.localtime()
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
339 lt1 = time.localtime(None)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
340 t0 = time.mktime(lt0)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 32939
diff changeset
341 t1 = time.mktime(lt1)
67436
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
342 self.assertAlmostEqual(t1, t0, delta=0.2)
67946
2ee2c76082c8 Merged revisions 87919 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67305
diff changeset
343
73505
bcd347cd6bf2 Use unittest.skipUnless to skip the test related to the glibc bug, issue #13309.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73490
diff changeset
344 def test_mktime(self):
73281
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
345 # Issue #1726687
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
346 for t in (-2, -1, 0, 1):
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
347 try:
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
348 tt = time.localtime(t)
74641
516d42a6e518 Issue #13847: Fix test_mktime(), time.localtime() now raises OSError
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74634
diff changeset
349 except (OverflowError, OSError):
73281
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
350 pass
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
351 else:
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
352 self.assertEqual(time.mktime(tt), t)
73505
bcd347cd6bf2 Use unittest.skipUnless to skip the test related to the glibc bug, issue #13309.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73490
diff changeset
353
bcd347cd6bf2 Use unittest.skipUnless to skip the test related to the glibc bug, issue #13309.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73490
diff changeset
354 # Issue #13309: passing extreme values to mktime() or localtime()
bcd347cd6bf2 Use unittest.skipUnless to skip the test related to the glibc bug, issue #13309.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73490
diff changeset
355 # borks the glibc's internal timezone data.
bcd347cd6bf2 Use unittest.skipUnless to skip the test related to the glibc bug, issue #13309.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73490
diff changeset
356 @unittest.skipUnless(platform.libc_ver()[0] != 'glibc',
bcd347cd6bf2 Use unittest.skipUnless to skip the test related to the glibc bug, issue #13309.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73490
diff changeset
357 "disabled because of a bug in glibc. Issue #13309")
bcd347cd6bf2 Use unittest.skipUnless to skip the test related to the glibc bug, issue #13309.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73490
diff changeset
358 def test_mktime_error(self):
bcd347cd6bf2 Use unittest.skipUnless to skip the test related to the glibc bug, issue #13309.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73490
diff changeset
359 # It may not be possible to reliably make mktime return error
bcd347cd6bf2 Use unittest.skipUnless to skip the test related to the glibc bug, issue #13309.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73490
diff changeset
360 # on all platfom. This will make sure that no other exception
bcd347cd6bf2 Use unittest.skipUnless to skip the test related to the glibc bug, issue #13309.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73490
diff changeset
361 # than OverflowError is raised for an extreme value.
73287
5b1e1967ea9d Replace temporary tests with the real test case for issue #13309 on Gentoo.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73283
diff changeset
362 tt = time.gmtime(self.t)
5b1e1967ea9d Replace temporary tests with the real test case for issue #13309 on Gentoo.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73283
diff changeset
363 tzname = time.strftime('%Z', tt)
5b1e1967ea9d Replace temporary tests with the real test case for issue #13309 on Gentoo.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73283
diff changeset
364 self.assertNotEqual(tzname, 'LMT')
73281
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
365 try:
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
366 time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
367 except OverflowError:
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
368 pass
73505
bcd347cd6bf2 Use unittest.skipUnless to skip the test related to the glibc bug, issue #13309.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73490
diff changeset
369 self.assertEqual(time.strftime('%Z', tt), tzname)
73281
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
370
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
371 @unittest.skipUnless(hasattr(time, 'monotonic'),
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
372 'need time.monotonic')
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
373 def test_monotonic(self):
87379
716e41100553 Issue #19715: Ensure that consecutive calls to monotonic() are monotonic
Victor Stinner <victor.stinner@gmail.com>
parents: 84436
diff changeset
374 # monotonic() should not go backward
716e41100553 Issue #19715: Ensure that consecutive calls to monotonic() are monotonic
Victor Stinner <victor.stinner@gmail.com>
parents: 84436
diff changeset
375 times = [time.monotonic() for n in range(100)]
716e41100553 Issue #19715: Ensure that consecutive calls to monotonic() are monotonic
Victor Stinner <victor.stinner@gmail.com>
parents: 84436
diff changeset
376 t1 = times[0]
716e41100553 Issue #19715: Ensure that consecutive calls to monotonic() are monotonic
Victor Stinner <victor.stinner@gmail.com>
parents: 84436
diff changeset
377 for t2 in times[1:]:
716e41100553 Issue #19715: Ensure that consecutive calls to monotonic() are monotonic
Victor Stinner <victor.stinner@gmail.com>
parents: 84436
diff changeset
378 self.assertGreaterEqual(t2, t1, "times=%s" % times)
716e41100553 Issue #19715: Ensure that consecutive calls to monotonic() are monotonic
Victor Stinner <victor.stinner@gmail.com>
parents: 84436
diff changeset
379 t1 = t2
716e41100553 Issue #19715: Ensure that consecutive calls to monotonic() are monotonic
Victor Stinner <victor.stinner@gmail.com>
parents: 84436
diff changeset
380
716e41100553 Issue #19715: Ensure that consecutive calls to monotonic() are monotonic
Victor Stinner <victor.stinner@gmail.com>
parents: 84436
diff changeset
381 # monotonic() includes time elapsed during a sleep
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
382 t1 = time.monotonic()
84436
13e154067de3 test_time.test_monotonic(): use a longer sleep to try to make the test more reliable
Victor Stinner <victor.stinner@gmail.com>
parents: 83249
diff changeset
383 time.sleep(0.5)
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
384 t2 = time.monotonic()
74820
376ce937823c Issue #13846: Add time.monotonic(), monotonic clock.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74683
diff changeset
385 dt = t2 - t1
74534
8502a9236c2e Issue #10278: Be more explicit in tests than wallclock() is monotonic (cannot
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74513
diff changeset
386 self.assertGreater(t2, t1)
84436
13e154067de3 test_time.test_monotonic(): use a longer sleep to try to make the test more reliable
Victor Stinner <victor.stinner@gmail.com>
parents: 83249
diff changeset
387 self.assertAlmostEqual(dt, 0.5, delta=0.2)
74513
83e8c3a6a81c Be more lenient in test_wallclock (issue #10278).
Antoine Pitrou <solipsis@pitrou.net>
parents: 74475
diff changeset
388
87379
716e41100553 Issue #19715: Ensure that consecutive calls to monotonic() are monotonic
Victor Stinner <victor.stinner@gmail.com>
parents: 84436
diff changeset
389 # monotonic() is a monotonic but non adjustable clock
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
390 info = time.get_clock_info('monotonic')
76700
bec267540811 don't use assertEqual for test for bool equality
Benjamin Peterson <benjamin@python.org>
parents: 76695
diff changeset
391 self.assertTrue(info.monotonic)
77416
26e2ee402a0b Fix test_time for adjusted/adjustable changes
Victor Stinner <victor.stinner@gmail.com>
parents: 77415
diff changeset
392 self.assertFalse(info.adjustable)
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
393
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
394 def test_perf_counter(self):
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
395 time.perf_counter()
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
396
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
397 def test_process_time(self):
77291
bdc7ad1f05ef Fix sporadic failure of test_time.test_process_time() on Windows
Victor Stinner <victor.stinner@gmail.com>
parents: 76700
diff changeset
398 # process_time() should not include time spend during a sleep
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
399 start = time.process_time()
77291
bdc7ad1f05ef Fix sporadic failure of test_time.test_process_time() on Windows
Victor Stinner <victor.stinner@gmail.com>
parents: 76700
diff changeset
400 time.sleep(0.100)
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
401 stop = time.process_time()
77291
bdc7ad1f05ef Fix sporadic failure of test_time.test_process_time() on Windows
Victor Stinner <victor.stinner@gmail.com>
parents: 76700
diff changeset
402 # use 20 ms because process_time() has usually a resolution of 15 ms
bdc7ad1f05ef Fix sporadic failure of test_time.test_process_time() on Windows
Victor Stinner <victor.stinner@gmail.com>
parents: 76700
diff changeset
403 # on Windows
bdc7ad1f05ef Fix sporadic failure of test_time.test_process_time() on Windows
Victor Stinner <victor.stinner@gmail.com>
parents: 76700
diff changeset
404 self.assertLess(stop - start, 0.020)
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
405
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
406 info = time.get_clock_info('process_time')
76700
bec267540811 don't use assertEqual for test for bool equality
Benjamin Peterson <benjamin@python.org>
parents: 76695
diff changeset
407 self.assertTrue(info.monotonic)
77415
0e46e0cd368f PEP 418: Rename adjusted attribute to adjustable in time.get_clock_info() result
Victor Stinner <victor.stinner@gmail.com>
parents: 77291
diff changeset
408 self.assertFalse(info.adjustable)
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
409
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
410 @unittest.skipUnless(hasattr(time, 'monotonic'),
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
411 'need time.monotonic')
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
412 @unittest.skipUnless(hasattr(time, 'clock_settime'),
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
413 'need time.clock_settime')
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
414 def test_monotonic_settime(self):
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
415 t1 = time.monotonic()
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
416 realtime = time.clock_gettime(time.CLOCK_REALTIME)
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
417 # jump backward with an offset of 1 hour
75672
27441e0d6a75 Issue #10278: Add an optional strict argument to time.steady(), False by default
Victor Stinner <victor.stinner@gmail.com>
parents: 75671
diff changeset
418 try:
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
419 time.clock_settime(time.CLOCK_REALTIME, realtime - 3600)
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
420 except PermissionError as err:
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
421 self.skipTest(err)
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
422 t2 = time.monotonic()
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
423 time.clock_settime(time.CLOCK_REALTIME, realtime)
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
424 # monotonic must not be affected by system clock updates
75672
27441e0d6a75 Issue #10278: Add an optional strict argument to time.steady(), False by default
Victor Stinner <victor.stinner@gmail.com>
parents: 75671
diff changeset
425 self.assertGreaterEqual(t2, t1)
27441e0d6a75 Issue #10278: Add an optional strict argument to time.steady(), False by default
Victor Stinner <victor.stinner@gmail.com>
parents: 75671
diff changeset
426
74634
9ee4a104e33d Issue #13847: time.localtime() and time.gmtime() now raise an OSError instead
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74587
diff changeset
427 def test_localtime_failure(self):
9ee4a104e33d Issue #13847: time.localtime() and time.gmtime() now raise an OSError instead
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74587
diff changeset
428 # Issue #13847: check for localtime() failure
74642
856f0864437a Issue #13847: Make test_localtime_failure() more robust
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74641
diff changeset
429 invalid_time_t = None
856f0864437a Issue #13847: Make test_localtime_failure() more robust
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74641
diff changeset
430 for time_t in (-1, 2**30, 2**33, 2**60):
856f0864437a Issue #13847: Make test_localtime_failure() more robust
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74641
diff changeset
431 try:
856f0864437a Issue #13847: Make test_localtime_failure() more robust
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74641
diff changeset
432 time.localtime(time_t)
75590
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
433 except OverflowError:
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
434 self.skipTest("need 64-bit time_t")
74642
856f0864437a Issue #13847: Make test_localtime_failure() more robust
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74641
diff changeset
435 except OSError:
856f0864437a Issue #13847: Make test_localtime_failure() more robust
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74641
diff changeset
436 invalid_time_t = time_t
856f0864437a Issue #13847: Make test_localtime_failure() more robust
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74641
diff changeset
437 break
856f0864437a Issue #13847: Make test_localtime_failure() more robust
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74641
diff changeset
438 if invalid_time_t is None:
856f0864437a Issue #13847: Make test_localtime_failure() more robust
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74641
diff changeset
439 self.skipTest("unable to find an invalid time_t value")
856f0864437a Issue #13847: Make test_localtime_failure() more robust
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74641
diff changeset
440
74634
9ee4a104e33d Issue #13847: time.localtime() and time.gmtime() now raise an OSError instead
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74587
diff changeset
441 self.assertRaises(OSError, time.localtime, invalid_time_t)
9ee4a104e33d Issue #13847: time.localtime() and time.gmtime() now raise an OSError instead
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74587
diff changeset
442 self.assertRaises(OSError, time.ctime, invalid_time_t)
74475
bb10cd354e49 Close #10278: Add time.wallclock() function, monotonic clock.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73921
diff changeset
443
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
444 def test_get_clock_info(self):
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
445 clocks = ['clock', 'perf_counter', 'process_time', 'time']
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
446 if hasattr(time, 'monotonic'):
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
447 clocks.append('monotonic')
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
448
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
449 for name in clocks:
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
450 info = time.get_clock_info(name)
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
451 #self.assertIsInstance(info, dict)
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
452 self.assertIsInstance(info.implementation, str)
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
453 self.assertNotEqual(info.implementation, '')
76695
47c84e3f08ae strip is_ prefixes on clock_info fields
Benjamin Peterson <benjamin@python.org>
parents: 76654
diff changeset
454 self.assertIsInstance(info.monotonic, bool)
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
455 self.assertIsInstance(info.resolution, float)
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
456 # 0.0 < resolution <= 1.0
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
457 self.assertGreater(info.resolution, 0.0)
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
458 self.assertLessEqual(info.resolution, 1.0)
77415
0e46e0cd368f PEP 418: Rename adjusted attribute to adjustable in time.get_clock_info() result
Victor Stinner <victor.stinner@gmail.com>
parents: 77291
diff changeset
459 self.assertIsInstance(info.adjustable, bool)
76597
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
460
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
461 self.assertRaises(ValueError, time.get_clock_info, 'xxx')
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
462
76d2e0761d18 Issue #14428, #14397: Implement the PEP 418
Victor Stinner <victor.stinner@gmail.com>
parents: 76080
diff changeset
463
54442
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 52474
diff changeset
464 class TestLocale(unittest.TestCase):
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 52474
diff changeset
465 def setUp(self):
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 52474
diff changeset
466 self.oldloc = locale.setlocale(locale.LC_ALL)
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 52474
diff changeset
467
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 52474
diff changeset
468 def tearDown(self):
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 52474
diff changeset
469 locale.setlocale(locale.LC_ALL, self.oldloc)
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 52474
diff changeset
470
54443
f5d3ff03dd5b 5562 -> 3061.
Martin v. Löwis <martin@v.loewis.de>
parents: 54442
diff changeset
471 def test_bug_3061(self):
54442
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 52474
diff changeset
472 try:
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 52474
diff changeset
473 tmp = locale.setlocale(locale.LC_ALL, "fr_FR")
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 52474
diff changeset
474 except locale.Error:
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 52474
diff changeset
475 # skip this test
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 52474
diff changeset
476 return
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 52474
diff changeset
477 # This should not cause an exception
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 52474
diff changeset
478 time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 52474
diff changeset
479
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
480
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
481 class _TestAsctimeYear:
73272
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
482 _format = '%d'
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
483
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
484 def yearstr(self, y):
67436
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
485 return time.asctime((y,) + (0,) * 8).split()[-1]
67390
92f61a692e39 - time.accept2dyear = True is now equivalent to time.accept2dyear = 1
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67373
diff changeset
486
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
487 def test_large_year(self):
67453
253d91e4f672 Issue #1777412: test large years value for strftime('%Y')
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67452
diff changeset
488 # Check that it doesn't crash for year > 9999
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
489 self.assertEqual(self.yearstr(12345), '12345')
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
490 self.assertEqual(self.yearstr(123456789), '123456789')
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
491
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
492 class _TestStrftimeYear:
73272
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
493
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
494 # Issue 13305: For years < 1000, the value is not always
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
495 # padded to 4 digits across platforms. The C standard
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
496 # assumes year >= 1900, so it does not specify the number
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
497 # of digits.
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
498
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
499 if time.strftime('%Y', (1,) + (0,) * 8) == '0001':
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
500 _format = '%04d'
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
501 else:
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
502 _format = '%d'
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
503
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
504 def yearstr(self, y):
73272
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
505 return time.strftime('%Y', (y,) + (0,) * 8)
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
506
73272
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
507 def test_4dyear(self):
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
508 # Check that we can return the zero padded value.
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
509 if self._format == '%04d':
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
510 self.test_year('%04d')
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
511 else:
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
512 def year4d(y):
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
513 return time.strftime('%4Y', (y,) + (0,) * 8)
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
514 self.test_year('%04d', func=year4d)
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
515
73281
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
516 def skip_if_not_supported(y):
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
517 msg = "strftime() is limited to [1; 9999] with Visual Studio"
67453
253d91e4f672 Issue #1777412: test large years value for strftime('%Y')
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67452
diff changeset
518 # Check that it doesn't crash for year > 9999
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
519 try:
73281
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
520 time.strftime('%Y', (y,) + (0,) * 8)
67453
253d91e4f672 Issue #1777412: test large years value for strftime('%Y')
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67452
diff changeset
521 except ValueError:
73281
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
522 cond = False
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
523 else:
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
524 cond = True
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
525 return unittest.skipUnless(cond, msg)
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
526
73281
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
527 @skip_if_not_supported(10000)
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
528 def test_large_year(self):
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
529 return super().test_large_year()
67390
92f61a692e39 - time.accept2dyear = True is now equivalent to time.accept2dyear = 1
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67373
diff changeset
530
73281
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
531 @skip_if_not_supported(0)
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
532 def test_negative(self):
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
533 return super().test_negative()
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
534
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
535 del skip_if_not_supported
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
536
67390
92f61a692e39 - time.accept2dyear = True is now equivalent to time.accept2dyear = 1
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67373
diff changeset
537
83249
20efcabcd5c0 #17690: test_time now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83091
diff changeset
538 class _Test4dYear:
73272
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
539 _format = '%d'
67390
92f61a692e39 - time.accept2dyear = True is now equivalent to time.accept2dyear = 1
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67373
diff changeset
540
73272
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
541 def test_year(self, fmt=None, func=None):
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
542 fmt = fmt or self._format
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
543 func = func or self.yearstr
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
544 self.assertEqual(func(1), fmt % 1)
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
545 self.assertEqual(func(68), fmt % 68)
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
546 self.assertEqual(func(69), fmt % 69)
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
547 self.assertEqual(func(99), fmt % 99)
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
548 self.assertEqual(func(999), fmt % 999)
230f0956aaa3 Strengthen the tests for format '%Y', in relation with issue #13305.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73126
diff changeset
549 self.assertEqual(func(9999), fmt % 9999)
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
550
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
551 def test_large_year(self):
73281
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
552 self.assertEqual(self.yearstr(12345), '12345')
68781
d56eb817b181 Issue #10864 has been fixed: remove the workaround
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
553 self.assertEqual(self.yearstr(123456789), '123456789')
73281
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
554 self.assertEqual(self.yearstr(TIME_MAXYEAR), str(TIME_MAXYEAR))
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
555 self.assertRaises(OverflowError, self.yearstr, TIME_MAXYEAR + 1)
67436
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
556
67455
e480604767c4 Issue #1777412: Remove all limits on tm_year from time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67454
diff changeset
557 def test_negative(self):
73281
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
558 self.assertEqual(self.yearstr(-1), self._format % -1)
67455
e480604767c4 Issue #1777412: Remove all limits on tm_year from time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67454
diff changeset
559 self.assertEqual(self.yearstr(-1234), '-1234')
e480604767c4 Issue #1777412: Remove all limits on tm_year from time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67454
diff changeset
560 self.assertEqual(self.yearstr(-123456), '-123456')
73294
f666ecd5c2c5 Additional tests for negative years.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73288
diff changeset
561 self.assertEqual(self.yearstr(-123456789), str(-123456789))
f666ecd5c2c5 Additional tests for negative years.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73288
diff changeset
562 self.assertEqual(self.yearstr(-1234567890), str(-1234567890))
73300
1a0bfc26af57 Issue #13312: skip the failing negative years for now.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73299
diff changeset
563 self.assertEqual(self.yearstr(TIME_MINYEAR + 1900), str(TIME_MINYEAR + 1900))
1a0bfc26af57 Issue #13312: skip the failing negative years for now.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73299
diff changeset
564 # Issue #13312: it may return wrong value for year < TIME_MINYEAR + 1900
1a0bfc26af57 Issue #13312: skip the failing negative years for now.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73299
diff changeset
565 # Skip the value test, but check that no error is raised
1a0bfc26af57 Issue #13312: skip the failing negative years for now.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73299
diff changeset
566 self.yearstr(TIME_MINYEAR)
73298
9cb1b85237a9 Issue #13312: skip the single failing value for now.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73294
diff changeset
567 # self.assertEqual(self.yearstr(TIME_MINYEAR), str(TIME_MINYEAR))
73281
55a3b563f0db Test the year range supported by time.strftime() and time.asctime().
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73272
diff changeset
568 self.assertRaises(OverflowError, self.yearstr, TIME_MINYEAR - 1)
67522
d70522cc9d4f Issue #1726687: time.mktime() will now correctly compute value one
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67455
diff changeset
569
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
570
83249
20efcabcd5c0 #17690: test_time now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83091
diff changeset
571 class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear, unittest.TestCase):
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
572 pass
67390
92f61a692e39 - time.accept2dyear = True is now equivalent to time.accept2dyear = 1
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67373
diff changeset
573
83249
20efcabcd5c0 #17690: test_time now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83091
diff changeset
574 class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear, unittest.TestCase):
67455
e480604767c4 Issue #1777412: Remove all limits on tm_year from time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67454
diff changeset
575 pass
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
576
67451
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67436
diff changeset
577
75372
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
578 class TestPytime(unittest.TestCase):
75590
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
579 def setUp(self):
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
580 self.invalid_values = (
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
581 -(2 ** 100), 2 ** 100,
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
582 -(2.0 ** 100.0), 2.0 ** 100.0,
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
583 )
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
584
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
585 def test_time_t(self):
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
586 from _testcapi import pytime_object_to_time_t
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
587 for obj, time_t in (
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
588 (0, 0),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
589 (-1, -1),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
590 (-1.0, -1),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
591 (-1.9, -1),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
592 (1.0, 1),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
593 (1.9, 1),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
594 ):
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
595 self.assertEqual(pytime_object_to_time_t(obj), time_t)
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
596
75590
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
597 for invalid in self.invalid_values:
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
598 self.assertRaises(OverflowError, pytime_object_to_time_t, invalid)
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
599
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
600 def test_timeval(self):
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
601 from _testcapi import pytime_object_to_timeval
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
602 for obj, timeval in (
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
603 (0, (0, 0)),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
604 (-1, (-1, 0)),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
605 (-1.0, (-1, 0)),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
606 (1e-6, (0, 1)),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
607 (-1e-6, (-1, 999999)),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
608 (-1.2, (-2, 800000)),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
609 (1.1234560, (1, 123456)),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
610 (1.1234569, (1, 123456)),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
611 (-1.1234560, (-2, 876544)),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
612 (-1.1234561, (-2, 876543)),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
613 ):
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
614 self.assertEqual(pytime_object_to_timeval(obj), timeval)
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
615
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
616 for invalid in self.invalid_values:
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
617 self.assertRaises(OverflowError, pytime_object_to_timeval, invalid)
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
618
75372
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
619 def test_timespec(self):
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
620 from _testcapi import pytime_object_to_timespec
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
621 for obj, timespec in (
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
622 (0, (0, 0)),
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
623 (-1, (-1, 0)),
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
624 (-1.0, (-1, 0)),
75590
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
625 (1e-9, (0, 1)),
75372
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
626 (-1e-9, (-1, 999999999)),
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
627 (-1.2, (-2, 800000000)),
75590
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
628 (1.1234567890, (1, 123456789)),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
629 (1.1234567899, (1, 123456789)),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
630 (-1.1234567890, (-2, 876543211)),
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
631 (-1.1234567891, (-2, 876543210)),
75372
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
632 ):
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
633 self.assertEqual(pytime_object_to_timespec(obj), timespec)
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
634
75590
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
635 for invalid in self.invalid_values:
75372
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
636 self.assertRaises(OverflowError, pytime_object_to_timespec, invalid)
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
637
77423
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
638 @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support")
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
639 def test_localtime_timezone(self):
75372
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
640
77423
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
641 # Get the localtime and examine it for the offset and zone.
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
642 lt = time.localtime()
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
643 self.assertTrue(hasattr(lt, "tm_gmtoff"))
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
644 self.assertTrue(hasattr(lt, "tm_zone"))
67451
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67436
diff changeset
645
77423
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
646 # See if the offset and zone are similar to the module
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
647 # attributes.
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
648 if lt.tm_gmtoff is None:
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
649 self.assertTrue(not hasattr(time, "timezone"))
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
650 else:
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
651 self.assertEqual(lt.tm_gmtoff, -[time.timezone, time.altzone][lt.tm_isdst])
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
652 if lt.tm_zone is None:
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
653 self.assertTrue(not hasattr(time, "tzname"))
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
654 else:
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
655 self.assertEqual(lt.tm_zone, time.tzname[lt.tm_isdst])
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
656
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
657 # Try and make UNIX times from the localtime and a 9-tuple
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
658 # created from the localtime. Test to see that the times are
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
659 # the same.
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
660 t = time.mktime(lt); t9 = time.mktime(lt[:9])
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
661 self.assertEqual(t, t9)
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
662
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
663 # Make localtimes from the UNIX times and compare them to
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
664 # the original localtime, thus making a round trip.
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
665 new_lt = time.localtime(t); new_lt9 = time.localtime(t9)
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
666 self.assertEqual(new_lt, lt)
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
667 self.assertEqual(new_lt.tm_gmtoff, lt.tm_gmtoff)
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
668 self.assertEqual(new_lt.tm_zone, lt.tm_zone)
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
669 self.assertEqual(new_lt9, lt)
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
670 self.assertEqual(new_lt.tm_gmtoff, lt.tm_gmtoff)
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
671 self.assertEqual(new_lt9.tm_zone, lt.tm_zone)
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
672
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
673 @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support")
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
674 def test_strptime_timezone(self):
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
675 t = time.strptime("UTC", "%Z")
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
676 self.assertEqual(t.tm_zone, 'UTC')
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
677 t = time.strptime("+0500", "%z")
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
678 self.assertEqual(t.tm_gmtoff, 5 * 3600)
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
679
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
680 @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support")
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
681 def test_short_times(self):
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
682
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
683 import pickle
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
684
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
685 # Load a short time structure using pickle.
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
686 st = b"ctime\nstruct_time\np0\n((I2007\nI8\nI11\nI1\nI24\nI49\nI5\nI223\nI1\ntp1\n(dp2\ntp3\nRp4\n."
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
687 lt = pickle.loads(st)
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
688 self.assertIs(lt.tm_gmtoff, None)
3b5545ba6432 Issue #1667546: On platforms supporting tm_zone and tm_gmtoff fields
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 77416
diff changeset
689 self.assertIs(lt.tm_zone, None)
67451
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67436
diff changeset
690
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17912
diff changeset
691
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17912
diff changeset
692 if __name__ == "__main__":
83249
20efcabcd5c0 #17690: test_time now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 83091
diff changeset
693 unittest.main()