cmlenz@2: .. -*- mode: rst; encoding: utf-8 -*- cmlenz@2: cmlenz@124: =============== cmlenz@124: Date Formatting cmlenz@124: =============== cmlenz@2: cmlenz@2: cmlenz@2: .. contents:: Contents cmlenz@2: :depth: 2 cmlenz@2: .. sectnum:: cmlenz@2: cmlenz@2: cmlenz@18: When working with date and time information in Python, you commonly use the cmlenz@98: 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@40: ``dates`` module: cmlenz@40: cmlenz@98: .. _`datetime`: http://docs.python.org/lib/module-datetime.html cmlenz@98: cmlenz@40: .. code-block:: pycon 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@20: * ``long``, and cmlenz@20: * ``full``. cmlenz@18: cmlenz@40: For example: cmlenz@40: cmlenz@40: .. code-block:: pycon 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: cmlenz@18: Pattern Syntax cmlenz@124: ============== 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@40: For example: cmlenz@40: cmlenz@40: .. code-block:: pycon 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: 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: 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@233: | +--------+--------------------------------------------------------+ cmlenz@233: | | ``V`` | Same as ``z``, except that timezone abbreviations | cmlenz@233: | | | should be used regardless of whether they are in | cmlenz@233: | | | common use by the locale. | cmlenz@18: +----------+--------+--------------------------------------------------------+ cmlenz@18: cmlenz@18: cmlenz@394: Time Delta Formatting cmlenz@394: ===================== cmlenz@394: cmlenz@394: In addition to providing functions for formatting localized dates and times, cmlenz@394: the ``babel.dates`` module also provides a function to format the difference cmlenz@394: between two times, called a ''time delta''. These are usually represented as cmlenz@394: ``datetime.timedelta`` objects in Python, and it's also what you get when you cmlenz@394: subtract one ``datetime`` object from an other. cmlenz@394: cmlenz@394: The ``format_timedelta`` function takes a ``timedelta`` object and returns a cmlenz@394: human-readable representation. This happens at the cost of precision, as it cmlenz@394: chooses only the most significant unit (such as year, week, or hour) of the cmlenz@394: difference, and displays that: cmlenz@394: cmlenz@394: .. code-block:: pycon cmlenz@394: cmlenz@394: >>> from datetime import timedelta cmlenz@394: >>> from babel.dates import format_timedelta cmlenz@394: >>> delta = timedelta(days=6) cmlenz@394: >>> format_timedelta(delta, locale='en_US') cmlenz@394: u'1 week' cmlenz@394: cmlenz@394: The resulting strings are based from the CLDR data, and are properly cmlenz@394: pluralized depending on the plural rules of the locale and the calculated cmlenz@394: number of units. cmlenz@394: cmlenz@394: The function provides parameters for you to influence how this most significant cmlenz@394: unit is chosen: with ``threshold`` you set the value after which the cmlenz@394: presentation switches to the next larger unit, and with ``granularity`` you cmlenz@394: can limit the smallest unit to display: cmlenz@394: cmlenz@394: .. code-block:: pycon cmlenz@394: cmlenz@394: >>> delta = timedelta(days=6) cmlenz@394: >>> format_timedelta(delta, threshold=1.2, locale='en_US') cmlenz@394: u'6 days' cmlenz@394: >>> format_timedelta(delta, granularity='month', locale='en_US') cmlenz@395: u'1 month' cmlenz@394: cmlenz@394: cmlenz@255: Time-zone Support cmlenz@124: ================= cmlenz@29: cmlenz@31: Many of the verbose time formats include the time-zone, but time-zone cmlenz@31: information is not by default available for the Python ``datetime`` and cmlenz@31: ``time`` objects. The standard library includes only the abstract ``tzinfo`` cmlenz@31: class, which you need appropriate implementations for to actually use in your cmlenz@29: application. Babel includes a ``tzinfo`` implementation for UTC (Universal cmlenz@31: Time). cmlenz@31: cmlenz@31: For real time-zone support, it is strongly recommended that you use the cmlenz@29: third-party package `pytz`_, which includes the definitions of practically all cmlenz@29: of the time-zones used on the world, as well as important functions for cmlenz@40: reliably converting from UTC to local time, and vice versa: cmlenz@40: cmlenz@40: .. code-block:: pycon cmlenz@29: cmlenz@29: >>> from datetime import time cmlenz@34: >>> from pytz import timezone, utc cmlenz@34: >>> dt = datetime(2007, 04, 01, 15, 30, tzinfo=utc) cmlenz@34: >>> eastern = timezone('US/Eastern') cmlenz@34: >>> format_datetime(dt, 'H:mm Z', tzinfo=eastern, locale='en_US') cmlenz@34: u'11:30 -0400' cmlenz@29: cmlenz@29: The recommended approach to deal with different time-zones in a Python cmlenz@29: application is to always use UTC internally, and only convert from/to the users cmlenz@29: time-zone when accepting user input and displaying date/time data, respectively. cmlenz@34: You can use Babel together with ``pytz`` to apply a time-zone to any cmlenz@34: ``datetime`` or ``time`` object for display, leaving the original information cmlenz@40: unchanged: cmlenz@40: cmlenz@40: .. code-block:: pycon cmlenz@34: cmlenz@34: >>> british = timezone('Europe/London') cmlenz@34: >>> format_datetime(dt, 'H:mm zzzz', tzinfo=british, locale='en_US') cmlenz@34: u'16:30 British Summer Time' cmlenz@29: cmlenz@35: Here, the given UTC time is adjusted to the "Europe/London" time-zone, and cmlenz@35: daylight savings time is taken into account. Daylight savings time is also cmlenz@35: applied to ``format_time``, but because the actual date is unknown in that cmlenz@35: case, the current day is assumed to determine whether DST or standard time cmlenz@35: should be used. cmlenz@35: cmlenz@29: .. _`pytz`: http://pytz.sourceforge.net/ cmlenz@29: cmlenz@29: cmlenz@255: Localized Time-zone Names cmlenz@255: ------------------------- cmlenz@255: cmlenz@255: While the ``Locale`` class provides access to various locale display names cmlenz@255: related to time-zones, the process of building a localized name of a time-zone cmlenz@255: is actually quite complicated. Babel implements it in separately usable cmlenz@255: functions in the ``babel.dates`` module, most importantly the cmlenz@255: ``get_timezone_name`` function: cmlenz@255: cmlenz@255: .. code-block:: pycon cmlenz@255: cmlenz@255: >>> from pytz import timezone cmlenz@255: >>> from babel import Locale cmlenz@255: >>> from babel.dates import get_timezone_name cmlenz@255: cmlenz@255: >>> tz = timezone('Europe/Berlin') cmlenz@255: >>> get_timezone_name(tz, locale=Locale.parse('pt_PT')) cmlenz@255: u'Hor\xe1rio Alemanha' cmlenz@255: cmlenz@255: You can pass the function either a ``datetime.tzinfo`` object, or a cmlenz@255: ``datetime.date`` or ``datetime.datetime`` object. If you pass an actual date, cmlenz@255: the function will be able to take daylight savings time into account. If you cmlenz@255: pass just the time-zone, Babel does not know whether daylight savings time is cmlenz@255: in effect, so it uses a generic representation, which is useful for example to cmlenz@255: display a list of time-zones to the user. cmlenz@255: cmlenz@255: .. code-block:: pycon cmlenz@255: cmlenz@255: >>> from datetime import datetime cmlenz@255: cmlenz@255: >>> dt = tz.localize(datetime(2007, 8, 15)) cmlenz@255: >>> get_timezone_name(dt, locale=Locale.parse('de_DE')) cmlenz@255: u'Mitteleurop\xe4ische Sommerzeit' cmlenz@255: >>> get_timezone_name(tz, locale=Locale.parse('de_DE')) cmlenz@255: u'Deutschland' cmlenz@255: cmlenz@255: cmlenz@18: Parsing Dates cmlenz@124: ============= cmlenz@18: cmlenz@40: Babel can also parse date and time information in a locale-sensitive manner: cmlenz@40: cmlenz@40: .. code-block:: pycon cmlenz@18: cmlenz@18: >>> from babel.dates import parse_date, parse_datetime, parse_time cmlenz@255: cmlenz@255: .. note:: Date/time parsing is not properly implemented yet