Ticket #15921: 15921.1.diff

File 15921.1.diff, 13.8 KB (added by Jannis Leidel, 14 years ago)
  • django/contrib/humanize/locale/en/LC_MESSAGES/django.po

    diff --git a/django/contrib/humanize/locale/en/LC_MESSAGES/django.mo b/django/contrib/humanize/locale/en/LC_MESSAGES/django.mo
    index a08dc32..ba05bc9 100644
    Binary files a/django/contrib/humanize/locale/en/LC_MESSAGES/django.mo and b/django/contrib/humanize/locale/en/LC_MESSAGES/django.mo differ
    diff --git a/django/contrib/humanize/locale/en/LC_MESSAGES/django.po b/django/contrib/humanize/locale/en/LC_MESSAGES/django.po
    index 0e7f3f3..360bdd9 100644
    a b msgid ""  
    44msgstr ""
    55"Project-Id-Version: Django\n"
    66"Report-Msgid-Bugs-To: \n"
    7 "POT-Creation-Date: 2011-05-07 21:53+0200\n"
     7"POT-Creation-Date: 2011-05-16 17:30+0200\n"
    88"PO-Revision-Date: 2010-05-13 15:35+0200\n"
    99"Last-Translator: Django team\n"
    1010"Language-Team: English <en@li.org>\n"
    msgstr ""  
    1212"Content-Type: text/plain; charset=UTF-8\n"
    1313"Content-Transfer-Encoding: 8bit\n"
    1414
    15 #: templatetags/humanize.py:21
     15#: templatetags/humanize.py:25
    1616msgid "th"
    1717msgstr ""
    1818
    19 #: templatetags/humanize.py:21
     19#: templatetags/humanize.py:25
    2020msgid "st"
    2121msgstr ""
    2222
    23 #: templatetags/humanize.py:21
     23#: templatetags/humanize.py:25
    2424msgid "nd"
    2525msgstr ""
    2626
    27 #: templatetags/humanize.py:21
     27#: templatetags/humanize.py:25
    2828msgid "rd"
    2929msgstr ""
    3030
    31 #: templatetags/humanize.py:74
     31#: templatetags/humanize.py:78
    3232#, python-format
    3333msgid "%(value).1f million"
    3434msgid_plural "%(value).1f million"
    3535msgstr[0] ""
    3636msgstr[1] ""
    3737
    38 #: templatetags/humanize.py:75
     38#: templatetags/humanize.py:79
    3939#, python-format
    4040msgid "%(value)s million"
    4141msgid_plural "%(value)s million"
    4242msgstr[0] ""
    4343msgstr[1] ""
    4444
    45 #: templatetags/humanize.py:80
     45#: templatetags/humanize.py:84
    4646#, python-format
    4747msgid "%(value).1f billion"
    4848msgid_plural "%(value).1f billion"
    4949msgstr[0] ""
    5050msgstr[1] ""
    5151
    52 #: templatetags/humanize.py:81
     52#: templatetags/humanize.py:85
    5353#, python-format
    5454msgid "%(value)s billion"
    5555msgid_plural "%(value)s billion"
    5656msgstr[0] ""
    5757msgstr[1] ""
    5858
    59 #: templatetags/humanize.py:86
     59#: templatetags/humanize.py:90
    6060#, python-format
    6161msgid "%(value).1f trillion"
    6262msgid_plural "%(value).1f trillion"
    6363msgstr[0] ""
    6464msgstr[1] ""
    6565
    66 #: templatetags/humanize.py:87
     66#: templatetags/humanize.py:91
    6767#, python-format
    6868msgid "%(value)s trillion"
    6969msgid_plural "%(value)s trillion"
    7070msgstr[0] ""
    7171msgstr[1] ""
    7272
    73 #: templatetags/humanize.py:104
     73#: templatetags/humanize.py:108
    7474msgid "one"
    7575msgstr ""
    7676
    77 #: templatetags/humanize.py:104
     77#: templatetags/humanize.py:108
    7878msgid "two"
    7979msgstr ""
    8080
    81 #: templatetags/humanize.py:104
     81#: templatetags/humanize.py:108
    8282msgid "three"
    8383msgstr ""
    8484
    85 #: templatetags/humanize.py:104
     85#: templatetags/humanize.py:108
    8686msgid "four"
    8787msgstr ""
    8888
    89 #: templatetags/humanize.py:104
     89#: templatetags/humanize.py:108
    9090msgid "five"
    9191msgstr ""
    9292
    93 #: templatetags/humanize.py:104
     93#: templatetags/humanize.py:108
    9494msgid "six"
    9595msgstr ""
    9696
    97 #: templatetags/humanize.py:104
     97#: templatetags/humanize.py:108
    9898msgid "seven"
    9999msgstr ""
    100100
    101 #: templatetags/humanize.py:104
     101#: templatetags/humanize.py:108
    102102msgid "eight"
    103103msgstr ""
    104104
    105 #: templatetags/humanize.py:104
     105#: templatetags/humanize.py:108
    106106msgid "nine"
    107107msgstr ""
    108108
    109 #: templatetags/humanize.py:127
     109#: templatetags/humanize.py:131
    110110msgid "today"
    111111msgstr ""
    112112
    113 #: templatetags/humanize.py:129
     113#: templatetags/humanize.py:133
    114114msgid "tomorrow"
    115115msgstr ""
    116116
    117 #: templatetags/humanize.py:131
     117#: templatetags/humanize.py:135
    118118msgid "yesterday"
    119119msgstr ""
    120120
    121 #: templatetags/humanize.py:153
     121#: templatetags/humanize.py:160
     122#, python-format
     123msgctxt "naturaltime"
     124msgid "%(delta)s ago"
     125msgstr ""
     126
     127#: templatetags/humanize.py:163 templatetags/humanize.py:185
    122128msgid "now"
    123129msgstr ""
    124130
    125 #: templatetags/humanize.py:155
     131#: templatetags/humanize.py:166
    126132#, python-format
    127 msgid "%s seconds ago"
    128 msgid_plural "%s seconds ago"
     133msgid "a second ago"
     134msgid_plural "%(count)s seconds ago"
    129135msgstr[0] ""
    130136msgstr[1] ""
    131137
    132 #: templatetags/humanize.py:157
     138#: templatetags/humanize.py:171
    133139#, python-format
    134140msgid "a minute ago"
    135 msgid_plural "%s minutes ago"
     141msgid_plural "%(count)s minutes ago"
    136142msgstr[0] ""
    137143msgstr[1] ""
    138144
    139 #: templatetags/humanize.py:159
     145#: templatetags/humanize.py:176
    140146#, python-format
    141147msgid "an hour ago"
    142 msgid_plural "%s hours ago"
     148msgid_plural "%(count)s hours ago"
     149msgstr[0] ""
     150msgstr[1] ""
     151
     152#: templatetags/humanize.py:182
     153#, python-format
     154msgctxt "naturaltime"
     155msgid "%(delta)s from now"
     156msgstr ""
     157
     158#: templatetags/humanize.py:188
     159#, python-format
     160msgid "a second from now"
     161msgid_plural "%(count)s seconds from now"
     162msgstr[0] ""
     163msgstr[1] ""
     164
     165#: templatetags/humanize.py:193
     166#, python-format
     167msgid "a minute from now"
     168msgid_plural "%(count)s minutes from now"
     169msgstr[0] ""
     170msgstr[1] ""
     171
     172#: templatetags/humanize.py:198
     173#, python-format
     174msgid "an hour from now"
     175msgid_plural "%(count)s hours from now"
    143176msgstr[0] ""
    144177msgstr[1] ""
  • django/contrib/humanize/templatetags/humanize.py

    diff --git a/django/contrib/humanize/templatetags/humanize.py b/django/contrib/humanize/templatetags/humanize.py
    index 560f2a4..7a4defd 100644
    a b  
    1 from django.utils.translation import ungettext, ugettext as _
    2 from django.utils.encoding import force_unicode
    3 from django.utils.formats import number_format
     1import re
     2from datetime import date, datetime, timedelta
     3
    44from django import template
    5 from django.template import defaultfilters
    65from django.conf import settings
    7 from datetime import date, datetime
    8 import re
     6from django.template import defaultfilters
     7from django.utils.datetime_safe import datetime, date
     8from django.utils.encoding import force_unicode
     9from django.utils.formats import number_format
     10from django.utils.translation import pgettext, ungettext, ugettext as _
     11from django.utils.tzinfo import LocalTimezone
     12
    913
    1014register = template.Library()
    1115
    def naturalday(value, arg=None):  
    132136    return defaultfilters.date(value, arg)
    133137
    134138@register.filter
    135 def naturaltime(value, arg=None):
     139def naturaltime(value):
    136140    """
    137     For date and time values shows how many seconds, minutes or hours ago compared to
    138     current timestamp returns representing string. Otherwise, returns a string
    139     formatted according to settings.DATE_FORMAT
     141    For date and time values shows how many seconds, minutes or hours ago
     142    compared to current timestamp returns representing string.
    140143    """
    141144    try:
    142145        value = datetime(value.year, value.month, value.day, value.hour, value.minute, value.second)
    def naturaltime(value, arg=None):  
    145148    except ValueError:
    146149        return value
    147150
    148     delta = datetime.now() - value
    149     if delta.days != 0:
    150         value = date(value.year, value.month, value.day)
    151         return naturalday(value, arg)
    152     elif delta.seconds == 0:
    153         return _(u'now')
    154     elif delta.seconds < 60:
    155         return ungettext(u'%s seconds ago', u'%s seconds ago', delta.seconds)
    156     elif delta.seconds / 60 < 60:
    157         return ungettext(u'a minute ago', u'%s minutes ago', delta.seconds/60)
    158     elif delta.seconds / 60 / 60 < 24:
    159         return ungettext(u'an hour ago', u'%s hours ago', delta.seconds/60/60)
    160     return naturalday(value, arg)
     151    if getattr(value, 'tzinfo', None):
     152        now = datetime.now(LocalTimezone(value))
     153    else:
     154        now = datetime.now()
     155    now = now - timedelta(0, 0, now.microsecond)
     156    if value < now:
     157        delta = now - value
     158        if delta.days != 0:
     159            return pgettext(
     160                'naturaltime', '%(delta)s ago'
     161            ) % {'delta': defaultfilters.timesince(value)}
     162        elif delta.seconds == 0:
     163            return _(u'now')
     164        elif delta.seconds < 60:
     165            return ungettext(
     166                u'a second ago', u'%(count)s seconds ago', delta.seconds
     167            ) % {'count': delta.seconds}
     168        elif delta.seconds / 60 < 60:
     169            count = delta.seconds / 60
     170            return ungettext(
     171                u'a minute ago', u'%(count)s minutes ago', count
     172            ) % {'count': count}
     173        else:
     174            count = delta.seconds / 60 / 60
     175            return ungettext(
     176                u'an hour ago', u'%(count)s hours ago', count
     177            ) % {'count': count}
     178    else:
     179        delta = value - now
     180        if delta.days != 0:
     181            return pgettext(
     182                'naturaltime', '%(delta)s from now'
     183            ) % {'delta': defaultfilters.timeuntil(value)}
     184        elif delta.seconds == 0:
     185            return _(u'now')
     186        elif delta.seconds < 60:
     187            return ungettext(
     188                u'a second from now', u'%(count)s seconds from now', delta.seconds
     189            ) % {'count': delta.seconds}
     190        elif delta.seconds / 60 < 60:
     191            count = delta.seconds / 60
     192            return ungettext(
     193                u'a minute from now', u'%(count)s minutes from now', count
     194            ) % {'count': count}
     195        else:
     196            count = delta.seconds / 60 / 60
     197            return ungettext(
     198                u'an hour from now', u'%(count)s hours from now', count
     199            ) % {'count': count}
  • docs/ref/contrib/humanize.txt

    diff --git a/docs/ref/contrib/humanize.txt b/docs/ref/contrib/humanize.txt
    index 5cacf2b..2f544c7 100644
    a b naturaltime  
    104104
    105105For datetime values, returns a string representing how many seconds,
    106106minutes or hours ago it was -- falling back to a longer date format if the
    107 value is more than a day old.
    108 
    109 **Argument:** Date formatting string as described in the :tfilter:`date` tag.
     107value is more than a day old. In case the datetime value is in the future
     108the return value will automatically use an appropriate phrase.
    110109
    111110Examples (when 'now' is 17 Feb 2007 16:30:00):
    112111
    Examples (when 'now' is 17 Feb 2007 16:30:00):  
    116115    * ``17 Feb 2007 16:25:35`` becomes ``4 minutes ago``.
    117116    * ``17 Feb 2007 15:30:29`` becomes ``an hour ago``.
    118117    * ``17 Feb 2007 13:31:29`` becomes ``2 hours ago``.
    119     * ``16 Feb 2007 13:31:29`` becomes ``yesterday``.
    120     * Any other day is formatted according to given argument or the
    121       :setting:`DATE_FORMAT` setting if no argument is given.
     118    * ``16 Feb 2007 13:31:29`` becomes ``1 day ago``.
     119    * ``17 Feb 2007 16:30:30`` becomes ``29 seconds from now``.
     120    * ``17 Feb 2007 16:31:00`` becomes ``a minute from now``.
     121    * ``17 Feb 2007 16:34:35`` becomes ``4 minutes from now``.
     122    * ``17 Feb 2007 16:30:29`` becomes ``an hour from now``.
     123    * ``17 Feb 2007 18:31:29`` becomes ``2 hours from now``.
     124    * ``18 Feb 2007 16:31:29`` becomes ``1 day from now``.
    122125
    123126.. templatefilter:: ordinal
    124127
  • tests/regressiontests/humanize/tests.py

    diff --git a/tests/regressiontests/humanize/tests.py b/tests/regressiontests/humanize/tests.py
    index bc1bf26..370575a 100644
    a b class HumanizeTests(TestCase):  
    8989                       someday_result, u"I'm not a date value", None)
    9090        self.humanize_tester(test_list, result_list, 'naturalday')
    9191
    92     def test_naturaltime(self):
    93         from django.template import defaultfilters
    94         now = datetime.now()
    95         seconds_ago = now - timedelta(seconds=30)
    96         a_minute_ago = now - timedelta(minutes=1, seconds=30)
    97         minutes_ago = now - timedelta(minutes=2)
    98         an_hour_ago = now - timedelta(hours=1, minutes=30, seconds=30)
    99         hours_ago = now - timedelta(hours=23, minutes=50, seconds=50)
    100 
    101         test_list = (now, a_minute_ago, an_hour_ago)
    102         result_list = (_(u'now'), _(u'a minute ago'), _(u'an hour ago'))
    103         self.humanize_tester(test_list, result_list, 'naturaltime')
    104 
    105         t = Template('{{ seconds_ago|%s }}' % 'naturaltime')
    106         rendered = t.render(Context(locals())).strip()
    107         self.assertTrue(u' seconds ago' in rendered)
    108 
    109         t = Template('{{ minutes_ago|%s }}' % 'naturaltime')
    110         rendered = t.render(Context(locals())).strip()
    111         self.assertTrue(u' minutes ago' in rendered)
    112 
    113         t = Template('{{ hours_ago|%s }}' % 'naturaltime')
    114         rendered = t.render(Context(locals())).strip()
    115         self.assertTrue(u' hours ago' in rendered)
    116 
    11792    def test_naturalday_tz(self):
    11893        from django.contrib.humanize.templatetags.humanize import naturalday
    11994
    class HumanizeTests(TestCase):  
    130105
    131106        # As 24h of difference they will never be the same
    132107        self.assertNotEqual(naturalday_one, naturalday_two)
     108
     109    def test_naturaltime(self):
     110        now = datetime.now()
     111        test_list = [
     112            now,
     113            now - timedelta(seconds=1),
     114            now - timedelta(seconds=30),
     115            now - timedelta(minutes=1, seconds=30),
     116            now - timedelta(minutes=2),
     117            now - timedelta(hours=1, minutes=30, seconds=30),
     118            now - timedelta(hours=23, minutes=50, seconds=50),
     119            now - timedelta(days=1),
     120            now - timedelta(days=500),
     121            now + timedelta(seconds=1),
     122            now + timedelta(seconds=30),
     123            now + timedelta(minutes=1, seconds=30),
     124            now + timedelta(minutes=2),
     125            now + timedelta(hours=1, minutes=30, seconds=30),
     126            now + timedelta(hours=23, minutes=50, seconds=50),
     127            now + timedelta(days=1),
     128            now + timedelta(days=500),
     129        ]
     130        result_list = [
     131            'now',
     132            'a second ago',
     133            '30 seconds ago',
     134            'a minute ago',
     135            '2 minutes ago',
     136            'an hour ago',
     137            '23 hours ago',
     138            '1 day ago',
     139            '1 year, 4 months ago',
     140            'a second from now',
     141            '30 seconds from now',
     142            'a minute from now',
     143            '2 minutes from now',
     144            'an hour from now',
     145            '23 hours from now',
     146            '1 day from now',
     147            '1 year, 4 months from now',
     148        ]
     149        self.humanize_tester(test_list, result_list, 'naturaltime')
Back to Top