示例#1
0
文件: __init__.py 项目: strogo/cms-1
def format_amount_of_time(seconds, precision=2, locale=None):
    """Return the number of seconds formatted 'X days, Y hours, ...'

    The time units that will be used are days, hours, minutes, seconds.
    Only the first "precision" units will be output. If they're not
    enough, a "more than ..." will be prefixed (non-positive precision
    means infinite).

    seconds (int): the length of the amount of time in seconds.
    precision (int): see above
    locale (tornado.locale.Locale): the locale to be used.

    return (string): seconds formatted as above.

    """
    seconds = abs(int(seconds))

    if locale is None:
        locale = tornado.locale.get()

    _ = locale.translate

    if seconds == 0:
        return _("%d second", "%d seconds", 0) % 0

    units = [(("%d day", "%d days"), 60 * 60 * 24),
             (("%d hour", "%d hours"), 60 * 60),
             (("%d minute", "%d minutes"), 60),
             (("%d second", "%d seconds"), 1)]

    ret = list()
    counter = 0

    for name, length in units:
        tmp = seconds // length
        seconds %= length
        if tmp == 0:
            continue
        else:
            ret.append(_(name[0], name[1], tmp) % tmp)
        counter += 1
        if counter == precision:
            break

    ret = locale.list(ret)

    if seconds > 0:
        ret = _("more than %s") % ret

    return ret
示例#2
0
def format_amount_of_time(seconds, precision=2, locale=None):
    """Return the number of seconds formatted 'X days, Y hours, ...'

    The time units that will be used are days, hours, minutes, seconds.
    Only the first "precision" units will be output. If they're not
    enough, a "more than ..." will be prefixed (non-positive precision
    means infinite).

    seconds (int): the length of the amount of time in seconds.
    precision (int): see above
    locale (tornado.locale.Locale): the locale to be used.

    return (string): seconds formatted as above.

    """
    seconds = abs(int(seconds))

    if locale is None:
        locale = tornado.locale.get()

    if seconds == 0:
        return locale.translate("0 seconds")

    units = [("day", 60 * 60 * 24), ("hour", 60 * 60), ("minute", 60),
             ("second", 1)]

    ret = list()
    counter = 0

    for name, length in units:
        tmp = seconds // length
        seconds %= length
        if tmp == 0:
            continue
        elif tmp == 1:
            ret.append(locale.translate("1 %s" % name))
        else:
            ret.append(locale.translate("%%d %ss" % name) % tmp)
        counter += 1
        if counter == precision:
            break

    ret = locale.list(ret)

    if seconds > 0:
        ret = locale.translate("more than %s") % ret

    return ret
示例#3
0
 def test_list(self):
     locale = tornado.locale.get('en_US')
     self.assertEqual(locale.list([]), '')
     self.assertEqual(locale.list(['A']), 'A')
     self.assertEqual(locale.list(['A', 'B']), 'A and B')
     self.assertEqual(locale.list(['A', 'B', 'C']), 'A, B and C')
示例#4
0
 def test_list(self):
     locale = tornado.locale.get('en_US')
     self.assertEqual(locale.list([]), '')
     self.assertEqual(locale.list(['A']), 'A')
     self.assertEqual(locale.list(['A', 'B']), 'A and B')
     self.assertEqual(locale.list(['A', 'B', 'C']), 'A, B and C')
示例#5
0
 def test_list(self):
     locale = tornado.locale.get("en_US")
     self.assertEqual(locale.list([]), "")
     self.assertEqual(locale.list(["A"]), "A")
     self.assertEqual(locale.list(["A", "B"]), "A and B")
     self.assertEqual(locale.list(["A", "B", "C"]), "A, B and C")
示例#6
0
 def test_list(self):
     locale = tornado.locale.get("en_US")
     self.assertEqual(locale.list([]), "")
     self.assertEqual(locale.list(["A"]), "A")
     self.assertEqual(locale.list(["A", "B"]), "A and B")
     self.assertEqual(locale.list(["A", "B", "C"]), "A, B and C")