cmlenz@263: .. -*- mode: rst; encoding: utf-8 -*- cmlenz@263: cmlenz@263: =============== cmlenz@263: Date Formatting cmlenz@263: =============== cmlenz@263: cmlenz@263: cmlenz@263: .. contents:: Contents cmlenz@263: :depth: 2 cmlenz@263: .. sectnum:: cmlenz@263: cmlenz@263: cmlenz@263: When working with date and time information in Python, you commonly use the cmlenz@263: classes ``date``, ``datetime`` and/or ``time`` from the `datetime`_ package. cmlenz@263: Babel provides functions for locale-specific formatting of those objects in its cmlenz@263: ``dates`` module: cmlenz@263: cmlenz@263: .. _`datetime`: http://docs.python.org/lib/module-datetime.html cmlenz@263: cmlenz@263: .. code-block:: pycon cmlenz@263: cmlenz@263: >>> from datetime import date, datetime, time cmlenz@263: >>> from babel.dates import format_date, format_datetime, format_time cmlenz@263: cmlenz@263: >>> d = date(2007, 4, 1) cmlenz@263: >>> format_date(d, locale='en') cmlenz@263: u'Apr 1, 2007' cmlenz@263: >>> format_date(d, locale='de_DE') cmlenz@263: u'01.04.2007' cmlenz@263: cmlenz@263: As this example demonstrates, Babel will automatically choose a date format cmlenz@263: that is appropriate for the requested locale. cmlenz@263: cmlenz@263: The ``format_*()`` functions also accept an optional ``format`` argument, which cmlenz@263: allows you to choose between one of four format variations: cmlenz@263: cmlenz@263: * ``short``, cmlenz@263: * ``medium`` (the default), cmlenz@263: * ``long``, and cmlenz@263: * ``full``. cmlenz@263: cmlenz@263: For example: cmlenz@263: cmlenz@263: .. code-block:: pycon cmlenz@263: cmlenz@263: >>> format_date(d, format='short', locale='en') cmlenz@263: u'4/1/07' cmlenz@263: >>> format_date(d, format='long', locale='en') cmlenz@263: u'April 1, 2007' cmlenz@263: >>> format_date(d, format='full', locale='en') cmlenz@263: u'Sunday, April 1, 2007' cmlenz@263: cmlenz@263: cmlenz@263: Pattern Syntax cmlenz@263: ============== cmlenz@263: cmlenz@263: While Babel makes it simple to use the appropriate date/time format for a given cmlenz@263: locale, you can also force it to use custom patterns. Note that Babel uses cmlenz@263: different patterns for specifying number and date formats compared to the cmlenz@263: Python equivalents (such as ``time.strftime()``), which have mostly been cmlenz@263: inherited from C and POSIX. The patterns used in Babel are based on the cmlenz@263: `Locale Data Markup Language specification`_ (LDML), which defines them as cmlenz@263: follows: cmlenz@263: cmlenz@263: A date/time pattern is a string of characters, where specific strings of cmlenz@263: characters are replaced with date and time data from a calendar when formatting cmlenz@263: or used to generate data for a calendar when parsing. […] cmlenz@263: cmlenz@263: Characters may be used multiple times. For example, if ``y`` is used for the cmlenz@263: year, ``yy`` might produce "99", whereas ``yyyy`` produces "1999". For most cmlenz@263: numerical fields, the number of characters specifies the field width. For cmlenz@263: example, if ``h`` is the hour, ``h`` might produce "5", but ``hh`` produces cmlenz@263: "05". For some characters, the count specifies whether an abbreviated or full cmlenz@263: form should be used […] cmlenz@263: cmlenz@263: Two single quotes represent a literal single quote, either inside or outside cmlenz@263: single quotes. Text within single quotes is not interpreted in any way (except cmlenz@263: for two adjacent single quotes). cmlenz@263: cmlenz@263: For example: cmlenz@263: cmlenz@263: .. code-block:: pycon cmlenz@263: cmlenz@263: >>> d = date(2007, 4, 1) cmlenz@263: >>> format_date(d, "EEE, MMM d, ''yy", locale='en') cmlenz@263: u"Sun, Apr 1, '07" cmlenz@263: >>> format_date(d, "EEEE, d.M.yyyy", locale='de') cmlenz@263: u'Sonntag, 1.4.2007' cmlenz@263: cmlenz@263: >>> t = time(15, 30) cmlenz@263: >>> format_time(t, "hh 'o''clock' a", locale='en') cmlenz@263: u"03 o'clock PM" cmlenz@263: >>> format_time(t, 'H:mm a', locale='de') cmlenz@263: u'15:30 nachm.' cmlenz@263: cmlenz@263: >>> dt = datetime(2007, 4, 1, 15, 30) cmlenz@263: >>> format_datetime(dt, "yyyyy.MMMM.dd GGG hh:mm a", locale='en') cmlenz@263: u'02007.April.01 AD 03:30 PM' cmlenz@263: cmlenz@263: The syntax for custom datetime format patterns is described in detail in the cmlenz@263: the `Locale Data Markup Language specification`_. The following table is just a cmlenz@263: relatively brief overview. cmlenz@263: cmlenz@263: .. _`Locale Data Markup Language specification`: http://unicode.org/reports/tr35/#Date_Format_Patterns cmlenz@263: cmlenz@263: Date Fields cmlenz@263: ----------- cmlenz@263: cmlenz@263: +----------+--------+--------------------------------------------------------+ cmlenz@263: | Field | Symbol | Description | cmlenz@263: +==========+========+========================================================+ cmlenz@263: | Era | ``G`` | Replaced with the era string for the current date. One | cmlenz@263: | | | to three letters for the abbreviated form, four | cmlenz@263: | | | lettersfor the long form, five for the narrow form | cmlenz@263: +----------+--------+--------------------------------------------------------+ cmlenz@263: | Year | ``y`` | Replaced by the year. Normally the length specifies | cmlenz@263: | | | the padding, but for two letters it also specifies the | cmlenz@263: | | | maximum length. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``Y`` | Same as ``y`` but uses the ISO year-week calendar. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``u`` | ?? | cmlenz@263: +----------+--------+--------------------------------------------------------+ cmlenz@263: | Quarter | ``Q`` | Use one or two for the numerical quarter, three for | cmlenz@263: | | | the abbreviation, or four for the full name. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``q`` | Use one or two for the numerical quarter, three for | cmlenz@263: | | | the abbreviation, or four for the full name. | cmlenz@263: +----------+--------+--------------------------------------------------------+ cmlenz@263: | Month | ``M`` | Use one or two for the numerical month, three for the | cmlenz@263: | | | abbreviation, or four for the full name, or five for | cmlenz@263: | | | the narrow name. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``L`` | Use one or two for the numerical month, three for the | cmlenz@263: | | | abbreviation, or four for the full name, or 5 for the | cmlenz@263: | | | narrow name. | cmlenz@263: +----------+--------+--------------------------------------------------------+ cmlenz@263: | Week | ``w`` | Week of year. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``W`` | Week of month. | cmlenz@263: +----------+--------+--------------------------------------------------------+ cmlenz@263: | Day | ``d`` | Day of month. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``D`` | Day of year. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``F`` | Day of week in month. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``g`` | ?? | cmlenz@263: +----------+--------+--------------------------------------------------------+ cmlenz@263: | Week day | ``E`` | Day of week. Use one through three letters for the | cmlenz@263: | | | short day, or four for the full name, or five for the | cmlenz@263: | | | narrow name. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``e`` | Local day of week. Same as E except adds a numeric | cmlenz@263: | | | value that will depend on the local starting day of | cmlenz@263: | | | the week, using one or two letters. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``c`` | ?? | cmlenz@263: +----------+--------+--------------------------------------------------------+ cmlenz@263: cmlenz@263: Time Fields cmlenz@263: ----------- cmlenz@263: cmlenz@263: +----------+--------+--------------------------------------------------------+ cmlenz@263: | Field | Symbol | Description | cmlenz@263: +==========+========+========================================================+ cmlenz@263: | Period | ``a`` | AM or PM | cmlenz@263: +----------+--------+--------------------------------------------------------+ cmlenz@263: | Hour | ``h`` | Hour [1-12]. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``H`` | Hour [0-23]. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``K`` | Hour [0-11]. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``k`` | Hour [1-24]. | cmlenz@263: +----------+--------+--------------------------------------------------------+ cmlenz@263: | Minute | ``m`` | Use one or two for zero places padding. | cmlenz@263: +----------+--------+--------------------------------------------------------+ cmlenz@263: | Second | ``s`` | Use one or two for zero places padding. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``S`` | Fractional second, rounds to the count of letters. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``A`` | Milliseconds in day. | cmlenz@263: +----------+--------+--------------------------------------------------------+ cmlenz@263: | Timezone | ``z`` | Use one to three letters for the short timezone or | cmlenz@263: | | | four for the full name. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``Z`` | Use one to three letters for RFC 822, four letters for | cmlenz@263: | | | GMT format. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``v`` | Use one letter for short wall (generic) time, four for | cmlenz@263: | | | long wall time. | cmlenz@263: | +--------+--------------------------------------------------------+ cmlenz@263: | | ``V`` | Same as ``z``, except that timezone abbreviations | cmlenz@263: | | | should be used regardless of whether they are in | cmlenz@263: | | | common use by the locale. | cmlenz@263: +----------+--------+--------------------------------------------------------+ cmlenz@263: cmlenz@263: cmlenz@263: Time-zone Support cmlenz@263: ================= cmlenz@263: cmlenz@263: Many of the verbose time formats include the time-zone, but time-zone cmlenz@263: information is not by default available for the Python ``datetime`` and cmlenz@263: ``time`` objects. The standard library includes only the abstract ``tzinfo`` cmlenz@263: class, which you need appropriate implementations for to actually use in your cmlenz@263: application. Babel includes a ``tzinfo`` implementation for UTC (Universal cmlenz@263: Time). cmlenz@263: cmlenz@263: For real time-zone support, it is strongly recommended that you use the cmlenz@263: third-party package `pytz`_, which includes the definitions of practically all cmlenz@263: of the time-zones used on the world, as well as important functions for cmlenz@263: reliably converting from UTC to local time, and vice versa: cmlenz@263: cmlenz@263: .. code-block:: pycon cmlenz@263: cmlenz@263: >>> from datetime import time cmlenz@263: >>> from pytz import timezone, utc cmlenz@263: >>> dt = datetime(2007, 04, 01, 15, 30, tzinfo=utc) cmlenz@263: >>> eastern = timezone('US/Eastern') cmlenz@263: >>> format_datetime(dt, 'H:mm Z', tzinfo=eastern, locale='en_US') cmlenz@263: u'11:30 -0400' cmlenz@263: cmlenz@263: The recommended approach to deal with different time-zones in a Python cmlenz@263: application is to always use UTC internally, and only convert from/to the users cmlenz@263: time-zone when accepting user input and displaying date/time data, respectively. cmlenz@263: You can use Babel together with ``pytz`` to apply a time-zone to any cmlenz@263: ``datetime`` or ``time`` object for display, leaving the original information cmlenz@263: unchanged: cmlenz@263: cmlenz@263: .. code-block:: pycon cmlenz@263: cmlenz@263: >>> british = timezone('Europe/London') cmlenz@263: >>> format_datetime(dt, 'H:mm zzzz', tzinfo=british, locale='en_US') cmlenz@263: u'16:30 British Summer Time' cmlenz@263: cmlenz@263: Here, the given UTC time is adjusted to the "Europe/London" time-zone, and cmlenz@263: daylight savings time is taken into account. Daylight savings time is also cmlenz@263: applied to ``format_time``, but because the actual date is unknown in that cmlenz@263: case, the current day is assumed to determine whether DST or standard time cmlenz@263: should be used. cmlenz@263: cmlenz@263: .. _`pytz`: http://pytz.sourceforge.net/ cmlenz@263: cmlenz@263: cmlenz@263: Localized Time-zone Names cmlenz@263: ------------------------- cmlenz@263: cmlenz@263: While the ``Locale`` class provides access to various locale display names cmlenz@263: related to time-zones, the process of building a localized name of a time-zone cmlenz@263: is actually quite complicated. Babel implements it in separately usable cmlenz@263: functions in the ``babel.dates`` module, most importantly the cmlenz@263: ``get_timezone_name`` function: cmlenz@263: cmlenz@263: .. code-block:: pycon cmlenz@263: cmlenz@263: >>> from pytz import timezone cmlenz@263: >>> from babel import Locale cmlenz@263: >>> from babel.dates import get_timezone_name cmlenz@263: cmlenz@263: >>> tz = timezone('Europe/Berlin') cmlenz@263: >>> get_timezone_name(tz, locale=Locale.parse('pt_PT')) cmlenz@263: u'Hor\xe1rio Alemanha' cmlenz@263: cmlenz@263: You can pass the function either a ``datetime.tzinfo`` object, or a cmlenz@263: ``datetime.date`` or ``datetime.datetime`` object. If you pass an actual date, cmlenz@263: the function will be able to take daylight savings time into account. If you cmlenz@263: pass just the time-zone, Babel does not know whether daylight savings time is cmlenz@263: in effect, so it uses a generic representation, which is useful for example to cmlenz@263: display a list of time-zones to the user. cmlenz@263: cmlenz@263: .. code-block:: pycon cmlenz@263: cmlenz@263: >>> from datetime import datetime cmlenz@263: cmlenz@263: >>> dt = tz.localize(datetime(2007, 8, 15)) cmlenz@263: >>> get_timezone_name(dt, locale=Locale.parse('de_DE')) cmlenz@263: u'Mitteleurop\xe4ische Sommerzeit' cmlenz@263: >>> get_timezone_name(tz, locale=Locale.parse('de_DE')) cmlenz@263: u'Deutschland' cmlenz@263: cmlenz@263: cmlenz@263: Parsing Dates cmlenz@263: ============= cmlenz@263: cmlenz@263: Babel can also parse date and time information in a locale-sensitive manner: cmlenz@263: cmlenz@263: .. code-block:: pycon cmlenz@263: cmlenz@263: >>> from babel.dates import parse_date, parse_datetime, parse_time cmlenz@263: cmlenz@263: .. note:: Date/time parsing is not properly implemented yet