Date Formatting

When working with date and time information in Python, you commonly use the
classes date, datetime and/or time from the datetime package.
Babel provides functions for locale-specific formatting of those objects in its
dates module:

While Babel makes it simple to use the appropriate date/time format for a given
locale, you can also force it to use custom patterns. Note that Babel uses
different patterns for specifying number and date formats compared to the
Python equivalents (such as time.strftime()), which have mostly been
inherited from C and POSIX. The patterns used in Babel are based on the
Locale Data Markup Language specification (LDML), which defines them as
follows:

A date/time pattern is a string of characters, where specific strings of
characters are replaced with date and time data from a calendar when formatting
or used to generate data for a calendar when parsing. […]

Characters may be used multiple times. For example, if y is used for the
year, yy might produce "99", whereas yyyy produces "1999". For most
numerical fields, the number of characters specifies the field width. For
example, if h is the hour, h might produce "5", but hh produces
"05". For some characters, the count specifies whether an abbreviated or full
form should be used […]

Two single quotes represent a literal single quote, either inside or outside
single quotes. Text within single quotes is not interpreted in any way (except
for two adjacent single quotes).

In addition to providing functions for formatting localized dates and times,
the babel.dates module also provides a function to format the difference
between two times, called a ''time delta''. These are usually represented as
datetime.timedelta objects in Python, and it's also what you get when you
subtract one datetime object from an other.

The format_timedelta function takes a timedelta object and returns a
human-readable representation. This happens at the cost of precision, as it
chooses only the most significant unit (such as year, week, or hour) of the
difference, and displays that:

The resulting strings are based from the CLDR data, and are properly
pluralized depending on the plural rules of the locale and the calculated
number of units.

The function provides parameters for you to influence how this most significant
unit is chosen: with threshold you set the value after which the
presentation switches to the next larger unit, and with granularity you
can limit the smallest unit to display:

Many of the verbose time formats include the time-zone, but time-zone
information is not by default available for the Python datetime and
time objects. The standard library includes only the abstract tzinfo
class, which you need appropriate implementations for to actually use in your
application. Babel includes a tzinfo implementation for UTC (Universal
Time).

For real time-zone support, it is strongly recommended that you use the
third-party package pytz, which includes the definitions of practically all
of the time-zones used on the world, as well as important functions for
reliably converting from UTC to local time, and vice versa:

The recommended approach to deal with different time-zones in a Python
application is to always use UTC internally, and only convert from/to the users
time-zone when accepting user input and displaying date/time data, respectively.
You can use Babel together with pytz to apply a time-zone to any
datetime or time object for display, leaving the original information
unchanged:

Here, the given UTC time is adjusted to the "Europe/London" time-zone, and
daylight savings time is taken into account. Daylight savings time is also
applied to format_time, but because the actual date is unknown in that
case, the current day is assumed to determine whether DST or standard time
should be used.

While the Locale class provides access to various locale display names
related to time-zones, the process of building a localized name of a time-zone
is actually quite complicated. Babel implements it in separately usable
functions in the babel.dates module, most importantly the
get_timezone_name function:

You can pass the function either a datetime.tzinfo object, or a
datetime.date or datetime.datetime object. If you pass an actual date,
the function will be able to take daylight savings time into account. If you
pass just the time-zone, Babel does not know whether daylight savings time is
in effect, so it uses a generic representation, which is useful for example to
display a list of time-zones to the user.