PageRenderTime 107ms CodeModel.GetById 94ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/humanize/templatetags/humanize.py

https://code.google.com/p/mango-py/
Python | 102 lines | 98 code | 2 blank | 2 comment | 1 complexity | 2930d2bbd5bb61303793b51ba683984b MD5 | raw file
  1from django.utils.translation import ungettext, ugettext as _
  2from django.utils.encoding import force_unicode
  3from django import template
  4from django.template import defaultfilters
  5from datetime import date
  6import re
  7
  8register = template.Library()
  9
 10def ordinal(value):
 11    """
 12    Converts an integer to its ordinal as a string. 1 is '1st', 2 is '2nd',
 13    3 is '3rd', etc. Works for any integer.
 14    """
 15    try:
 16        value = int(value)
 17    except (TypeError, ValueError):
 18        return value
 19    t = (_('th'), _('st'), _('nd'), _('rd'), _('th'), _('th'), _('th'), _('th'), _('th'), _('th'))
 20    if value % 100 in (11, 12, 13): # special case
 21        return u"%d%s" % (value, t[0])
 22    return u'%d%s' % (value, t[value % 10])
 23ordinal.is_safe = True
 24register.filter(ordinal)
 25
 26def intcomma(value):
 27    """
 28    Converts an integer to a string containing commas every three digits.
 29    For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
 30    """
 31    orig = force_unicode(value)
 32    new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig)
 33    if orig == new:
 34        return new
 35    else:
 36        return intcomma(new)
 37intcomma.is_safe = True
 38register.filter(intcomma)
 39
 40def intword(value):
 41    """
 42    Converts a large integer to a friendly text representation. Works best for
 43    numbers over 1 million. For example, 1000000 becomes '1.0 million', 1200000
 44    becomes '1.2 million' and '1200000000' becomes '1.2 billion'.
 45    """
 46    try:
 47        value = int(value)
 48    except (TypeError, ValueError):
 49        return value
 50    if value < 1000000:
 51        return value
 52    if value < 1000000000:
 53        new_value = value / 1000000.0
 54        return ungettext('%(value).1f million', '%(value).1f million', new_value) % {'value': new_value}
 55    if value < 1000000000000:
 56        new_value = value / 1000000000.0
 57        return ungettext('%(value).1f billion', '%(value).1f billion', new_value) % {'value': new_value}
 58    if value < 1000000000000000:
 59        new_value = value / 1000000000000.0
 60        return ungettext('%(value).1f trillion', '%(value).1f trillion', new_value) % {'value': new_value}
 61    return value
 62intword.is_safe = False
 63register.filter(intword)
 64
 65def apnumber(value):
 66    """
 67    For numbers 1-9, returns the number spelled out. Otherwise, returns the
 68    number. This follows Associated Press style.
 69    """
 70    try:
 71        value = int(value)
 72    except (TypeError, ValueError):
 73        return value
 74    if not 0 < value < 10:
 75        return value
 76    return (_('one'), _('two'), _('three'), _('four'), _('five'), _('six'), _('seven'), _('eight'), _('nine'))[value-1]
 77apnumber.is_safe = True
 78register.filter(apnumber)
 79
 80def naturalday(value, arg=None):
 81    """
 82    For date values that are tomorrow, today or yesterday compared to
 83    present day returns representing string. Otherwise, returns a string
 84    formatted according to settings.DATE_FORMAT.
 85    """
 86    try: 
 87        value = date(value.year, value.month, value.day)
 88    except AttributeError:
 89        # Passed value wasn't a date object
 90        return value
 91    except ValueError:
 92        # Date arguments out of range
 93        return value
 94    delta = value - date.today()
 95    if delta.days == 0:
 96        return _(u'today')
 97    elif delta.days == 1:
 98        return _(u'tomorrow')
 99    elif delta.days == -1:
100        return _(u'yesterday')
101    return defaultfilters.date(value, arg)
102register.filter(naturalday)