mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 13:47:35 +00:00
Remove humanize and use a relative time thing that @balloob found on Github
This commit is contained in:
parent
d5a1c52359
commit
16933abce9
@ -6,8 +6,6 @@ import logging
|
|||||||
import jinja2
|
import jinja2
|
||||||
from jinja2.sandbox import ImmutableSandboxedEnvironment
|
from jinja2.sandbox import ImmutableSandboxedEnvironment
|
||||||
|
|
||||||
import humanize
|
|
||||||
|
|
||||||
from homeassistant.components import group
|
from homeassistant.components import group
|
||||||
from homeassistant.const import STATE_UNKNOWN, ATTR_LATITUDE, ATTR_LONGITUDE
|
from homeassistant.const import STATE_UNKNOWN, ATTR_LATITUDE, ATTR_LONGITUDE
|
||||||
from homeassistant.core import State
|
from homeassistant.core import State
|
||||||
@ -42,7 +40,7 @@ def render_with_possible_json_value(hass, template, value,
|
|||||||
|
|
||||||
def relative_time(end_time):
|
def relative_time(end_time):
|
||||||
"""Return a relative (human readable) timestamp for the given time."""
|
"""Return a relative (human readable) timestamp for the given time."""
|
||||||
return humanize.naturaltime(dt_util.now() - end_time)
|
return dt_util.get_age(end_time)
|
||||||
|
|
||||||
|
|
||||||
def render(hass, template, variables=None, **kwargs):
|
def render(hass, template, variables=None, **kwargs):
|
||||||
|
@ -152,3 +152,49 @@ def parse_time(time_str):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
# ValueError if value cannot be converted to an int or not in range
|
# ValueError if value cannot be converted to an int or not in range
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
# Found in this gist: https://gist.github.com/zhangsen/1199964
|
||||||
|
def get_age(date):
|
||||||
|
"""
|
||||||
|
Take a datetime and return its "age" as a string.
|
||||||
|
|
||||||
|
The age can be in second, minute, hour, day, month or year. Only the
|
||||||
|
biggest unit is considered, e.g. if it's 2 days and 3 hours, "2 days" will
|
||||||
|
be returned.
|
||||||
|
Make sure date is not in the future, or else it won't work.
|
||||||
|
"""
|
||||||
|
def formatn(number, unit):
|
||||||
|
"""Add "unit" if it's plural."""
|
||||||
|
if number == 1:
|
||||||
|
return "1 %s" % unit
|
||||||
|
elif number > 1:
|
||||||
|
return "%d %ss" % (number, unit)
|
||||||
|
|
||||||
|
def q_n_r(first, second):
|
||||||
|
"""Return quotient and remaining."""
|
||||||
|
return first // second, first % second
|
||||||
|
|
||||||
|
# pylint: disable=too-few-public-methods
|
||||||
|
class PrettyDelta:
|
||||||
|
"""A class for relative times."""
|
||||||
|
|
||||||
|
def __init__(self, subDt):
|
||||||
|
delta = now() - subDt
|
||||||
|
self.day = delta.days
|
||||||
|
self.second = delta.seconds
|
||||||
|
|
||||||
|
self.year, self.day = q_n_r(self.day, 365)
|
||||||
|
self.month, self.day = q_n_r(self.day, 30)
|
||||||
|
self.hour, self.second = q_n_r(self.second, 3600)
|
||||||
|
self.minute, self.second = q_n_r(self.second, 60)
|
||||||
|
|
||||||
|
def format(self):
|
||||||
|
"""Format a datetime to relative time string."""
|
||||||
|
for period in ['year', 'month', 'day', 'hour', 'minute', 'second']:
|
||||||
|
number = getattr(self, period)
|
||||||
|
if number > 0:
|
||||||
|
return formatn(number, period)
|
||||||
|
return "0 second"
|
||||||
|
|
||||||
|
return PrettyDelta(date).format()
|
||||||
|
@ -6,7 +6,6 @@ pip>=7.0.0
|
|||||||
vincenty==0.1.4
|
vincenty==0.1.4
|
||||||
jinja2>=2.8
|
jinja2>=2.8
|
||||||
voluptuous==0.8.9
|
voluptuous==0.8.9
|
||||||
humanize==0.5.1
|
|
||||||
|
|
||||||
# homeassistant.components.isy994
|
# homeassistant.components.isy994
|
||||||
PyISY==1.0.5
|
PyISY==1.0.5
|
||||||
|
Loading…
x
Reference in New Issue
Block a user