def test_get_age(self): """Test get_age.""" diff = dt_util.now() - timedelta(seconds=0) self.assertEqual(dt_util.get_age(diff), "0 seconds") diff = dt_util.now() - timedelta(seconds=1) self.assertEqual(dt_util.get_age(diff), "1 second") diff = dt_util.now() - timedelta(seconds=30) self.assertEqual(dt_util.get_age(diff), "30 seconds") diff = dt_util.now() - timedelta(minutes=5) self.assertEqual(dt_util.get_age(diff), "5 minutes") diff = dt_util.now() - timedelta(minutes=1) self.assertEqual(dt_util.get_age(diff), "1 minute") diff = dt_util.now() - timedelta(minutes=300) self.assertEqual(dt_util.get_age(diff), "5 hours") diff = dt_util.now() - timedelta(minutes=320) self.assertEqual(dt_util.get_age(diff), "5 hours") diff = dt_util.now() - timedelta(minutes=2 * 60 * 24) self.assertEqual(dt_util.get_age(diff), "2 days") diff = dt_util.now() - timedelta(minutes=32 * 60 * 24) self.assertEqual(dt_util.get_age(diff), "1 month") diff = dt_util.now() - timedelta(minutes=365 * 60 * 24) self.assertEqual(dt_util.get_age(diff), "1 year")
def test_as_utc_with_local_object(self): """Test the UTC time with local object.""" dt_util.set_default_time_zone(dt_util.get_time_zone(TEST_TIME_ZONE)) localnow = dt_util.now() utcnow = dt_util.as_utc(localnow) self.assertEqual(localnow, utcnow) self.assertNotEqual(localnow.tzinfo, utcnow.tzinfo)
def test_set_default_time_zone(self): """Test setting default time zone.""" time_zone = dt_util.get_time_zone(TEST_TIME_ZONE) dt_util.set_default_time_zone(time_zone) # We cannot compare the timezones directly because of DST self.assertEqual(time_zone.zone, dt_util.now().tzinfo.zone)
def test_now(self): """Test the now method.""" dt_util.set_default_time_zone(dt_util.get_time_zone(TEST_TIME_ZONE)) self.assertAlmostEqual( dt_util.as_utc(dt_util.now()).replace(tzinfo=None), datetime.utcnow(), delta=timedelta(seconds=1), )
def test_as_local_with_local_object(self): """Test local with local object.""" now = dt_util.now() self.assertEqual(now, now)
def test_as_local_with_naive_object(self): """Test local time with native object.""" now = dt_util.now() self.assertAlmostEqual(now, dt_util.as_local(datetime.utcnow()), delta=timedelta(seconds=1))
def test_as_local_with_naive_object(self): """Test local time with native object.""" now = dt_util.now() self.assertAlmostEqual( now, dt_util.as_local(datetime.utcnow()), delta=timedelta(seconds=1) )