mirror of
https://github.com/reddit-archive/reddit.git
synced 2026-01-27 07:48:16 -05:00
Higher precision version of format_timedelta.
This commit is contained in:
@@ -74,9 +74,9 @@ from r2.lib.memoize import memoize
|
||||
from r2.lib.utils import trunc_string as _truncate, to_date
|
||||
from r2.lib.filters import safemarkdown
|
||||
from r2.lib.utils import Storage
|
||||
from r2.lib.utils import precise_format_timedelta
|
||||
|
||||
from babel.numbers import format_currency
|
||||
from babel.dates import format_timedelta
|
||||
from collections import defaultdict
|
||||
import csv
|
||||
import cStringIO
|
||||
@@ -1745,7 +1745,8 @@ class ServerSecondsBar(Templated):
|
||||
self.message = ''
|
||||
else:
|
||||
delta = datetime.timedelta(seconds=seconds)
|
||||
server_time = format_timedelta(delta, threshold=5, locale=c.locale)
|
||||
server_time = precise_format_timedelta(delta, threshold=5,
|
||||
locale=c.locale)
|
||||
|
||||
if user == c.user:
|
||||
message = _("you have helped pay for %(time)s of reddit "
|
||||
|
||||
@@ -26,6 +26,7 @@ import traceback
|
||||
import ConfigParser
|
||||
import codecs
|
||||
|
||||
from babel.dates import TIMEDELTA_UNITS
|
||||
from urllib import unquote_plus
|
||||
from urllib2 import urlopen, Request
|
||||
from urlparse import urlparse, urlunparse
|
||||
@@ -1492,3 +1493,25 @@ def canonicalize_email(email):
|
||||
localpart = localpart.partition("+")[0]
|
||||
|
||||
return localpart + "@" + domain
|
||||
|
||||
|
||||
def precise_format_timedelta(delta, locale, threshold=.85, decimals=2):
|
||||
"""Like babel.dates.format_datetime but with adjustable precision"""
|
||||
seconds = delta.total_seconds()
|
||||
|
||||
for unit, secs_per_unit in TIMEDELTA_UNITS:
|
||||
value = abs(seconds) / secs_per_unit
|
||||
if value >= threshold:
|
||||
plural_form = locale.plural_form(value)
|
||||
pattern = None
|
||||
for choice in (unit + ':medium', unit):
|
||||
patterns = locale._data['unit_patterns'].get(choice)
|
||||
if patterns is not None:
|
||||
pattern = patterns[plural_form]
|
||||
break
|
||||
if pattern is None:
|
||||
return u''
|
||||
decimals = int(decimals)
|
||||
format_string = "%." + str(decimals) + "f"
|
||||
return pattern.replace('{0}', format_string % value)
|
||||
return u''
|
||||
|
||||
Reference in New Issue
Block a user