def test_to_universal_without_tzinfo(self): """Convert local dates without timezone info to universal date""" # Same as above, but with tzinfo stripped off (as if a NY and Amsterdam # user used datetime.now()) ny_time = self.time_in_ny.replace(tzinfo=None) ams_time = self.time_in_ams.replace(tzinfo=None) # When time has no tzinfo attached, it should be specified explicitly est = 'EST' self.assertEquals(times.to_universal(ny_time, est), self.sometime_univ) # ...or simply with a string self.assertEquals(times.to_universal(ams_time, 'Europe/Amsterdam'), self.sometime_univ)
def test_to_universal_with_unix_timestamp(self): """Convert UNIX timestamps to universal date""" unix_time = 1328257004.456 # as returned by time.time() self.assertEquals( times.to_universal(unix_time), pytz.utc.localize(datetime(2012, 2, 3, 8, 16, 44, 456000)) )
def test_to_universal_with_tzinfo(self): # noqa """Convert local dates with timezone info to universal date""" ny_time = self.time_in_ny ams_time = self.time_in_ams self.assertEquals(times.to_universal(ny_time), self.sometime_univ) self.assertEquals(times.to_universal(ams_time), self.sometime_univ) self.assertEquals(ny_time.hour, 6) self.assertEquals(times.to_universal(ny_time).hour, 11) self.assertEquals(ams_time.hour, 12) self.assertEquals(times.to_universal(ams_time).hour, 11) # Test alias from_local, too self.assertEquals(times.from_local(ny_time), self.sometime_univ)
def test_to_universal_with_string(self): dt = self.sometime_univ # Timezone-aware strings self.assertEquals(dt, times.to_universal('2012-02-02 00:56:31+13:00')) self.assertEquals(dt, times.to_universal('2012-02-01 12:56:31+01:00')) self.assertEquals(dt, times.to_universal('2012-02-01 06:56:31-05:00')) # Timezone-less strings require explicit source timezone self.assertEquals(dt, times.to_universal('2012-02-02 00:56:31', 'Pacific/Auckland')) self.assertEquals(dt, times.to_universal('2012-02-01 12:56:31', 'CET')) self.assertEquals(dt, times.to_universal('2012-02-01 06:56:31', 'EST')) # Without a timezone, UTC is assumed self.assertEquals(dt, times.to_universal('2012-02-01 11:56:31'))
def test_to_universal_rejects_non_date_arguments(self): """to_universal rejects non-date arguments""" with self.assertRaises(TypeError): times.to_universal([1, 2, 3])