def relative_datestring(self): """ Return this block's date in a human-readable format. If the date is None, returns the empty string. """ if self.date is None: return '' locale = to_locale(get_language()) delta = self.date - self.current_time try: relative_date = format_timedelta(delta, locale=locale) # Babel doesn't have translations for Esperanto, so we get # a KeyError when testing translations with # ?preview-lang=eo. This should not happen with any other # languages. See https://github.com/python-babel/babel/issues/107 except KeyError: relative_date = format_timedelta(delta) date_has_passed = delta.days < 0 # Translators: 'absolute' is a date such as "Jan 01, # 2020". 'relative' is a fuzzy description of the time until # 'absolute'. For example, 'absolute' might be "Jan 01, 2020", # and if today were December 5th, 2020, 'relative' would be "1 # month". date_format = _( u"{relative} ago - {absolute}") if date_has_passed else _( u"in {relative} - {absolute}") return date_format.format( relative=relative_date, absolute='{date}', )
def relative_datestring(self): """ Return this block's date in a human-readable format. If the date is None, returns the empty string. """ if self.date is None: return '' locale = to_locale(get_language()) delta = self.date - self.current_time try: relative_date = format_timedelta(delta, locale=locale) # Babel doesn't have translations for Esperanto, so we get # a KeyError when testing translations with # ?preview-lang=eo. This should not happen with any other # languages. See https://github.com/python-babel/babel/issues/107 except KeyError: relative_date = format_timedelta(delta) date_has_passed = delta.days < 0 # Translators: 'absolute' is a date such as "Jan 01, # 2020". 'relative' is a fuzzy description of the time until # 'absolute'. For example, 'absolute' might be "Jan 01, 2020", # and if today were December 5th, 2020, 'relative' would be "1 # month". date_format = _(u"{relative} ago - {absolute}") if date_has_passed else _(u"in {relative} - {absolute}") return date_format.format( relative=relative_date, absolute='{date}', )