def test_sanitize_dt(self): """Test datetime timezone conversion to UTC. """ # aware utc should just go in and out. utc = aware_utcnow() sanitized_utc = sanitize_dt(utc) self.assertTrue(dt_is_aware(sanitized_utc)) self.assertEqual(utc, sanitized_utc) # Sanitize a time zone aware localtime to UTC. The # sanitized object # Use .localize and not datetime.replace to generate # the local date because that doesn't handle DST correctly. def get_unrounded_local(): """get an unrounded local time deal - a datetime object with real microsecond precision.""" pacific = pytz.timezone('US/Pacific') local = pacific.localize(datetime.datetime.now()) if len(str(local.microsecond)) < 6: # Prevent a microsecond value that starts with # a zero - ie: .023493. This will interefere # with the janky string-based rounding that this # test is doing for the purposes of this test by # becoming 23493. time.sleep(.1) return get_unrounded_local() elif local.microsecond % 1000 != 0: # The "good" case - we have a six decimal place # microseconds value that has greater than millisecond # precision. This is to work sanitizing incoming # microsecond values down to milliseconds to have # parity with the javascript precision. return local else: # Unlikely - this will happen if the microsecond # value is spontaneously exactly at millisecond # precision. return get_unrounded_local() local = get_unrounded_local() # testing mode to suppress warnings local_utc = sanitize_dt(local, testing=True) # objects will not be equal since sanitize is rounding to milliseconds self.assertNotEqual(local_utc, local) # this is a terrible way to round to milliseconds, but in this # case, we are trying to leave the time zone difference intact # whereas pypond.util wants to force everything to UTC. # But in general DO NOT DO THIS. -MMG msec = '{ms}000'.format(ms=str(local.microsecond)[0:3]) local = local.replace(microsecond=int(msec)) self.assertEqual(local_utc, local) # double check that delta is zero local_utc_delta = local_utc - local self.assertEqual(int(local_utc_delta.total_seconds()), 0) # test sanity check stopping naive datetime objects with self.assertRaises(UtilityException): sanitize_dt(self.naive)
def test_epoch_constant(self): """Make sure util.EPOCH does not get changed to be naive.""" self.assertTrue(dt_is_aware(EPOCH))
def test_dt_from_ms(self): """Test function to make datetime from epoch ms/verify aware.""" dtime = dt_from_ms(self.ms_reference) self.assertTrue(dt_is_aware(dtime))
def test_aware(self): """Verify test_aware function.""" self.assertFalse(dt_is_aware(self.naive)) aware = aware_utcnow() self.assertTrue(dt_is_aware(aware))