cmlenz@2: .. -*- mode: rst; encoding: utf-8 -*- cmlenz@2: cmlenz@2: ========================== cmlenz@2: Number and Date Formatting cmlenz@2: ========================== cmlenz@2: cmlenz@2: cmlenz@2: .. contents:: Contents cmlenz@2: :depth: 2 cmlenz@2: .. sectnum:: cmlenz@2: cmlenz@2: cmlenz@18: Date Formatting cmlenz@18: =============== cmlenz@18: cmlenz@18: When working with date and time information in Python, you commonly use the cmlenz@18: classes ``date``, ``datetime`` and/or ``time`` from the `datetime package`_. cmlenz@18: Babel provides functions for locale-specific formatting of those objects in its cmlenz@18: ``dates`` module:: cmlenz@18: cmlenz@18: >>> from datetime import date, datetime, time cmlenz@18: >>> from babel.dates import format_date, format_datetime, format_time cmlenz@18: cmlenz@18: >>> d = date(2007, 4, 1) cmlenz@18: >>> format_date(d, locale='en') cmlenz@18: u'Apr 1, 2007' cmlenz@18: >>> format_date(d, locale='de_DE') cmlenz@18: u'01.04.2007' cmlenz@18: cmlenz@18: As this example demonstrates, Babel will automatically choose a date format cmlenz@18: that is appropriate for the requested locale. cmlenz@18: cmlenz@18: The ``format_*()`` functions also accept an optional ``format`` argument, which cmlenz@18: allows you to choose between one of four format variations: cmlenz@18: cmlenz@18: * ``short``, cmlenz@18: * ``medium`` (the default), cmlenz@18: * ``long`` (the default), and cmlenz@18: * ``full`` (the default). cmlenz@18: cmlenz@18: For example:: cmlenz@18: cmlenz@18: >>> format_date(d, format='short', locale='en') cmlenz@18: u'4/1/07' cmlenz@18: >>> format_date(d, format='long', locale='en') cmlenz@18: u'April 1, 2007' cmlenz@18: >>> format_date(d, format='full', locale='en') cmlenz@18: u'Sunday, April 1, 2007' cmlenz@18: cmlenz@18: .. _`datetime package`: http://docs.python.org/lib/module-datetime.html cmlenz@18: cmlenz@18: cmlenz@18: Pattern Syntax cmlenz@18: -------------- cmlenz@18: cmlenz@18: While Babel makes it simple to use the appropriate date/time format for a given cmlenz@18: locale, you can also force it to use custom patterns. Note that Babel uses cmlenz@18: different patterns for specifying number and date formats compared to the cmlenz@18: Python equivalents (such as ``time.strftime()``), which have mostly been cmlenz@18: inherited from C and POSIX. The patterns used in Babel are based on the cmlenz@18: `Locale Data Markup Language specification`_ (LDML), which defines them as cmlenz@18: follows: cmlenz@18: cmlenz@18: A date/time pattern is a string of characters, where specific strings of cmlenz@18: characters are replaced with date and time data from a calendar when formatting cmlenz@18: or used to generate data for a calendar when parsing. […] cmlenz@18: cmlenz@18: Characters may be used multiple times. For example, if ``y`` is used for the cmlenz@18: year, ``yy`` might produce "99", whereas ``yyyy`` produces "1999". For most cmlenz@18: numerical fields, the number of characters specifies the field width. For cmlenz@18: example, if ``h`` is the hour, ``h`` might produce "5", but ``hh`` produces cmlenz@18: "05". For some characters, the count specifies whether an abbreviated or full cmlenz@18: form should be used […] cmlenz@18: cmlenz@18: Two single quotes represent a literal single quote, either inside or outside cmlenz@18: single quotes. Text within single quotes is not interpreted in any way (except cmlenz@18: for two adjacent single quotes). cmlenz@18: cmlenz@18: For example:: cmlenz@18: cmlenz@18: >>> d = date(2007, 4, 1) cmlenz@18: >>> format_date(d, "EEE, MMM d, ''yy", locale='en') cmlenz@18: u"Sun, Apr 1, '07" cmlenz@18: >>> format_date(d, "EEEE, d.M.yyyy", locale='de') cmlenz@18: u'Sonntag, 1.4.2007' cmlenz@18: cmlenz@18: >>> t = time(15, 30) cmlenz@18: >>> format_time(t, "hh 'o''clock' a", locale='en') cmlenz@18: u"03 o'clock PM" cmlenz@18: >>> format_time(t, 'H:mm a', locale='de') cmlenz@18: u'15:30 nachm.' cmlenz@18: cmlenz@18: >>> dt = datetime(2007, 4, 1, 15, 30) cmlenz@18: >>> format_datetime(dt, "yyyyy.MMMM.dd GGG hh:mm a", locale='en') cmlenz@18: u'02007.April.01 AD 03:30 PM' cmlenz@18: cmlenz@18: The syntax for custom datetime format patterns is described in detail in the cmlenz@18: the `Locale Data Markup Language specification`_. The following table is just a cmlenz@18: relatively brief overview. cmlenz@18: cmlenz@18: .. _`Locale Data Markup Language specification`: http://unicode.org/reports/tr35/#Date_Format_Patterns cmlenz@18: cmlenz@18: ----------- cmlenz@18: Date Fields cmlenz@18: ----------- cmlenz@18: cmlenz@18: +----------+--------+--------------------------------------------------------+ cmlenz@18: | Field | Symbol | Description | cmlenz@18: +==========+========+========================================================+ cmlenz@18: | Era | ``G`` | Replaced with the era string for the current date. One | cmlenz@18: | | | to three letters for the abbreviated form, four | cmlenz@18: | | | lettersfor the long form, five for the narrow form | cmlenz@18: +----------+--------+--------------------------------------------------------+ cmlenz@18: | Year | ``y`` | Replaced by the year. Normally the length specifies | cmlenz@18: | | | the padding, but for two letters it also specifies the | cmlenz@18: | | | maximum length. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``Y`` | Same as ``y`` but uses the ISO year-week calendar. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``u`` | ?? | cmlenz@18: +----------+--------+--------------------------------------------------------+ cmlenz@18: | Quarter | ``Q`` | Use one or two for the numerical quarter, three for | cmlenz@18: | | | the abbreviation, or four for the full name. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``q`` | Use one or two for the numerical quarter, three for | cmlenz@18: | | | the abbreviation, or four for the full name. | cmlenz@18: +----------+--------+--------------------------------------------------------+ cmlenz@18: | Month | ``M`` | Use one or two for the numerical month, three for the | cmlenz@18: | | | abbreviation, or four for the full name, or five for | cmlenz@18: | | | the narrow name. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``L`` | Use one or two for the numerical month, three for the | cmlenz@18: | | | abbreviation, or four for the full name, or 5 for the | cmlenz@18: | | | narrow name. | cmlenz@18: +----------+--------+--------------------------------------------------------+ cmlenz@18: | Week | ``w`` | Week of year. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``W`` | Week of month. | cmlenz@18: +----------+--------+--------------------------------------------------------+ cmlenz@18: | Day | ``d`` | Day of month. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``D`` | Day of year. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``F`` | Day of week in month. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``g`` | ?? | cmlenz@18: +----------+--------+--------------------------------------------------------+ cmlenz@18: | Week day | ``E`` | Day of week. Use one through three letters for the | cmlenz@18: | | | short day, or four for the full name, or five for the | cmlenz@18: | | | narrow name. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``e`` | Local day of week. Same as E except adds a numeric | cmlenz@18: | | | value that will depend on the local starting day of | cmlenz@18: | | | the week, using one or two letters. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``c`` | ?? | cmlenz@18: +----------+--------+--------------------------------------------------------+ cmlenz@18: cmlenz@18: ----------- cmlenz@18: Time Fields cmlenz@18: ----------- cmlenz@18: cmlenz@18: +----------+--------+--------------------------------------------------------+ cmlenz@18: | Field | Symbol | Description | cmlenz@18: +==========+========+========================================================+ cmlenz@18: | Period | ``a`` | AM or PM | cmlenz@18: +----------+--------+--------------------------------------------------------+ cmlenz@18: | Hour | ``h`` | Hour [1-12]. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``H`` | Hour [0-23]. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``K`` | Hour [0-11]. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``k`` | Hour [1-24]. | cmlenz@18: +----------+--------+--------------------------------------------------------+ cmlenz@18: | Minute | ``m`` | Use one or two for zero places padding. | cmlenz@18: +----------+--------+--------------------------------------------------------+ cmlenz@18: | Second | ``s`` | Use one or two for zero places padding. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``S`` | Fractional second, rounds to the count of letters. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``A`` | Milliseconds in day. | cmlenz@18: +----------+--------+--------------------------------------------------------+ cmlenz@18: | Timezone | ``z`` | Use one to three letters for the short timezone or | cmlenz@18: | | | four for the full name. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``Z`` | Use one to three letters for RFC 822, four letters for | cmlenz@18: | | | GMT format. | cmlenz@18: | +--------+--------------------------------------------------------+ cmlenz@18: | | ``v`` | Use one letter for short wall (generic) time, four for | cmlenz@18: | | | long wall time. | cmlenz@18: +----------+--------+--------------------------------------------------------+ cmlenz@18: cmlenz@18: cmlenz@18: Parsing Dates cmlenz@18: ------------- cmlenz@18: cmlenz@18: Babel can also parse date and time information in a locale-sensitive manner:: cmlenz@18: cmlenz@18: >>> from babel.dates import parse_date, parse_datetime, parse_time cmlenz@18: cmlenz@18: cmlenz@18: Number Formatting cmlenz@18: ================= cmlenz@18: cmlenz@18: cmlenz@18: Pattern Syntax cmlenz@18: -------------- cmlenz@18: cmlenz@18: cmlenz@18: Parsing Numbers cmlenz@18: ---------------