Package babel :: Module dates

# Module dates

Locale dependent formatting and parsing of dates and times.

The default locale for the functions in this module is determined by the following environment variables, in that order:

> - `LC_TIME`,
> - `LC_ALL`, and
> - `LANG`

Functions

`unicode`

get_timezone_name(dt_or_tzinfo=None, width=`'``long``'`, uncommon=False, locale=`'``de_DE``'`) Return the localized display name for the given timezone. |

`unicode`

format_date(date=None, format=`'``medium``'`, locale=`'``de_DE``'`) Return a date formatted according to the given pattern. |

`unicode`

format_datetime(datetime=None, format=`'``medium``'`, tzinfo=None, locale=`'``de_DE``'`) Return a date formatted according to the given pattern. |

`unicode`

format_time(time=None, format=`'``medium``'`, tzinfo=None, locale=`'``de_DE``'`) Return a time formatted according to the given pattern. |

date

parse_date(string, locale=`'``de_DE``'`) Parse a date from a string. |

datetime

parse_datetime(string, locale=`'``de_DE``'`) Parse a date and time from a string. |

time

parse_time(string, locale=`'``de_DE``'`) Parse a time from a string. |

Function Details

### get_timezone_name(dt_or_tzinfo=None, width=`'``long``'`, uncommon=False, locale=`'``de_DE``'`)

Return the localized display name for the given timezone. The timezone may be specified using a `datetime` or tzinfo object.

    >>> from pytz import timezone
    >>> dt = time(15, 30, tzinfo=timezone('America/Los_Angeles'))
    >>> get_timezone_name(dt, locale='en_US')
    u'Pacific Standard Time'
    >>> get_timezone_name(dt, width='short', locale='en_US')
    u'PST'

If this function gets passed only a tzinfo object and no concrete datetime, the returned display name is indenpendent of daylight savings time. This can be used for example for selecting timezones, or to set the time of events that recur across DST changes:

    >>> tz = timezone('America/Los_Angeles')
    >>> get_timezone_name(tz, locale='en_US')
    u'Pacific Time'
    >>> get_timezone_name(tz, 'short', locale='en_US')
    u'PT'

If no localized display name for the timezone is available, and the timezone is associated with a country that uses only a single timezone, the name of that country is returned, formatted according to the locale:

    >>> tz = timezone('Europe/Berlin')
    >>> get_timezone_name(tz, locale='de_DE')
    u'Deutschland'
    >>> get_timezone_name(tz, locale='pt_BR')
    u'Hor\xe1rio Alemanha'

On the other hand, if the country uses multiple timezones, the city is also included in the representation:

    >>> tz = timezone('America/St_Johns')
    >>> get_timezone_name(tz, locale='de_DE')
    u"Kanada (St. John's)"

The `uncommon` parameter can be set to `True` to enable the use of timezone representations that are not commonly used by the requested locale. For example, while in French the central European timezone is usually abbreviated as "HEC", in Canadian French, this abbreviation is not in common use, so a generic name would be chosen by default:

    >>> tz = timezone('Europe/Paris')
    >>> get_timezone_name(tz, 'short', locale='fr_CA')
    u'France'
    >>> get_timezone_name(tz, 'short', uncommon=True, locale='fr_CA')
    u'HEC'

Parameters:
  - `dt_or_tzinfo` - the `datetime` or `tzinfo` object that determines the timezone; if a `tzinfo` object is used, the resulting display name will be generic, i.e. independent of daylight savings time; if `None`, the current date in UTC is assumed
  - `width` - either "long" or "short"
  - `uncommon` - whether even uncommon timezone abbreviations should be used
  - `locale` - the Locale object, or a locale string
Returns: `unicode`
  the timezone display name

Since: version 0.9

See Also: LDML Appendix J: Time Zone Display Names

### format_date(date=None, format=`'``medium``'`, locale=`'``de_DE``'`)

Return a date formatted according to the given pattern.

    >>> d = date(2007, 04, 01)
    >>> format_date(d, locale='en_US')
    u'Apr 1, 2007'
    >>> format_date(d, format='full', locale='de_DE')
    u'Sonntag, 1. April 2007'

If you don't want to use the locale default formats, you can specify a custom date pattern:

    >>> format_date(d, "EEE, MMM d, ''yy", locale='en')
    u"Sun, Apr 1, '07"

Parameters:
  - `date` - the `date` or `datetime` object; if `None`, the current date is used
  - `format` - one of "full", "long", "medium", or "short", or a custom date/time pattern
  - `locale` - a Locale object or a locale identifier
Returns: `unicode`

Note: If the pattern contains time fields, an `AttributeError` will be raised when trying to apply the formatting. This is also true if the value of `date` parameter is actually a `datetime` object, as this function automatically converts that to a `date`.

### format_datetime(datetime=None, format=`'``medium``'`, tzinfo=None, locale=`'``de_DE``'`)

Return a date formatted according to the given pattern.

    >>> dt = datetime(2007, 04, 01, 15, 30)
    >>> format_datetime(dt, locale='en_US')
    u'Apr 1, 2007 3:30:00 PM'

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
    >>> format_datetime(dt, 'full', tzinfo=timezone('Europe/Paris'),
    ...                 locale='fr_FR')
    u'dimanche 1 avril 2007 17:30:00 HEC'
    >>> format_datetime(dt, "yyyy.MM.dd G 'at' HH:mm:ss zzz",
    ...                 tzinfo=timezone('US/Eastern'), locale='en')
    u'2007.04.01 AD at 11:30:00 EDT'

Parameters:
  - `datetime` - the datetime object; if `None`, the current date and time is used
  - `format` - one of "full", "long", "medium", or "short", or a custom date/time pattern
  - `tzinfo` - the timezone to apply to the time for display
  - `locale` - a Locale object or a locale identifier
Returns: `unicode`

### format_time(time=None, format=`'``medium``'`, tzinfo=None, locale=`'``de_DE``'`)

Return a time formatted according to the given pattern.

    >>> t = time(15, 30)
    >>> format_time(t, locale='en_US')
    u'3:30:00 PM'
    >>> format_time(t, format='short', locale='de_DE')
    u'15:30'

If you don't want to use the locale default formats, you can specify a custom time pattern:

    >>> format_time(t, "hh 'o''clock' a", locale='en')
    u"03 o'clock PM"

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
    >>> t = datetime(2007, 4, 1, 15, 30)
    >>> tzinfo = timezone('Europe/Paris')
    >>> t = tzinfo.localize(t)
    >>> format_time(t, format='full', tzinfo=tzinfo, locale='fr_FR')
    u'15:30:00 HEC'
    >>> format_time(t, "hh 'o''clock' a, zzzz", tzinfo=timezone('US/Eastern'),
    ...             locale='en')
    u"09 o'clock AM, Eastern Daylight Time"

As that example shows, when this function gets passed a `datetime.datetime` value, the actual time in the formatted string is adjusted to the timezone specified by the tzinfo parameter. If the `datetime` is "naive" (i.e. it has no associated timezone information), it is assumed to be in UTC.

These timezone calculations are not performed if the value is of type `datetime.time`, as without date information there's no way to determine what a given time would translate to in a different timezone without information about whether daylight savings time is in effect or not. This means that time values are left as-is, and the value of the tzinfo parameter is only used to display the timezone name if needed:

    >>> t = time(15, 30)
    >>> format_time(t, format='full', tzinfo=timezone('Europe/Paris'),
    ...             locale='fr_FR')
    u'15:30:00 HEC'
    >>> format_time(t, format='full', tzinfo=timezone('US/Eastern'),
    ...             locale='en_US')
    u'3:30:00 PM ET'

Parameters:
  - `time` - the `time` or `datetime` object; if `None`, the current time in UTC is used
  - `format` - one of "full", "long", "medium", or "short", or a custom date/time pattern
  - `tzinfo` - the time-zone to apply to the time for display
  - `locale` - a Locale object or a locale identifier
Returns: `unicode`

Note: If the pattern contains date fields, an `AttributeError` will be raised when trying to apply the formatting. This is also true if the value of `time` parameter is actually a `datetime` object, as this function automatically converts that to a `time`.

### parse_date(string, locale=`'``de_DE``'`)

Parse a date from a string.

This function uses the date format for the locale as a hint to determine the order in which the date fields appear in the string.

    >>> parse_date('4/1/04', locale='en_US')
    datetime.date(2004, 4, 1)
    >>> parse_date('01.04.2004', locale='de_DE')
    datetime.date(2004, 4, 1)

Parameters:
  - `string` - the string containing the date
  - `locale` - a Locale object or a locale identifier
Returns: date
  the parsed date

### parse_datetime(string, locale=`'``de_DE``'`)

Parse a date and time from a string.

This function uses the date and time formats for the locale as a hint to determine the order in which the time fields appear in the string.

Parameters:
  - `string` - the string containing the date and time
  - `locale` - a Locale object or a locale identifier
Returns: datetime
  the parsed date/time

### parse_time(string, locale=`'``de_DE``'`)

Parse a time from a string.

This function uses the time format for the locale as a hint to determine the order in which the time fields appear in the string.

    >>> parse_time('15:30:00', locale='en_US')
    datetime.time(15, 30)

Parameters:
  - `string` - the string containing the time
  - `locale` - a Locale object or a locale identifier
Returns: time
  the parsed time