Use UTC as the internal datetime format
This commit is contained in:
1 parent
362e176397
commit
e0ecb64a10
17 files changed
+457
-147
No files matched your search
@@ -1,17 +1,18 @@
|
||||
"""
|
||||
tests.test_component_group
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
tests.test_component_device_tracker
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests the group compoments.
|
||||
Tests the device tracker compoments.
|
||||
"""
|
||||
# pylint: disable=protected-access,too-many-public-methods
|
||||
import unittest
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
import os
|
||||
|
||||
import homeassistant as ha
|
||||
import homeassistant.loader as loader
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.const import (
|
||||
STATE_HOME, STATE_NOT_HOME, ATTR_ENTITY_PICTURE, CONF_PLATFORM)
|
||||
import homeassistant.components.device_tracker as device_tracker
|
||||
@@ -116,7 +117,7 @@ class TestComponentsDeviceTracker(unittest.TestCase):
|
||||
dev2 = device_tracker.ENTITY_ID_FORMAT.format('device_2')
|
||||
dev3 = device_tracker.ENTITY_ID_FORMAT.format('DEV3')
|
||||
|
||||
now = datetime.now()
|
||||
now = dt_util.utcnow()
|
||||
|
||||
# Device scanner scans every 12 seconds. We need to sync our times to
|
||||
# be every 12 seconds or else the time_changed event will be ignored.
|
||||
+12
-13
@@ -6,11 +6,12 @@ Tests Sun component.
|
||||
"""
|
||||
# pylint: disable=too-many-public-methods,protected-access
|
||||
import unittest
|
||||
import datetime as dt
|
||||
from datetime import timedelta
|
||||
|
||||
import ephem
|
||||
|
||||
import homeassistant as ha
|
||||
import homeassistant.util.dt as dt_util
|
||||
import homeassistant.components.sun as sun
|
||||
|
||||
|
||||
@@ -42,22 +43,20 @@ class TestSun(unittest.TestCase):
|
||||
observer.lat = '32.87336' # pylint: disable=assigning-non-slot
|
||||
observer.long = '117.22743' # pylint: disable=assigning-non-slot
|
||||
|
||||
utc_now = dt.datetime.utcnow()
|
||||
utc_now = dt_util.utcnow()
|
||||
body_sun = ephem.Sun() # pylint: disable=no-member
|
||||
next_rising_dt = ephem.localtime(
|
||||
observer.next_rising(body_sun, start=utc_now))
|
||||
next_setting_dt = ephem.localtime(
|
||||
observer.next_setting(body_sun, start=utc_now))
|
||||
next_rising_dt = observer.next_rising(
|
||||
body_sun, start=utc_now).datetime().replace(tzinfo=dt_util.UTC)
|
||||
next_setting_dt = observer.next_setting(
|
||||
body_sun, start=utc_now).datetime().replace(tzinfo=dt_util.UTC)
|
||||
|
||||
# Home Assistant strips out microseconds
|
||||
# strip it out of the datetime objects
|
||||
next_rising_dt = next_rising_dt - dt.timedelta(
|
||||
microseconds=next_rising_dt.microsecond)
|
||||
next_setting_dt = next_setting_dt - dt.timedelta(
|
||||
microseconds=next_setting_dt.microsecond)
|
||||
next_rising_dt = dt_util.strip_microseconds(next_rising_dt)
|
||||
next_setting_dt = dt_util.strip_microseconds(next_setting_dt)
|
||||
|
||||
self.assertEqual(next_rising_dt, sun.next_rising(self.hass))
|
||||
self.assertEqual(next_setting_dt, sun.next_setting(self.hass))
|
||||
self.assertEqual(next_rising_dt, sun.next_rising_utc(self.hass))
|
||||
self.assertEqual(next_setting_dt, sun.next_setting_utc(self.hass))
|
||||
|
||||
# Point it at a state without the proper attributes
|
||||
self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON)
|
||||
@@ -84,7 +83,7 @@ class TestSun(unittest.TestCase):
|
||||
self.assertIsNotNone(test_time)
|
||||
|
||||
self.hass.bus.fire(ha.EVENT_TIME_CHANGED,
|
||||
{ha.ATTR_NOW: test_time + dt.timedelta(seconds=5)})
|
||||
{ha.ATTR_NOW: test_time + timedelta(seconds=5)})
|
||||
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
|
||||
+2
-2
@@ -72,7 +72,7 @@ class TestHomeAssistant(unittest.TestCase):
|
||||
|
||||
runs = []
|
||||
|
||||
self.hass.track_point_in_time(
|
||||
self.hass.track_point_in_utc_time(
|
||||
lambda x: runs.append(1), birthday_paulus)
|
||||
|
||||
self._send_time_changed(before_birthday)
|
||||
@@ -88,7 +88,7 @@ class TestHomeAssistant(unittest.TestCase):
|
||||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(1, len(runs))
|
||||
|
||||
self.hass.track_point_in_time(
|
||||
self.hass.track_point_in_utc_time(
|
||||
lambda x: runs.append(1), birthday_paulus)
|
||||
|
||||
self._send_time_changed(after_birthday)
|
||||
|
||||
@@ -35,17 +35,6 @@ class TestUtil(unittest.TestCase):
|
||||
self.assertEqual("Test_More", util.slugify("Test More"))
|
||||
self.assertEqual("Test_More", util.slugify("Test_(More)"))
|
||||
|
||||
def test_datetime_to_str(self):
|
||||
""" Test datetime_to_str. """
|
||||
self.assertEqual("12:00:00 09-07-1986",
|
||||
util.datetime_to_str(datetime(1986, 7, 9, 12, 0, 0)))
|
||||
|
||||
def test_str_to_datetime(self):
|
||||
""" Test str_to_datetime. """
|
||||
self.assertEqual(datetime(1986, 7, 9, 12, 0, 0),
|
||||
util.str_to_datetime("12:00:00 09-07-1986"))
|
||||
self.assertIsNone(util.str_to_datetime("not a datetime string"))
|
||||
|
||||
def test_split_entity_id(self):
|
||||
""" Test split_entity_id. """
|
||||
self.assertEqual(['domain', 'object_id'],
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
"""
|
||||
tests.test_util
|
||||
~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests Home Assistant date util methods.
|
||||
"""
|
||||
# pylint: disable=too-many-public-methods
|
||||
import unittest
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
TEST_TIME_ZONE = 'America/Los_Angeles'
|
||||
|
||||
|
||||
class TestDateUtil(unittest.TestCase):
|
||||
""" Tests util date methods. """
|
||||
|
||||
def setUp(self):
|
||||
self.orig_default_time_zone = dt_util.DEFAULT_TIME_ZONE
|
||||
|
||||
def tearDown(self):
|
||||
dt_util.set_default_time_zone(self.orig_default_time_zone)
|
||||
|
||||
def test_get_time_zone_retrieves_valid_time_zone(self):
|
||||
""" Test getting a time zone. """
|
||||
time_zone = dt_util.get_time_zone(TEST_TIME_ZONE)
|
||||
|
||||
self.assertIsNotNone(time_zone)
|
||||
self.assertEqual(TEST_TIME_ZONE, time_zone.zone)
|
||||
|
||||
def test_get_time_zone_returns_none_for_garbage_time_zone(self):
|
||||
""" Test getting a non existing time zone. """
|
||||
time_zone = dt_util.get_time_zone("Non existing time zone")
|
||||
|
||||
self.assertIsNone(time_zone)
|
||||
|
||||
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_utcnow(self):
|
||||
""" Test the UTC now method. """
|
||||
self.assertAlmostEqual(
|
||||
dt_util.utcnow().replace(tzinfo=None),
|
||||
datetime.utcnow(),
|
||||
delta=timedelta(seconds=1))
|
||||
|
||||
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_utc_with_naive_object(self):
|
||||
utcnow = datetime.utcnow()
|
||||
|
||||
self.assertEqual(utcnow,
|
||||
dt_util.as_utc(utcnow).replace(tzinfo=None))
|
||||
|
||||
def test_as_utc_with_utc_object(self):
|
||||
utcnow = dt_util.utcnow()
|
||||
|
||||
self.assertEqual(utcnow, dt_util.as_utc(utcnow))
|
||||
|
||||
def test_as_utc_with_local_object(self):
|
||||
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_as_local_with_naive_object(self):
|
||||
now = dt_util.now()
|
||||
|
||||
self.assertAlmostEqual(
|
||||
now, dt_util.as_local(datetime.utcnow()),
|
||||
delta=timedelta(seconds=1))
|
||||
|
||||
def test_as_local_with_local_object(self):
|
||||
now = dt_util.now()
|
||||
|
||||
self.assertEqual(now, now)
|
||||
|
||||
def test_as_local_with_utc_object(self):
|
||||
dt_util.set_default_time_zone(dt_util.get_time_zone(TEST_TIME_ZONE))
|
||||
|
||||
utcnow = dt_util.utcnow()
|
||||
localnow = dt_util.as_local(utcnow)
|
||||
|
||||
self.assertEqual(localnow, utcnow)
|
||||
self.assertNotEqual(localnow.tzinfo, utcnow.tzinfo)
|
||||
|
||||
def test_utc_from_timestamp(self):
|
||||
""" Test utc_from_timestamp method. """
|
||||
self.assertEqual(
|
||||
datetime(1986, 7, 9, tzinfo=dt_util.UTC),
|
||||
dt_util.utc_from_timestamp(521251200))
|
||||
|
||||
def test_datetime_to_str(self):
|
||||
""" Test datetime_to_str. """
|
||||
self.assertEqual(
|
||||
"12:00:00 09-07-1986",
|
||||
dt_util.datetime_to_str(datetime(1986, 7, 9, 12, 0, 0)))
|
||||
|
||||
def test_datetime_to_local_str(self):
|
||||
""" Test datetime_to_local_str. """
|
||||
self.assertEqual(
|
||||
dt_util.datetime_to_str(dt_util.now()),
|
||||
dt_util.datetime_to_local_str(dt_util.utcnow()))
|
||||
|
||||
def test_str_to_datetime_converts_correctly(self):
|
||||
""" Test str_to_datetime converts strings. """
|
||||
self.assertEqual(
|
||||
datetime(1986, 7, 9, 12, 0, 0, tzinfo=dt_util.UTC),
|
||||
dt_util.str_to_datetime("12:00:00 09-07-1986"))
|
||||
|
||||
def test_str_to_datetime_returns_none_for_incorrect_format(self):
|
||||
""" Test str_to_datetime returns None if incorrect format. """
|
||||
self.assertIsNone(dt_util.str_to_datetime("not a datetime string"))
|
||||
|
||||
def test_strip_microseconds(self):
|
||||
test_time = datetime(2015, 1, 1, microsecond=5000)
|
||||
|
||||
self.assertNotEqual(0, test_time.microsecond)
|
||||
self.assertEqual(0, dt_util.strip_microseconds(test_time).microsecond)
|
||||
Reference in new issue
Block a user