return Locale.parse(locale).time_formats[format]
def format_date(date=None, format='medium', locale=LC_TIME):
- """Returns a date formatted according to the given pattern.
+ """Return a date formatted according to the given pattern.
>>> d = date(2007, 04, 01)
>>> format_date(d, locale='en_US')
def format_datetime(datetime=None, format='medium', tzinfo=None,
locale=LC_TIME):
- """Returns a date formatted according to the given pattern.
+ """Return a date formatted according to the given pattern.
>>> dt = datetime(2007, 04, 01, 15, 30)
>>> format_datetime(dt, locale='en_US')
datetime = datetime.replace(tzinfo=UTC)
if tzinfo is not None:
datetime = datetime.astimezone(tzinfo)
- if hasattr(tzinfo, 'normalize'):
+ if hasattr(tzinfo, 'normalize'): # pytz
datetime = tzinfo.normalize(datetime)
locale = Locale.parse(locale)
return parse_pattern(format).apply(datetime, locale)
def format_time(time=None, format='medium', tzinfo=None, locale=LC_TIME):
- """Returns a time formatted according to the given pattern.
+ """Return a time formatted according to the given pattern.
>>> t = time(15, 30)
>>> format_time(t, locale='en_US')
For any pattern requiring the display of the time-zone, the third-party
``pytz`` package is needed to explicitly specify the time-zone:
- >>> from pytz import timezone, utc
- >>> t = time(15, 30, tzinfo=utc)
+ >>> from pytz import timezone
+ >>> t = time(15, 30)
>>> format_time(t, format='full', tzinfo=timezone('Europe/Berlin'),
... locale='de_DE')
u'17:30 Uhr MESZ'
time = time.replace(tzinfo=UTC)
if tzinfo is not None:
dt = datetime.combine(date.today(), time).astimezone(tzinfo)
- if hasattr(tzinfo, 'normalize'):
+ if hasattr(tzinfo, 'normalize'): # pytz
dt = tzinfo.normalize(dt)
time = dt.timetz()
return Locale.parse(locale).number_symbols.get('group', u',')
def format_number(number, locale=LC_NUMERIC):
- """Returns the given number formatted for a specific locale.
+ """Return the given number formatted for a specific locale.
>>> format_number(1099, locale='en_US')
u'1,099'
return format_decimal(number, locale=locale)
def format_decimal(number, format=None, locale=LC_NUMERIC):
- """Returns the given decimal number formatted for a specific locale.
+ """Return the given decimal number formatted for a specific locale.
>>> format_decimal(1.2345, locale='en_US')
u'1.234'
return pattern.apply(number, locale)
def format_currency(number, currency, locale=LC_NUMERIC):
- """Returns formatted currency value.
+ """Return formatted currency value.
>>> format_currency(1099.98, 'USD', locale='en_US')
u'1,099.98'
return format_decimal(number, locale=locale)
def format_percent(number, format=None, locale=LC_NUMERIC):
- """Returns formatted percent value for a specific locale.
+ """Return formatted percent value for a specific locale.
>>> format_percent(0.34, locale='en_US')
u'34%'
.. note: the code in this module is not used by Babel itself
"""
+from datetime import date, datetime, time
import gettext
-__all__ = ['LazyProxy', 'Translations']
+from babel.core import Locale
+from babel.dates import format_date, format_datetime, format_time, LC_TIME
+from babel.numbers import format_number, format_decimal, format_currency, \
+ format_percent, format_scientific, LC_NUMERIC
+from babel.util import UTC
+
+__all__ = ['Format', 'LazyProxy', 'Translations']
__docformat__ = 'restructuredtext en'
+class Format(object):
+ """Wrapper class providing the various date and number formatting functions
+ bound to a specific locale and time-zone.
+
+ >>> fmt = Format('en_US', UTC)
+ >>> fmt.date(date(2007, 4, 1))
+ u'Apr 1, 2007'
+ >>> fmt.decimal(1.2345)
+ u'1.234'
+ """
+
+ def __init__(self, locale, tzinfo=None):
+ """Initialize the formatter.
+
+ :param locale: the locale identifier or `Locale` instance
+ :param tzinfo: the time-zone info (a `tzinfo` instance or `None`)
+ """
+ self.locale = Locale.parse(locale)
+ self.tzinfo = tzinfo
+
+ def date(self, date=None, format='medium'):
+ """Return a date formatted according to the given pattern.
+
+ >>> fmt = Format('en_US')
+ >>> fmt.date(date(2007, 4, 1))
+ u'Apr 1, 2007'
+
+ :see: `babel.dates.format_date`
+ """
+ return format_date(date, format, locale=self.locale)
+
+ def datetime(self, datetime=None, format='medium'):
+ """Return a date and time formatted according to the given pattern.
+
+ >>> from pytz import timezone
+ >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern'))
+ >>> fmt.datetime(datetime(2007, 4, 1, 15, 30))
+ u'Apr 1, 2007 11:30:00 AM'
+
+ :see: `babel.dates.format_datetime`
+ """
+ return format_datetime(datetime, format, tzinfo=self.tzinfo,
+ locale=self.locale)
+
+ def time(self, time=None, format='medium'):
+ """Return a time formatted according to the given pattern.
+
+ >>> from pytz import timezone
+ >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern'))
+ >>> fmt.time(time(15, 30))
+ u'11:30:00 AM'
+
+ :see: `babel.dates.format_time`
+ """
+ return format_time(time, format, tzinfo=self.tzinfo, locale=self.locale)
+
+ def number(self, number):
+ """Return an integer number formatted for the locale.
+
+ >>> fmt = Format('en_US')
+ >>> fmt.number(1099)
+ u'1,099'
+
+ :see: `babel.numbers.format_number`
+ """
+ return format_number(number, locale=self.locale)
+
+ def decimal(self, number, format=None):
+ """Return a decimal number formatted for the locale.
+
+ >>> fmt = Format('en_US')
+ >>> fmt.decimal(1.2345)
+ u'1.234'
+
+ :see: `babel.numbers.format_decimal`
+ """
+ return format_decimal(number, format, locale=self.locale)
+
+ def currency(self, number, currency):
+ """Return a number in the given currency formatted for the locale.
+
+ :see: `babel.numbers.format_currency`
+ """
+ return format_currency(number, currency, locale=self.locale)
+
+ def percent(self, number, format=None):
+ """Return a number formatted as percentage for the locale.
+
+ >>> fmt = Format('en_US')
+ >>> fmt.percent(0.34)
+ u'34%'
+
+ :see: `babel.numbers.format_percent`
+ """
+ return format_percent(number, format, locale=self.locale)
+
+ def scientific(self, number):
+ """Return a number formatted using scientific notation for the locale.
+
+ :see: `babel.numbers.format_scientific`
+ """
+ return format_scientific(number, locale=self.locale)
+
+
class LazyProxy(object):
"""Class for proxy objects that delegate to a specified function to evaluate
the actual object.