Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions
test.test_time.TimeTestCase Class Reference

List of all members.

Public Member Functions

def setUp
def test_data_attributes
def test_clock
def test_conversions
def test_sleep
def test_strftime
def test_strftime_bounding_check
def test_default_values_for_zero
def test_strptime
def test_strptime_bytes
def test_asctime
def test_asctime_bounding_check
def test_ctime
def test_tzset
def test_insane_timestamps
def test_ctime_without_arg
def test_gmtime_without_arg
def test_localtime_without_arg

Public Attributes

 t

Private Member Functions

def _bounds_checking

Detailed Description

Definition at line 9 of file test_time.py.


Member Function Documentation

def test.test_time.TimeTestCase._bounds_checking (   self,
  func = time.strftime 
) [private]

Definition at line 50 of file test_time.py.

00050 
00051     def _bounds_checking(self, func=time.strftime):
00052         # Make sure that strftime() checks the bounds of the various parts
00053         #of the time tuple (0 is valid for *all* values).
00054 
00055         # The year field is tested by other test cases above
00056 
00057         # Check month [1, 12] + zero support
00058         self.assertRaises(ValueError, func,
00059                             (1900, -1, 1, 0, 0, 0, 0, 1, -1))
00060         self.assertRaises(ValueError, func,
00061                             (1900, 13, 1, 0, 0, 0, 0, 1, -1))
00062         # Check day of month [1, 31] + zero support
00063         self.assertRaises(ValueError, func,
00064                             (1900, 1, -1, 0, 0, 0, 0, 1, -1))
00065         self.assertRaises(ValueError, func,
00066                             (1900, 1, 32, 0, 0, 0, 0, 1, -1))
00067         # Check hour [0, 23]
00068         self.assertRaises(ValueError, func,
00069                             (1900, 1, 1, -1, 0, 0, 0, 1, -1))
00070         self.assertRaises(ValueError, func,
00071                             (1900, 1, 1, 24, 0, 0, 0, 1, -1))
00072         # Check minute [0, 59]
00073         self.assertRaises(ValueError, func,
00074                             (1900, 1, 1, 0, -1, 0, 0, 1, -1))
00075         self.assertRaises(ValueError, func,
00076                             (1900, 1, 1, 0, 60, 0, 0, 1, -1))
00077         # Check second [0, 61]
00078         self.assertRaises(ValueError, func,
00079                             (1900, 1, 1, 0, 0, -1, 0, 1, -1))
00080         # C99 only requires allowing for one leap second, but Python's docs say
00081         # allow two leap seconds (0..61)
00082         self.assertRaises(ValueError, func,
00083                             (1900, 1, 1, 0, 0, 62, 0, 1, -1))
00084         # No check for upper-bound day of week;
00085         #  value forced into range by a ``% 7`` calculation.
00086         # Start check at -2 since gettmarg() increments value before taking
00087         #  modulo.
00088         self.assertRaises(ValueError, func,
00089                             (1900, 1, 1, 0, 0, 0, -2, 1, -1))
00090         # Check day of the year [1, 366] + zero support
00091         self.assertRaises(ValueError, func,
00092                             (1900, 1, 1, 0, 0, 0, 0, -1, -1))
00093         self.assertRaises(ValueError, func,
00094                             (1900, 1, 1, 0, 0, 0, 0, 367, -1))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 11 of file test_time.py.

00011 
00012     def setUp(self):
00013         self.t = time.time()

Here is the caller graph for this function:

Definition at line 127 of file test_time.py.

00127 
00128     def test_asctime(self):
00129         time.asctime(time.gmtime(self.t))
00130 
00131         # Max year is only limited by the size of C int.
00132         sizeof_int = sysconfig.get_config_var('SIZEOF_INT') or 4
00133         bigyear = (1 << 8 * sizeof_int - 1) - 1
00134         asc = time.asctime((bigyear, 6, 1) + (0,)*6)
00135         self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
00136         self.assertRaises(OverflowError, time.asctime, (bigyear + 1,) + (0,)*8)
00137         self.assertRaises(TypeError, time.asctime, 0)
00138         self.assertRaises(TypeError, time.asctime, ())
00139         self.assertRaises(TypeError, time.asctime, (0,) * 10)

Here is the call graph for this function:

Definition at line 140 of file test_time.py.

00140 
00141     def test_asctime_bounding_check(self):
00142         self._bounds_checking(time.asctime)

Here is the call graph for this function:

Definition at line 20 of file test_time.py.

00020 
00021     def test_clock(self):
00022         time.clock()

Definition at line 23 of file test_time.py.

00023 
00024     def test_conversions(self):
00025         self.assertEqual(time.ctime(self.t),
00026                          time.asctime(time.localtime(self.t)))
00027         self.assertEqual(int(time.mktime(time.localtime(self.t))),
00028                          int(self.t))

Here is the call graph for this function:

Definition at line 143 of file test_time.py.

00143 
00144     def test_ctime(self):
00145         t = time.mktime((1973, 9, 16, 1, 3, 52, 0, 0, -1))
00146         self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973')
00147         t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1))
00148         self.assertEqual(time.ctime(t), 'Sat Jan  1 00:00:00 2000')
00149         for year in [-100, 100, 1000, 2000, 10000]:
00150             try:
00151                 testval = time.mktime((year, 1, 10) + (0,)*6)
00152             except (ValueError, OverflowError):
00153                 # If mktime fails, ctime will fail too.  This may happen
00154                 # on some platforms.
00155                 pass
00156             else:
00157                 self.assertEqual(time.ctime(testval)[20:], str(year))

Here is the call graph for this function:

Definition at line 235 of file test_time.py.

00235 
00236     def test_ctime_without_arg(self):
00237         # Not sure how to check the values, since the clock could tick
00238         # at any time.  Make sure these are at least accepted and
00239         # don't raise errors.
00240         time.ctime()
00241         time.ctime(None)

Definition at line 14 of file test_time.py.

00014 
00015     def test_data_attributes(self):
00016         time.altzone
00017         time.daylight
00018         time.timezone
00019         time.tzname

Definition at line 98 of file test_time.py.

00098 
00099     def test_default_values_for_zero(self):
00100         # Make sure that using all zeros uses the proper default values.
00101         # No test for daylight savings since strftime() does not change output
00102         # based on its value.
00103         expected = "2000 01 01 00 00 00 1 001"
00104         with support.check_warnings():
00105             result = time.strftime("%Y %m %d %H %M %S %w %j", (0,)*9)
00106         self.assertEqual(expected, result)

Here is the call graph for this function:

Definition at line 242 of file test_time.py.

00242 
00243     def test_gmtime_without_arg(self):
00244         gt0 = time.gmtime()
00245         gt1 = time.gmtime(None)
00246         t0 = time.mktime(gt0)
00247         t1 = time.mktime(gt1)
00248         self.assertAlmostEqual(t1, t0, delta=0.2)

Here is the call graph for this function:

Definition at line 226 of file test_time.py.

00226 
00227     def test_insane_timestamps(self):
00228         # It's possible that some platform maps time_t to double,
00229         # and that this test will fail there.  This test should
00230         # exempt such platforms (provided they return reasonable
00231         # results!).
00232         for func in time.ctime, time.gmtime, time.localtime:
00233             for unreasonable in -1e200, 1e200:
00234                 self.assertRaises(ValueError, func, unreasonable)

Here is the call graph for this function:

Definition at line 249 of file test_time.py.

00249 
00250     def test_localtime_without_arg(self):
00251         lt0 = time.localtime()
00252         lt1 = time.localtime(None)
00253         t0 = time.mktime(lt0)
00254         t1 = time.mktime(lt1)
00255         self.assertAlmostEqual(t1, t0, delta=0.2)

Here is the call graph for this function:

Definition at line 29 of file test_time.py.

00029 
00030     def test_sleep(self):
00031         time.sleep(1.2)

Definition at line 32 of file test_time.py.

00032 
00033     def test_strftime(self):
00034         tt = time.gmtime(self.t)
00035         for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
00036                           'j', 'm', 'M', 'p', 'S',
00037                           'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
00038             format = ' %' + directive
00039             try:
00040                 time.strftime(format, tt)
00041             except ValueError:
00042                 self.fail('conversion specifier: %r failed.' % format)
00043 
00044         # Issue #10762: Guard against invalid/non-supported format string
00045         # so that Python don't crash (Windows crashes when the format string
00046         # input to [w]strftime is not kosher.
00047         if sys.platform.startswith('win'):
00048             with self.assertRaises(ValueError):
00049                 time.strftime('%f')

Here is the call graph for this function:

Definition at line 95 of file test_time.py.

00095 
00096     def test_strftime_bounding_check(self):
00097         self._bounds_checking(lambda tup: time.strftime('', tup))

Here is the call graph for this function:

Definition at line 107 of file test_time.py.

00107 
00108     def test_strptime(self):
00109         # Should be able to go round-trip from strftime to strptime without
00110         # throwing an exception.
00111         tt = time.gmtime(self.t)
00112         for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
00113                           'j', 'm', 'M', 'p', 'S',
00114                           'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
00115             format = '%' + directive
00116             strf_output = time.strftime(format, tt)
00117             try:
00118                 time.strptime(strf_output, format)
00119             except ValueError:
00120                 self.fail("conversion specifier %r failed with '%s' input." %
00121                           (format, strf_output))

Here is the call graph for this function:

Definition at line 122 of file test_time.py.

00122 
00123     def test_strptime_bytes(self):
00124         # Make sure only strings are accepted as arguments to strptime.
00125         self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
00126         self.assertRaises(TypeError, time.strptime, '2009', b'%Y')

Here is the call graph for this function:

Definition at line 160 of file test_time.py.

00160 
00161     def test_tzset(self):
00162 
00163         from os import environ
00164 
00165         # Epoch time of midnight Dec 25th 2002. Never DST in northern
00166         # hemisphere.
00167         xmas2002 = 1040774400.0
00168 
00169         # These formats are correct for 2002, and possibly future years
00170         # This format is the 'standard' as documented at:
00171         # http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap08.html
00172         # They are also documented in the tzset(3) man page on most Unix
00173         # systems.
00174         eastern = 'EST+05EDT,M4.1.0,M10.5.0'
00175         victoria = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
00176         utc='UTC+0'
00177 
00178         org_TZ = environ.get('TZ',None)
00179         try:
00180             # Make sure we can switch to UTC time and results are correct
00181             # Note that unknown timezones default to UTC.
00182             # Note that altzone is undefined in UTC, as there is no DST
00183             environ['TZ'] = eastern
00184             time.tzset()
00185             environ['TZ'] = utc
00186             time.tzset()
00187             self.assertEqual(
00188                 time.gmtime(xmas2002), time.localtime(xmas2002)
00189                 )
00190             self.assertEqual(time.daylight, 0)
00191             self.assertEqual(time.timezone, 0)
00192             self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
00193 
00194             # Make sure we can switch to US/Eastern
00195             environ['TZ'] = eastern
00196             time.tzset()
00197             self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
00198             self.assertEqual(time.tzname, ('EST', 'EDT'))
00199             self.assertEqual(len(time.tzname), 2)
00200             self.assertEqual(time.daylight, 1)
00201             self.assertEqual(time.timezone, 18000)
00202             self.assertEqual(time.altzone, 14400)
00203             self.assertEqual(time.localtime(xmas2002).tm_isdst, 0)
00204             self.assertEqual(len(time.tzname), 2)
00205 
00206             # Now go to the southern hemisphere.
00207             environ['TZ'] = victoria
00208             time.tzset()
00209             self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
00210             self.assertTrue(time.tzname[0] == 'AEST', str(time.tzname[0]))
00211             self.assertTrue(time.tzname[1] == 'AEDT', str(time.tzname[1]))
00212             self.assertEqual(len(time.tzname), 2)
00213             self.assertEqual(time.daylight, 1)
00214             self.assertEqual(time.timezone, -36000)
00215             self.assertEqual(time.altzone, -39600)
00216             self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
00217 
00218         finally:
00219             # Repair TZ environment variable in case any other tests
00220             # rely on it.
00221             if org_TZ is not None:
00222                 environ['TZ'] = org_TZ
00223             elif 'TZ' in environ:
00224                 del environ['TZ']
00225             time.tzset()

Here is the call graph for this function:


Member Data Documentation

Definition at line 12 of file test_time.py.


The documentation for this class was generated from the following file: