From 16933abce95a434e3cab0bab10a30170758fc5d1 Mon Sep 17 00:00:00 2001 From: Robbie Trencheny Date: Tue, 10 May 2016 00:04:53 -0700 Subject: [PATCH] Remove humanize and use a relative time thing that @balloob found on Github --- homeassistant/helpers/template.py | 4 +-- homeassistant/util/dt.py | 46 +++++++++++++++++++++++++++++++ requirements_all.txt | 1 - 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 2d9563ade86..a2a1856d3c2 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -6,8 +6,6 @@ import logging import jinja2 from jinja2.sandbox import ImmutableSandboxedEnvironment -import humanize - from homeassistant.components import group from homeassistant.const import STATE_UNKNOWN, ATTR_LATITUDE, ATTR_LONGITUDE from homeassistant.core import State @@ -42,7 +40,7 @@ def render_with_possible_json_value(hass, template, value, def relative_time(end_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): diff --git a/homeassistant/util/dt.py b/homeassistant/util/dt.py index 16e8dfebfd1..b06b70f2cdd 100644 --- a/homeassistant/util/dt.py +++ b/homeassistant/util/dt.py @@ -152,3 +152,49 @@ def parse_time(time_str): except ValueError: # ValueError if value cannot be converted to an int or not in range 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() diff --git a/requirements_all.txt b/requirements_all.txt index 6fd11b160b2..ddfc5a0802f 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -6,7 +6,6 @@ pip>=7.0.0 vincenty==0.1.4 jinja2>=2.8 voluptuous==0.8.9 -humanize==0.5.1 # homeassistant.components.isy994 PyISY==1.0.5