2002-07-23 16:04:11 -03:00
|
|
|
from test import test_support
|
1996-12-06 19:30:07 -04:00
|
|
|
import time
|
2001-05-22 14:02:02 -03:00
|
|
|
import unittest
|
2011-04-06 03:27:47 -03:00
|
|
|
import sys
|
1996-12-06 19:30:07 -04:00
|
|
|
|
2001-05-22 14:02:02 -03:00
|
|
|
|
|
|
|
class TimeTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.t = time.time()
|
|
|
|
|
|
|
|
def test_data_attributes(self):
|
|
|
|
time.altzone
|
|
|
|
time.daylight
|
|
|
|
time.timezone
|
|
|
|
time.tzname
|
|
|
|
|
|
|
|
def test_clock(self):
|
|
|
|
time.clock()
|
|
|
|
|
|
|
|
def test_conversions(self):
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(time.ctime(self.t)
|
2001-05-22 14:02:02 -03:00
|
|
|
== time.asctime(time.localtime(self.t)))
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(long(time.mktime(time.localtime(self.t)))
|
2001-05-22 14:02:02 -03:00
|
|
|
== long(self.t))
|
|
|
|
|
|
|
|
def test_sleep(self):
|
|
|
|
time.sleep(1.2)
|
|
|
|
|
|
|
|
def test_strftime(self):
|
|
|
|
tt = time.gmtime(self.t)
|
|
|
|
for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
|
|
|
|
'j', 'm', 'M', 'p', 'S',
|
|
|
|
'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
|
|
|
|
format = ' %' + directive
|
|
|
|
try:
|
|
|
|
time.strftime(format, tt)
|
|
|
|
except ValueError:
|
|
|
|
self.fail('conversion specifier: %r failed.' % format)
|
|
|
|
|
2011-04-06 03:27:47 -03:00
|
|
|
# Issue #10762: Guard against invalid/non-supported format string
|
|
|
|
# so that Python don't crash (Windows crashes when the format string
|
|
|
|
# input to [w]strftime is not kosher.
|
|
|
|
if sys.platform.startswith('win'):
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
time.strftime('%f')
|
|
|
|
|
2004-03-02 00:38:10 -04:00
|
|
|
def test_strftime_bounds_checking(self):
|
|
|
|
# Make sure that strftime() checks the bounds of the various parts
|
2006-07-18 01:41:36 -03:00
|
|
|
#of the time tuple (0 is valid for *all* values).
|
2004-03-02 00:38:10 -04:00
|
|
|
|
2006-07-18 01:41:36 -03:00
|
|
|
# Check year [1900, max(int)]
|
2004-03-02 00:38:10 -04:00
|
|
|
self.assertRaises(ValueError, time.strftime, '',
|
|
|
|
(1899, 1, 1, 0, 0, 0, 0, 1, -1))
|
|
|
|
if time.accept2dyear:
|
|
|
|
self.assertRaises(ValueError, time.strftime, '',
|
|
|
|
(-1, 1, 1, 0, 0, 0, 0, 1, -1))
|
|
|
|
self.assertRaises(ValueError, time.strftime, '',
|
|
|
|
(100, 1, 1, 0, 0, 0, 0, 1, -1))
|
2006-07-18 01:41:36 -03:00
|
|
|
# Check month [1, 12] + zero support
|
2004-03-02 00:38:10 -04:00
|
|
|
self.assertRaises(ValueError, time.strftime, '',
|
2006-07-18 01:41:36 -03:00
|
|
|
(1900, -1, 1, 0, 0, 0, 0, 1, -1))
|
2004-03-02 00:38:10 -04:00
|
|
|
self.assertRaises(ValueError, time.strftime, '',
|
|
|
|
(1900, 13, 1, 0, 0, 0, 0, 1, -1))
|
2006-07-18 01:41:36 -03:00
|
|
|
# Check day of month [1, 31] + zero support
|
2004-03-02 00:38:10 -04:00
|
|
|
self.assertRaises(ValueError, time.strftime, '',
|
2006-07-18 01:41:36 -03:00
|
|
|
(1900, 1, -1, 0, 0, 0, 0, 1, -1))
|
2004-03-02 00:38:10 -04:00
|
|
|
self.assertRaises(ValueError, time.strftime, '',
|
|
|
|
(1900, 1, 32, 0, 0, 0, 0, 1, -1))
|
2006-07-18 01:41:36 -03:00
|
|
|
# Check hour [0, 23]
|
2004-03-02 00:38:10 -04:00
|
|
|
self.assertRaises(ValueError, time.strftime, '',
|
|
|
|
(1900, 1, 1, -1, 0, 0, 0, 1, -1))
|
|
|
|
self.assertRaises(ValueError, time.strftime, '',
|
|
|
|
(1900, 1, 1, 24, 0, 0, 0, 1, -1))
|
2006-07-18 01:41:36 -03:00
|
|
|
# Check minute [0, 59]
|
2004-03-02 00:38:10 -04:00
|
|
|
self.assertRaises(ValueError, time.strftime, '',
|
|
|
|
(1900, 1, 1, 0, -1, 0, 0, 1, -1))
|
|
|
|
self.assertRaises(ValueError, time.strftime, '',
|
|
|
|
(1900, 1, 1, 0, 60, 0, 0, 1, -1))
|
2006-07-18 01:41:36 -03:00
|
|
|
# Check second [0, 61]
|
2004-03-02 00:38:10 -04:00
|
|
|
self.assertRaises(ValueError, time.strftime, '',
|
|
|
|
(1900, 1, 1, 0, 0, -1, 0, 1, -1))
|
|
|
|
# C99 only requires allowing for one leap second, but Python's docs say
|
|
|
|
# allow two leap seconds (0..61)
|
|
|
|
self.assertRaises(ValueError, time.strftime, '',
|
|
|
|
(1900, 1, 1, 0, 0, 62, 0, 1, -1))
|
|
|
|
# No check for upper-bound day of week;
|
|
|
|
# value forced into range by a ``% 7`` calculation.
|
|
|
|
# Start check at -2 since gettmarg() increments value before taking
|
|
|
|
# modulo.
|
|
|
|
self.assertRaises(ValueError, time.strftime, '',
|
|
|
|
(1900, 1, 1, 0, 0, 0, -2, 1, -1))
|
2006-07-18 01:41:36 -03:00
|
|
|
# Check day of the year [1, 366] + zero support
|
2004-03-02 00:38:10 -04:00
|
|
|
self.assertRaises(ValueError, time.strftime, '',
|
2006-07-18 01:41:36 -03:00
|
|
|
(1900, 1, 1, 0, 0, 0, 0, -1, -1))
|
2004-03-02 00:38:10 -04:00
|
|
|
self.assertRaises(ValueError, time.strftime, '',
|
|
|
|
(1900, 1, 1, 0, 0, 0, 0, 367, -1))
|
|
|
|
|
2006-07-18 01:41:36 -03:00
|
|
|
def test_default_values_for_zero(self):
|
|
|
|
# Make sure that using all zeros uses the proper default values.
|
|
|
|
# No test for daylight savings since strftime() does not change output
|
|
|
|
# based on its value.
|
|
|
|
expected = "2000 01 01 00 00 00 1 001"
|
|
|
|
result = time.strftime("%Y %m %d %H %M %S %w %j", (0,)*9)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(expected, result)
|
2006-07-18 01:41:36 -03:00
|
|
|
|
2002-07-19 14:04:46 -03:00
|
|
|
def test_strptime(self):
|
2006-09-23 16:53:20 -03:00
|
|
|
# Should be able to go round-trip from strftime to strptime without
|
|
|
|
# throwing an exception.
|
2002-07-19 14:04:46 -03:00
|
|
|
tt = time.gmtime(self.t)
|
|
|
|
for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
|
|
|
|
'j', 'm', 'M', 'p', 'S',
|
|
|
|
'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
|
2006-09-23 16:53:20 -03:00
|
|
|
format = '%' + directive
|
|
|
|
strf_output = time.strftime(format, tt)
|
2002-07-19 14:04:46 -03:00
|
|
|
try:
|
2006-09-23 16:53:20 -03:00
|
|
|
time.strptime(strf_output, format)
|
2002-07-19 14:04:46 -03:00
|
|
|
except ValueError:
|
2006-09-23 16:53:20 -03:00
|
|
|
self.fail("conversion specifier %r failed with '%s' input." %
|
|
|
|
(format, strf_output))
|
2002-07-19 14:04:46 -03:00
|
|
|
|
2001-05-22 14:02:02 -03:00
|
|
|
def test_asctime(self):
|
|
|
|
time.asctime(time.gmtime(self.t))
|
|
|
|
self.assertRaises(TypeError, time.asctime, 0)
|
2011-01-02 19:23:54 -04:00
|
|
|
self.assertRaises(TypeError, time.asctime, ())
|
|
|
|
# XXX: Posix compiant asctime should refuse to convert
|
|
|
|
# year > 9999, but Linux implementation does not.
|
|
|
|
# self.assertRaises(ValueError, time.asctime,
|
|
|
|
# (12345, 1, 0, 0, 0, 0, 0, 0, 0))
|
|
|
|
# XXX: For now, just make sure we don't have a crash:
|
|
|
|
try:
|
2011-01-03 13:52:03 -04:00
|
|
|
time.asctime((12345, 1, 1, 0, 0, 0, 0, 1, 0))
|
2011-01-02 19:23:54 -04:00
|
|
|
except ValueError:
|
|
|
|
pass
|
2001-05-22 14:02:02 -03:00
|
|
|
|
2010-12-13 21:28:59 -04:00
|
|
|
@unittest.skipIf(not hasattr(time, "tzset"),
|
|
|
|
"time module has no attribute tzset")
|
2003-03-14 17:51:36 -04:00
|
|
|
def test_tzset(self):
|
2003-03-15 08:01:52 -04:00
|
|
|
|
2003-03-14 17:51:36 -04:00
|
|
|
from os import environ
|
|
|
|
|
2003-04-24 13:02:54 -03:00
|
|
|
# Epoch time of midnight Dec 25th 2002. Never DST in northern
|
2003-03-14 17:51:36 -04:00
|
|
|
# hemisphere.
|
2003-04-24 13:02:54 -03:00
|
|
|
xmas2002 = 1040774400.0
|
2003-03-14 17:51:36 -04:00
|
|
|
|
2003-04-11 12:35:53 -03:00
|
|
|
# These formats are correct for 2002, and possibly future years
|
|
|
|
# This format is the 'standard' as documented at:
|
|
|
|
# http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
|
|
|
|
# They are also documented in the tzset(3) man page on most Unix
|
|
|
|
# systems.
|
2003-04-24 13:02:54 -03:00
|
|
|
eastern = 'EST+05EDT,M4.1.0,M10.5.0'
|
2003-04-11 12:35:53 -03:00
|
|
|
victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
|
|
|
|
utc='UTC+0'
|
|
|
|
|
2003-03-14 17:51:36 -04:00
|
|
|
org_TZ = environ.get('TZ',None)
|
|
|
|
try:
|
|
|
|
# Make sure we can switch to UTC time and results are correct
|
|
|
|
# Note that unknown timezones default to UTC.
|
2003-04-11 12:35:53 -03:00
|
|
|
# Note that altzone is undefined in UTC, as there is no DST
|
|
|
|
environ['TZ'] = eastern
|
2003-03-14 17:51:36 -04:00
|
|
|
time.tzset()
|
2003-04-11 12:35:53 -03:00
|
|
|
environ['TZ'] = utc
|
2003-03-14 17:51:36 -04:00
|
|
|
time.tzset()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(
|
2003-04-11 12:35:53 -03:00
|
|
|
time.gmtime(xmas2002), time.localtime(xmas2002)
|
|
|
|
)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertEqual(time.daylight, 0)
|
|
|
|
self.assertEqual(time.timezone, 0)
|
|
|
|
self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
|
2003-03-14 17:51:36 -04:00
|
|
|
|
2003-04-11 12:35:53 -03:00
|
|
|
# Make sure we can switch to US/Eastern
|
|
|
|
environ['TZ'] = eastern
|
2003-03-14 17:51:36 -04:00
|
|
|
time.tzset()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
|
|
|
|
self.assertEqual(time.tzname, ('EST', 'EDT'))
|
|
|
|
self.assertEqual(len(time.tzname), 2)
|
|
|
|
self.assertEqual(time.daylight, 1)
|
|
|
|
self.assertEqual(time.timezone, 18000)
|
|
|
|
self.assertEqual(time.altzone, 14400)
|
|
|
|
self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
|
|
|
|
self.assertEqual(len(time.tzname), 2)
|
2003-04-11 12:35:53 -03:00
|
|
|
|
|
|
|
# Now go to the southern hemisphere.
|
|
|
|
environ['TZ'] = victoria
|
|
|
|
time.tzset()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
|
2011-12-07 19:32:51 -04:00
|
|
|
|
|
|
|
# Issue #11886: Australian Eastern Standard Time (UTC+10) is called
|
|
|
|
# "EST" (as Eastern Standard Time, UTC-5) instead of "AEST" on some
|
|
|
|
# operating systems (e.g. FreeBSD), which is wrong. See for example
|
|
|
|
# this bug: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=93810
|
|
|
|
self.assertIn(time.tzname[0], ('AEST' 'EST'), time.tzname[0])
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(time.tzname[1] == 'AEDT', str(time.tzname[1]))
|
|
|
|
self.assertEqual(len(time.tzname), 2)
|
|
|
|
self.assertEqual(time.daylight, 1)
|
|
|
|
self.assertEqual(time.timezone, -36000)
|
|
|
|
self.assertEqual(time.altzone, -39600)
|
|
|
|
self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
|
2003-03-14 17:51:36 -04:00
|
|
|
|
|
|
|
finally:
|
|
|
|
# Repair TZ environment variable in case any other tests
|
|
|
|
# rely on it.
|
|
|
|
if org_TZ is not None:
|
|
|
|
environ['TZ'] = org_TZ
|
|
|
|
elif environ.has_key('TZ'):
|
|
|
|
del environ['TZ']
|
2003-04-11 12:35:53 -03:00
|
|
|
time.tzset()
|
2003-04-24 13:02:54 -03:00
|
|
|
|
2004-06-19 23:50:16 -03:00
|
|
|
def test_insane_timestamps(self):
|
|
|
|
# It's possible that some platform maps time_t to double,
|
|
|
|
# and that this test will fail there. This test should
|
|
|
|
# exempt such platforms (provided they return reasonable
|
|
|
|
# results!).
|
|
|
|
for func in time.ctime, time.gmtime, time.localtime:
|
|
|
|
for unreasonable in -1e200, 1e200:
|
|
|
|
self.assertRaises(ValueError, func, unreasonable)
|
2001-05-22 14:02:02 -03:00
|
|
|
|
2004-08-03 14:58:55 -03:00
|
|
|
def test_ctime_without_arg(self):
|
|
|
|
# Not sure how to check the values, since the clock could tick
|
|
|
|
# at any time. Make sure these are at least accepted and
|
|
|
|
# don't raise errors.
|
|
|
|
time.ctime()
|
|
|
|
time.ctime(None)
|
|
|
|
|
|
|
|
def test_gmtime_without_arg(self):
|
2006-06-15 02:55:49 -03:00
|
|
|
gt0 = time.gmtime()
|
|
|
|
gt1 = time.gmtime(None)
|
|
|
|
t0 = time.mktime(gt0)
|
|
|
|
t1 = time.mktime(gt1)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(0 <= (t1-t0) < 0.2)
|
2004-08-03 14:58:55 -03:00
|
|
|
|
|
|
|
def test_localtime_without_arg(self):
|
2006-06-15 02:55:49 -03:00
|
|
|
lt0 = time.localtime()
|
|
|
|
lt1 = time.localtime(None)
|
|
|
|
t0 = time.mktime(lt0)
|
|
|
|
t1 = time.mktime(lt1)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(0 <= (t1-t0) < 0.2)
|
2004-08-03 14:58:55 -03:00
|
|
|
|
2011-02-15 11:51:17 -04:00
|
|
|
def test_mktime(self):
|
|
|
|
# Issue #1726687
|
|
|
|
for t in (-2, -1, 0, 1):
|
|
|
|
try:
|
|
|
|
tt = time.localtime(t)
|
|
|
|
except (OverflowError, ValueError):
|
|
|
|
pass
|
2011-02-15 12:01:11 -04:00
|
|
|
else:
|
|
|
|
self.assertEqual(time.mktime(tt), t)
|
2011-02-15 11:51:17 -04:00
|
|
|
|
|
|
|
|
2001-09-20 18:33:42 -03:00
|
|
|
def test_main():
|
|
|
|
test_support.run_unittest(TimeTestCase)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|