Debian Codegraph @

docs/dates.rst

raw ◀ ▶
1 Debian patch touch this file
1 .. -*- mode: rst; encoding: utf-8 -*-
2
3 .. _date-and-time:
4
5 =============
6 Date and Time
7 =============
8
9
10 When working with date and time information in Python, you commonly use the
11 classes ``date``, ``datetime`` and/or ``time`` from the `datetime` package.
12 Babel provides functions for locale-specific formatting of those objects in its
13 ``dates`` module:
14
15 .. code-block:: pycon
16
17 >>> from datetime import date, datetime, time
18 >>> from babel.dates import format_date, format_datetime, format_time
19
20 >>> d = date(2007, 4, 1)
21 >>> format_date(d, locale='en')
22 'Apr 1, 2007'
23 >>> format_date(d, locale='de_DE')
24 '01.04.2007'
25
26 As this example demonstrates, Babel will automatically choose a date format
27 that is appropriate for the requested locale.
28
29 The ``format_*()`` functions also accept an optional ``format`` argument, which
30 allows you to choose between one of four format variations:
31
32 * ``short``,
33 * ``medium`` (the default),
34 * ``long``, and
35 * ``full``.
36
37 For example:
38
39 .. code-block:: pycon
40
41 >>> format_date(d, format='short', locale='en')
42 '4/1/07'
43 >>> format_date(d, format='long', locale='en')
44 'April 1, 2007'
45 >>> format_date(d, format='full', locale='en')
46 'Sunday, April 1, 2007'
47
48 Core Time Concepts
49 ==================
50
51 Working with dates and time can be a complicated thing. Babel attempts to
52 simplify working with them by making some decisions for you. Python's
53 datetime module has different ways to deal with times and dates: naive and
54 timezone-aware datetime objects.
55
56 Babel generally recommends you to store all your time in naive datetime
57 objects and treat them as UTC at all times. This simplifies dealing with
58 time a lot because otherwise you can get into the hairy situation where
59 you are dealing with datetime objects of different timezones. That is
60 tricky because there are situations where time can be ambiguous. This is
61 usually the case when dealing with dates around timezone transitions. The
62 most common case of timezone transition is changes between daylight saving
63 time and standard time.
64
65 As such we recommend to always use UTC internally and only reformat to
66 local time when returning dates to users. At that point the timezone the
67 user has selected can usually be established and Babel can automatically
68 rebase the time for you.
69
70 To get the current time use the :meth:`~datetime.datetime.now` method
71 of the :class:`~datetime.datetime` object,
72 passing :attr:`~datetime.timezone.utc` to it as the timezone.
73
74 For more information about timezones see :ref:`timezone-support`.
75
76 Pattern Syntax
77 ==============
78
79 While Babel makes it simple to use the appropriate date/time format for a given
80 locale, you can also force it to use custom patterns. Note that Babel uses
81 different patterns for specifying number and date formats compared to the
82 Python equivalents (such as ``time.strftime()``), which have mostly been
83 inherited from C and POSIX. The patterns used in Babel are based on the
84 `Locale Data Markup Language specification`_ (LDML), which defines them as
85 follows:
86
87 A date/time pattern is a string of characters, where specific strings of
88 characters are replaced with date and time data from a calendar when
89 formatting or used to generate data for a calendar when parsing. […]
90
91 Characters may be used multiple times. For example, if ``y`` is used for the
92 year, ``yy`` might produce "99", whereas ``yyyy`` produces "1999". For most
93 numerical fields, the number of characters specifies the field width. For
94 example, if ``h`` is the hour, ``h`` might produce "5", but ``hh`` produces
95 "05". For some characters, the count specifies whether an abbreviated or full
96 form should be used […]
97
98 Two single quotes represent a literal single quote, either inside or outside
99 single quotes. Text within single quotes is not interpreted in any way (except
100 for two adjacent single quotes).
101
102 For example:
103
104 .. code-block:: pycon
105
106 >>> d = date(2007, 4, 1)
107 >>> format_date(d, "EEE, MMM d, ''yy", locale='en')
108 "Sun, Apr 1, '07"
109 >>> format_date(d, "EEEE, d.M.yyyy", locale='de')
110 'Sonntag, 1.4.2007'
111
112 >>> t = time(15, 30)
113 >>> format_time(t, "hh 'o''clock' a", locale='en')
114 "03 o'clock PM"
115 >>> format_time(t, 'H:mm a', locale='de')
116 '15:30 nachm.'
117
118 >>> dt = datetime(2007, 4, 1, 15, 30)
119 >>> format_datetime(dt, "yyyyy.MMMM.dd GGG hh:mm a", locale='en')
120 '02007.April.01 AD 03:30 PM'
121
122 The syntax for custom datetime format patterns is described in detail in the
123 the `Locale Data Markup Language specification`_. The following table is just a
124 relatively brief overview.
125
126 .. _`Locale Data Markup Language specification`:
127 https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns
128
129 Date Fields
130 -----------
131
132 +----------+--------+--------------------------------------------------------+
133 | Field | Symbol | Description |
134 +==========+========+========================================================+
135 | Era | ``G`` | Replaced with the era string for the current date. One |
136 | | | to three letters for the abbreviated form, four |
137 | | | lettersfor the long form, five for the narrow form |
138 +----------+--------+--------------------------------------------------------+
139 | Year | ``y`` | Replaced by the year. Normally the length specifies |
140 | | | the padding, but for two letters it also specifies the |
141 | | | maximum length. |
142 | +--------+--------------------------------------------------------+
143 | | ``Y`` | Same as ``y`` but uses the ISO year-week calendar. ISO |
144 | | | year-week increments after completing the last week of |
145 | | | the year. Therefore it may change a few days before or |
146 | | | after ``y``. Recommend use with the ``w`` Symbol. |
147 | +--------+--------------------------------------------------------+
148 | | ``u`` | ?? |
149 +----------+--------+--------------------------------------------------------+
150 | Quarter | ``Q`` | Use one or two for the numerical quarter, three for |
151 | | | the abbreviation, or four for the full name. |
152 | +--------+--------------------------------------------------------+
153 | | ``q`` | Use one or two for the numerical quarter, three for |
154 | | | the abbreviation, or four for the full name. |
155 +----------+--------+--------------------------------------------------------+
156 | Month | ``M`` | Use one or two for the numerical month, three for the |
157 | | | abbreviation, or four for the full name, or five for |
158 | | | the narrow name. |
159 | +--------+--------------------------------------------------------+
160 | | ``L`` | Use one or two for the numerical month, three for the |
161 | | | abbreviation, or four for the full name, or 5 for the |
162 | | | narrow name. |
163 +----------+--------+--------------------------------------------------------+
164 | Week | ``w`` | Week of year according to the ISO year-week calendar. |
165 | | | This may have 52 or 53 weeks depending on the year. |
166 | | | Recommend use with the ``Y`` symbol. |
167 | +--------+--------------------------------------------------------+
168 | | ``W`` | Week of month. |
169 +----------+--------+--------------------------------------------------------+
170 | Day | ``d`` | Day of month. |
171 | +--------+--------------------------------------------------------+
172 | | ``D`` | Day of year. |
173 | +--------+--------------------------------------------------------+
174 | | ``F`` | Day of week in month. |
175 | +--------+--------------------------------------------------------+
176 | | ``g`` | ?? |
177 +----------+--------+--------------------------------------------------------+
178 | Week day | ``E`` | Day of week. Use one through three letters for the |
179 | | | short day, or four for the full name, or five for the |
180 | | | narrow name. |
181 | +--------+--------------------------------------------------------+
182 | | ``e`` | Local day of week. Same as E except adds a numeric |
183 | | | value that will depend on the local starting day of |
184 | | | the week, using one or two letters. |
185 | +--------+--------------------------------------------------------+
186 | | ``c`` | ?? |
187 +----------+--------+--------------------------------------------------------+
188
189 Time Fields
190 -----------
191
192 +----------+--------+--------------------------------------------------------+
193 | Field | Symbol | Description |
194 +==========+========+========================================================+
195 | Period | ``a`` | AM or PM |
196 +----------+--------+--------------------------------------------------------+
197 | Hour | ``h`` | Hour [1-12]. |
198 | +--------+--------------------------------------------------------+
199 | | ``H`` | Hour [0-23]. |
200 | +--------+--------------------------------------------------------+
201 | | ``K`` | Hour [0-11]. |
202 | +--------+--------------------------------------------------------+
203 | | ``k`` | Hour [1-24]. |
204 +----------+--------+--------------------------------------------------------+
205 | Minute | ``m`` | Use one or two for zero places padding. |
206 +----------+--------+--------------------------------------------------------+
207 | Second | ``s`` | Use one or two for zero places padding. |
208 | +--------+--------------------------------------------------------+
209 | | ``S`` | Fractional second, rounds to the count of letters. |
210 | +--------+--------------------------------------------------------+
211 | | ``A`` | Milliseconds in day. |
212 +----------+--------+--------------------------------------------------------+
213 | Timezone | ``z`` | Use one to three letters for the short timezone or |
214 | | | four for the full name. |
215 | +--------+--------------------------------------------------------+
216 | | ``Z`` | Use one to three letters for RFC 822, four letters for |
217 | | | GMT format. |
218 | +--------+--------------------------------------------------------+
219 | | ``v`` | Use one letter for short wall (generic) time, four for |
220 | | | long wall time. |
221 | +--------+--------------------------------------------------------+
222 | | ``V`` | Same as ``z``, except that timezone abbreviations |
223 | | | should be used regardless of whether they are in |
224 | | | common use by the locale. |
225 +----------+--------+--------------------------------------------------------+
226
227
228 Time Delta Formatting
229 =====================
230
231 In addition to providing functions for formatting localized dates and times,
232 the ``babel.dates`` module also provides a function to format the difference
233 between two times, called a ''time delta''. These are usually represented as
234 ``datetime.timedelta`` objects in Python, and it's also what you get when you
235 subtract one ``datetime`` object from an other.
236
237 The ``format_timedelta`` function takes a ``timedelta`` object and returns a
238 human-readable representation. This happens at the cost of precision, as it
239 chooses only the most significant unit (such as year, week, or hour) of the
240 difference, and displays that:
241
242 .. code-block:: pycon
243
244 >>> from datetime import timedelta
245 >>> from babel.dates import format_timedelta
246 >>> delta = timedelta(days=6)
247 >>> format_timedelta(delta, locale='en_US')
248 '1 week'
249
250 The resulting strings are based from the CLDR data, and are properly
251 pluralized depending on the plural rules of the locale and the calculated
252 number of units.
253
254 The function provides parameters for you to influence how this most significant
255 unit is chosen: with ``threshold`` you set the value after which the
256 presentation switches to the next larger unit, and with ``granularity`` you
257 can limit the smallest unit to display:
258
259 .. code-block:: pycon
260
261 >>> delta = timedelta(days=6)
262 >>> format_timedelta(delta, threshold=1.2, locale='en_US')
263 '6 days'
264 >>> format_timedelta(delta, granularity='month', locale='en_US')
265 '1 month'
266
267 .. _timezone-support:
268
269 Time-zone Support
270 =================
271
272 Many of the verbose time formats include the time-zone, but time-zone
273 information is not by default available for the Python ``datetime`` and
274 ``time`` objects. The standard library includes only the abstract ``tzinfo``
275 class, which you need appropriate implementations for to actually use in your
276 application. Babel includes a ``tzinfo`` implementation for UTC (Universal
277 Time).
278
279 Babel uses either `zoneinfo`_ or `pytz`_ for timezone support.
280 If pytz is installed, it is preferred over the standard library's zoneinfo.
281 You can directly interface with either of these modules from within Babel:
282
283 .. code-block:: pycon
284
285 >>> from datetime import time
286 >>> from babel.dates import get_timezone, UTC
287 >>> dt = datetime(2007, 4, 1, 15, 30, tzinfo=UTC)
•288 >>> eastern = get_timezone('America/New_York')
289 >>> format_datetime(dt, 'H:mm Z', tzinfo=eastern, locale='en_US')
290 '11:30 -0400'
291
292 The recommended approach to deal with different time-zones in a Python
293 application is to always use UTC internally, and only convert from/to the users
294 time-zone when accepting user input and displaying date/time data, respectively.
295 You can use Babel together with ``zoneinfo`` or ``pytz`` to apply a time-zone
296 to any ``datetime`` or ``time`` object for display, leaving the original
297 information unchanged:
298
299 .. code-block:: pycon
300
301 >>> british = get_timezone('Europe/London')
302 >>> format_datetime(dt, 'H:mm zzzz', tzinfo=british, locale='en_US')
303 '16:30 British Summer Time'
304
305 Here, the given UTC time is adjusted to the "Europe/London" time-zone, and
306 daylight savings time is taken into account. Daylight savings time is also
307 applied to ``format_time``, but because the actual date is unknown in that
308 case, the current day is assumed to determine whether DST or standard time
309 should be used.
310
311 Babel also provides support for working with the local timezone of
312 your operating system. It's provided through the ``LOCALTZ`` constant:
313
314 .. code-block:: pycon
315
316 >>> from babel.dates import LOCALTZ, get_timezone_name
317 >>> LOCALTZ
318 <DstTzInfo 'Europe/Vienna' CET+1:00:00 STD>
319 >>> get_timezone_name(LOCALTZ)
320 'Central European Time'
321
322 .. _pytz: https://pythonhosted.org/pytz/
323
324
325 Localized Time-zone Names
326 -------------------------
327
328 While the ``Locale`` class provides access to various locale display names
329 related to time-zones, the process of building a localized name of a time-zone
330 is actually quite complicated. Babel implements it in separately usable
331 functions in the ``babel.dates`` module, most importantly the
332 ``get_timezone_name`` function:
333
334 .. code-block:: pycon
335
336 >>> from babel import Locale
337 >>> from babel.dates import get_timezone_name, get_timezone
338
339 >>> tz = get_timezone('Europe/Berlin')
340 >>> get_timezone_name(tz, locale=Locale.parse('pt_PT'))
341 'Hora da Europa Central'
342
343 You can pass the function either a ``datetime.tzinfo`` object, or a
344 ``datetime.date`` or ``datetime.datetime`` object. If you pass an actual date,
345 the function will be able to take daylight savings time into account. If you
346 pass just the time-zone, Babel does not know whether daylight savings time is
347 in effect, so it uses a generic representation, which is useful for example to
348 display a list of time-zones to the user.
349
350 .. code-block:: pycon
351
352 >>> from datetime import datetime
353 >>> from babel.dates import _localize
354
355 >>> dt = _localize(tz, datetime(2007, 8, 15))
356 >>> get_timezone_name(dt, locale=Locale.parse('de_DE'))
357 'Mitteleuropäische Sommerzeit'
358 >>> get_timezone_name(tz, locale=Locale.parse('de_DE'))
359 'Mitteleuropäische Zeit'