2015-04-29 02:12:05 +00:00
|
|
|
"""
|
|
|
|
homeassistant.util.dt
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Provides helper methods to handle the time in HA.
|
|
|
|
|
|
|
|
"""
|
|
|
|
import datetime as dt
|
|
|
|
|
|
|
|
import pytz
|
|
|
|
|
2015-06-15 05:56:55 +00:00
|
|
|
DATETIME_STR_FORMAT = "%H:%M:%S %d-%m-%Y"
|
|
|
|
DATE_STR_FORMAT = "%Y-%m-%d"
|
|
|
|
TIME_STR_FORMAT = "%H:%M"
|
2015-04-29 02:12:05 +00:00
|
|
|
UTC = DEFAULT_TIME_ZONE = pytz.utc
|
|
|
|
|
|
|
|
|
|
|
|
def set_default_time_zone(time_zone):
|
|
|
|
""" Sets a default time zone to be used when none is specified. """
|
|
|
|
global DEFAULT_TIME_ZONE # pylint: disable=global-statement
|
|
|
|
|
|
|
|
assert isinstance(time_zone, dt.tzinfo)
|
|
|
|
|
|
|
|
DEFAULT_TIME_ZONE = time_zone
|
|
|
|
|
|
|
|
|
|
|
|
def get_time_zone(time_zone_str):
|
|
|
|
""" Get time zone from string. Return None if unable to determine. """
|
|
|
|
try:
|
|
|
|
return pytz.timezone(time_zone_str)
|
|
|
|
except pytz.exceptions.UnknownTimeZoneError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def utcnow():
|
|
|
|
""" Get now in UTC time. """
|
2015-06-15 05:56:55 +00:00
|
|
|
return dt.datetime.now(UTC)
|
2015-04-29 02:12:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def now(time_zone=None):
|
|
|
|
""" Get now in specified time zone. """
|
2015-04-29 05:38:43 +00:00
|
|
|
return dt.datetime.now(time_zone or DEFAULT_TIME_ZONE)
|
2015-04-29 02:12:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def as_utc(dattim):
|
|
|
|
""" Return a datetime as UTC time.
|
|
|
|
Assumes datetime without tzinfo to be in the DEFAULT_TIME_ZONE. """
|
2015-06-15 05:56:55 +00:00
|
|
|
if dattim.tzinfo == UTC:
|
2015-04-29 02:12:05 +00:00
|
|
|
return dattim
|
|
|
|
elif dattim.tzinfo is None:
|
|
|
|
dattim = dattim.replace(tzinfo=DEFAULT_TIME_ZONE)
|
|
|
|
|
2015-06-15 05:56:55 +00:00
|
|
|
return dattim.astimezone(UTC)
|
2015-04-29 02:12:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def as_local(dattim):
|
|
|
|
""" Converts a UTC datetime object to local time_zone. """
|
|
|
|
if dattim.tzinfo == DEFAULT_TIME_ZONE:
|
|
|
|
return dattim
|
|
|
|
elif dattim.tzinfo is None:
|
2015-06-15 05:56:55 +00:00
|
|
|
dattim = dattim.replace(tzinfo=UTC)
|
2015-04-29 02:12:05 +00:00
|
|
|
|
|
|
|
return dattim.astimezone(DEFAULT_TIME_ZONE)
|
|
|
|
|
|
|
|
|
|
|
|
def utc_from_timestamp(timestamp):
|
|
|
|
""" Returns a UTC time from a timestamp. """
|
2015-06-15 05:56:55 +00:00
|
|
|
return dt.datetime.utcfromtimestamp(timestamp).replace(tzinfo=UTC)
|
2015-04-29 02:12:05 +00:00
|
|
|
|
|
|
|
|
2015-06-15 05:56:55 +00:00
|
|
|
def start_of_local_day(dt_or_d=None):
|
|
|
|
""" Return local datetime object of start of day from date or datetime. """
|
|
|
|
if dt_or_d is None:
|
|
|
|
dt_or_d = now().date()
|
|
|
|
elif isinstance(dt_or_d, dt.datetime):
|
|
|
|
dt_or_d = dt_or_d.date()
|
|
|
|
|
|
|
|
return dt.datetime.combine(dt_or_d, dt.time()).replace(
|
|
|
|
tzinfo=DEFAULT_TIME_ZONE)
|
|
|
|
|
|
|
|
|
|
|
|
def datetime_to_local_str(dattim):
|
2015-04-29 02:12:05 +00:00
|
|
|
""" Converts datetime to specified time_zone and returns a string. """
|
|
|
|
return datetime_to_str(as_local(dattim))
|
|
|
|
|
|
|
|
|
|
|
|
def datetime_to_str(dattim):
|
|
|
|
""" Converts datetime to a string format.
|
|
|
|
|
|
|
|
@rtype : str
|
|
|
|
"""
|
2015-06-15 05:56:55 +00:00
|
|
|
return dattim.strftime(DATETIME_STR_FORMAT)
|
2015-04-29 02:12:05 +00:00
|
|
|
|
|
|
|
|
2015-06-15 05:56:55 +00:00
|
|
|
def datetime_to_time_str(dattim):
|
|
|
|
""" Converts datetime to a string containing only the time.
|
2015-05-08 15:00:12 +00:00
|
|
|
|
|
|
|
@rtype : str
|
|
|
|
"""
|
2015-06-15 05:56:55 +00:00
|
|
|
return dattim.strftime(TIME_STR_FORMAT)
|
2015-05-08 15:00:12 +00:00
|
|
|
|
|
|
|
|
2015-06-15 05:56:55 +00:00
|
|
|
def datetime_to_date_str(dattim):
|
|
|
|
""" Converts datetime to a string containing only the date.
|
2015-05-08 15:00:12 +00:00
|
|
|
|
|
|
|
@rtype : str
|
|
|
|
"""
|
2015-06-15 05:56:55 +00:00
|
|
|
return dattim.strftime(DATE_STR_FORMAT)
|
2015-05-08 15:00:12 +00:00
|
|
|
|
|
|
|
|
2015-04-29 02:12:05 +00:00
|
|
|
def str_to_datetime(dt_str):
|
|
|
|
""" Converts a string to a UTC datetime object.
|
|
|
|
|
|
|
|
@rtype: datetime
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return dt.datetime.strptime(
|
2015-06-15 05:56:55 +00:00
|
|
|
dt_str, DATETIME_STR_FORMAT).replace(tzinfo=pytz.utc)
|
|
|
|
except ValueError: # If dt_str did not match our format
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def date_str_to_date(dt_str):
|
|
|
|
""" Converts a date string to a date object. """
|
|
|
|
try:
|
|
|
|
return dt.datetime.strptime(dt_str, DATE_STR_FORMAT).date()
|
2015-04-29 02:12:05 +00:00
|
|
|
except ValueError: # If dt_str did not match our format
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def strip_microseconds(dattim):
|
|
|
|
""" Returns a copy of dattime object but with microsecond set to 0. """
|
|
|
|
return dattim.replace(microsecond=0)
|