Mercurial > babel > old > mirror
annotate doc/formatting.txt @ 39:52b98eca619f
Add optional pytz dependency to `INSTALL.txt`.
author | cmlenz |
---|---|
date | Tue, 05 Jun 2007 10:38:11 +0000 |
parents | 3b23c60191e0 |
children | cf94e70a77f3 |
rev | line source |
---|---|
4 | 1 .. -*- mode: rst; encoding: utf-8 -*- |
2 | |
3 ========================== | |
4 Number and Date Formatting | |
5 ========================== | |
6 | |
7 | |
8 .. contents:: Contents | |
9 :depth: 2 | |
10 .. sectnum:: | |
11 | |
12 | |
20
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
13 Date Formatting |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
14 =============== |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
15 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
16 When working with date and time information in Python, you commonly use the |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
17 classes ``date``, ``datetime`` and/or ``time`` from the `datetime package`_. |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
18 Babel provides functions for locale-specific formatting of those objects in its |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
19 ``dates`` module:: |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
20 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
21 >>> from datetime import date, datetime, time |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
22 >>> from babel.dates import format_date, format_datetime, format_time |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
23 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
24 >>> d = date(2007, 4, 1) |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
25 >>> format_date(d, locale='en') |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
26 u'Apr 1, 2007' |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
27 >>> format_date(d, locale='de_DE') |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
28 u'01.04.2007' |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
29 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
30 As this example demonstrates, Babel will automatically choose a date format |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
31 that is appropriate for the requested locale. |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
32 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
33 The ``format_*()`` functions also accept an optional ``format`` argument, which |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
34 allows you to choose between one of four format variations: |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
35 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
36 * ``short``, |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
37 * ``medium`` (the default), |
22 | 38 * ``long``, and |
39 * ``full``. | |
20
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
40 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
41 For example:: |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
42 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
43 >>> format_date(d, format='short', locale='en') |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
44 u'4/1/07' |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
45 >>> format_date(d, format='long', locale='en') |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
46 u'April 1, 2007' |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
47 >>> format_date(d, format='full', locale='en') |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
48 u'Sunday, April 1, 2007' |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
49 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
50 .. _`datetime package`: http://docs.python.org/lib/module-datetime.html |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
51 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
52 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
53 Pattern Syntax |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
54 -------------- |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
55 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
56 While Babel makes it simple to use the appropriate date/time format for a given |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
57 locale, you can also force it to use custom patterns. Note that Babel uses |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
58 different patterns for specifying number and date formats compared to the |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
59 Python equivalents (such as ``time.strftime()``), which have mostly been |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
60 inherited from C and POSIX. The patterns used in Babel are based on the |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
61 `Locale Data Markup Language specification`_ (LDML), which defines them as |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
62 follows: |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
63 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
64 A date/time pattern is a string of characters, where specific strings of |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
65 characters are replaced with date and time data from a calendar when formatting |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
66 or used to generate data for a calendar when parsing. […] |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
67 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
68 Characters may be used multiple times. For example, if ``y`` is used for the |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
69 year, ``yy`` might produce "99", whereas ``yyyy`` produces "1999". For most |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
70 numerical fields, the number of characters specifies the field width. For |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
71 example, if ``h`` is the hour, ``h`` might produce "5", but ``hh`` produces |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
72 "05". For some characters, the count specifies whether an abbreviated or full |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
73 form should be used […] |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
74 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
75 Two single quotes represent a literal single quote, either inside or outside |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
76 single quotes. Text within single quotes is not interpreted in any way (except |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
77 for two adjacent single quotes). |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
78 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
79 For example:: |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
80 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
81 >>> d = date(2007, 4, 1) |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
82 >>> format_date(d, "EEE, MMM d, ''yy", locale='en') |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
83 u"Sun, Apr 1, '07" |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
84 >>> format_date(d, "EEEE, d.M.yyyy", locale='de') |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
85 u'Sonntag, 1.4.2007' |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
86 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
87 >>> t = time(15, 30) |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
88 >>> format_time(t, "hh 'o''clock' a", locale='en') |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
89 u"03 o'clock PM" |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
90 >>> format_time(t, 'H:mm a', locale='de') |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
91 u'15:30 nachm.' |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
92 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
93 >>> dt = datetime(2007, 4, 1, 15, 30) |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
94 >>> format_datetime(dt, "yyyyy.MMMM.dd GGG hh:mm a", locale='en') |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
95 u'02007.April.01 AD 03:30 PM' |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
96 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
97 The syntax for custom datetime format patterns is described in detail in the |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
98 the `Locale Data Markup Language specification`_. The following table is just a |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
99 relatively brief overview. |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
100 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
101 .. _`Locale Data Markup Language specification`: http://unicode.org/reports/tr35/#Date_Format_Patterns |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
102 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
103 ----------- |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
104 Date Fields |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
105 ----------- |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
106 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
107 +----------+--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
108 | Field | Symbol | Description | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
109 +==========+========+========================================================+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
110 | Era | ``G`` | Replaced with the era string for the current date. One | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
111 | | | to three letters for the abbreviated form, four | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
112 | | | lettersfor the long form, five for the narrow form | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
113 +----------+--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
114 | Year | ``y`` | Replaced by the year. Normally the length specifies | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
115 | | | the padding, but for two letters it also specifies the | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
116 | | | maximum length. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
117 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
118 | | ``Y`` | Same as ``y`` but uses the ISO year-week calendar. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
119 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
120 | | ``u`` | ?? | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
121 +----------+--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
122 | Quarter | ``Q`` | Use one or two for the numerical quarter, three for | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
123 | | | the abbreviation, or four for the full name. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
124 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
125 | | ``q`` | Use one or two for the numerical quarter, three for | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
126 | | | the abbreviation, or four for the full name. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
127 +----------+--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
128 | Month | ``M`` | Use one or two for the numerical month, three for the | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
129 | | | abbreviation, or four for the full name, or five for | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
130 | | | the narrow name. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
131 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
132 | | ``L`` | Use one or two for the numerical month, three for the | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
133 | | | abbreviation, or four for the full name, or 5 for the | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
134 | | | narrow name. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
135 +----------+--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
136 | Week | ``w`` | Week of year. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
137 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
138 | | ``W`` | Week of month. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
139 +----------+--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
140 | Day | ``d`` | Day of month. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
141 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
142 | | ``D`` | Day of year. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
143 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
144 | | ``F`` | Day of week in month. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
145 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
146 | | ``g`` | ?? | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
147 +----------+--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
148 | Week day | ``E`` | Day of week. Use one through three letters for the | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
149 | | | short day, or four for the full name, or five for the | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
150 | | | narrow name. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
151 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
152 | | ``e`` | Local day of week. Same as E except adds a numeric | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
153 | | | value that will depend on the local starting day of | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
154 | | | the week, using one or two letters. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
155 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
156 | | ``c`` | ?? | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
157 +----------+--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
158 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
159 ----------- |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
160 Time Fields |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
161 ----------- |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
162 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
163 +----------+--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
164 | Field | Symbol | Description | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
165 +==========+========+========================================================+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
166 | Period | ``a`` | AM or PM | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
167 +----------+--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
168 | Hour | ``h`` | Hour [1-12]. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
169 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
170 | | ``H`` | Hour [0-23]. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
171 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
172 | | ``K`` | Hour [0-11]. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
173 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
174 | | ``k`` | Hour [1-24]. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
175 +----------+--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
176 | Minute | ``m`` | Use one or two for zero places padding. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
177 +----------+--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
178 | Second | ``s`` | Use one or two for zero places padding. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
179 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
180 | | ``S`` | Fractional second, rounds to the count of letters. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
181 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
182 | | ``A`` | Milliseconds in day. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
183 +----------+--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
184 | Timezone | ``z`` | Use one to three letters for the short timezone or | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
185 | | | four for the full name. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
186 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
187 | | ``Z`` | Use one to three letters for RFC 822, four letters for | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
188 | | | GMT format. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
189 | +--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
190 | | ``v`` | Use one letter for short wall (generic) time, four for | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
191 | | | long wall time. | |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
192 +----------+--------+--------------------------------------------------------+ |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
193 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
194 |
33 | 195 Time-Zone Support |
31 | 196 ----------------- |
197 | |
33 | 198 Many of the verbose time formats include the time-zone, but time-zone |
199 information is not by default available for the Python ``datetime`` and | |
200 ``time`` objects. The standard library includes only the abstract ``tzinfo`` | |
201 class, which you need appropriate implementations for to actually use in your | |
31 | 202 application. Babel includes a ``tzinfo`` implementation for UTC (Universal |
33 | 203 Time). |
204 | |
205 For real time-zone support, it is strongly recommended that you use the | |
31 | 206 third-party package `pytz`_, which includes the definitions of practically all |
207 of the time-zones used on the world, as well as important functions for | |
208 reliably converting from UTC to local time, and vice versa:: | |
209 | |
210 >>> from datetime import time | |
36 | 211 >>> from pytz import timezone, utc |
212 >>> dt = datetime(2007, 04, 01, 15, 30, tzinfo=utc) | |
213 >>> eastern = timezone('US/Eastern') | |
214 >>> format_datetime(dt, 'H:mm Z', tzinfo=eastern, locale='en_US') | |
215 u'11:30 -0400' | |
31 | 216 |
217 The recommended approach to deal with different time-zones in a Python | |
218 application is to always use UTC internally, and only convert from/to the users | |
219 time-zone when accepting user input and displaying date/time data, respectively. | |
36 | 220 You can use Babel together with ``pytz`` to apply a time-zone to any |
221 ``datetime`` or ``time`` object for display, leaving the original information | |
222 unchanged:: | |
223 | |
224 >>> british = timezone('Europe/London') | |
225 >>> format_datetime(dt, 'H:mm zzzz', tzinfo=british, locale='en_US') | |
226 u'16:30 British Summer Time' | |
31 | 227 |
37 | 228 Here, the given UTC time is adjusted to the "Europe/London" time-zone, and |
229 daylight savings time is taken into account. Daylight savings time is also | |
230 applied to ``format_time``, but because the actual date is unknown in that | |
231 case, the current day is assumed to determine whether DST or standard time | |
232 should be used. | |
233 | |
31 | 234 .. _`pytz`: http://pytz.sourceforge.net/ |
235 | |
236 | |
20
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
237 Parsing Dates |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
238 ------------- |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
239 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
240 Babel can also parse date and time information in a locale-sensitive manner:: |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
241 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
242 >>> from babel.dates import parse_date, parse_datetime, parse_time |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
243 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
244 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
245 Number Formatting |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
246 ================= |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
247 |
34 | 248 Support for locale-specific formatting and parsing of numbers is provided by |
249 the ``babel.numbers`` module:: | |
250 | |
251 >>> from babel.numbers import format_number, format_decimal, format_percent | |
252 | |
253 Examples:: | |
254 | |
255 >>> format_decimal(1.2345, locale='en_US') | |
256 u'1.234' | |
257 >>> format_decimal(1.2345, locale='sv_SE') | |
258 u'1,234' | |
259 >>> format_decimal(12345, locale='de_DE') | |
260 u'12.345' | |
261 | |
20
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
262 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
263 Pattern Syntax |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
264 -------------- |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
265 |
34 | 266 While Babel makes it simple to use the appropriate number format for a given |
267 locale, you can also force it to use custom patterns. As with date/time | |
268 formatting patterns, the patterns Babel supports for number formatting are | |
269 based on the `Locale Data Markup Language specification`_ (LDML). | |
270 | |
271 Examples:: | |
272 | |
273 >>> format_decimal(-1.2345, format='#,##0.##;-#', locale='en') | |
274 u'-1.23' | |
275 >>> format_decimal(-1.2345, format='#,##0.##;(#)', locale='en') | |
276 u'(1.23)' | |
277 | |
278 The syntax for custom number format patterns is described in detail in the | |
279 the specification. The following table is just a relatively brief overview. | |
280 | |
281 +----------+-----------------------------------------------------------------+ | |
282 | Symbol | Description | | |
283 +==========+=================================================================+ | |
284 | ``0`` | Digit | | |
285 +----------+-----------------------------------------------------------------+ | |
286 | ``1-9`` | '1' through '9' indicate rounding. | | |
287 +----------+-----------------------------------------------------------------+ | |
288 | ``@`` | Significant digit | | |
289 +----------+-----------------------------------------------------------------+ | |
290 | ``#`` | Digit, zero shows as absent | | |
291 +----------+-----------------------------------------------------------------+ | |
292 | ``.`` | Decimal separator or monetary decimal separator | | |
293 +----------+-----------------------------------------------------------------+ | |
294 | ``-`` | Minus sign | | |
295 +----------+-----------------------------------------------------------------+ | |
296 | ``,`` | Grouping separator | | |
297 +----------+-----------------------------------------------------------------+ | |
298 | ``E`` | Separates mantissa and exponent in scientific notation | | |
299 +----------+-----------------------------------------------------------------+ | |
300 | ``+`` | Prefix positive exponents with localized plus sign | | |
301 +----------+-----------------------------------------------------------------+ | |
302 | ``;`` | Separates positive and negative subpatterns | | |
303 +----------+-----------------------------------------------------------------+ | |
304 | ``%`` | Multiply by 100 and show as percentage | | |
305 +----------+-----------------------------------------------------------------+ | |
306 | ``‰`` | Multiply by 1000 and show as per mille | | |
307 +----------+-----------------------------------------------------------------+ | |
308 | ``¤`` | Currency sign, replaced by currency symbol. If doubled, | | |
309 | | replaced by international currency symbol. If tripled, uses the | | |
310 | | long form of the decimal symbol. | | |
311 +----------+-----------------------------------------------------------------+ | |
312 | ``'`` | Used to quote special characters in a prefix or suffix | | |
313 +----------+-----------------------------------------------------------------+ | |
314 | ``*`` | Pad escape, precedes pad character | | |
315 +----------+-----------------------------------------------------------------+ | |
316 | |
20
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
317 |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
318 Parsing Numbers |
dce4cfd4ba5d
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
4
diff
changeset
|
319 --------------- |
34 | 320 |
321 Babel can also parse numeric data in a locale-sensitive manner:: | |
322 | |
36 | 323 >>> from babel.numbers import parse_decimal, parse_number |
34 | 324 |
325 Examples:: | |
326 | |
327 >>> parse_decimal('1,099.98', locale='en_US') | |
328 1099.98 | |
329 >>> parse_decimal('1.099,98', locale='de') | |
330 1099.98 | |
331 >>> parse_decimal('2,109,998', locale='de') | |
332 Traceback (most recent call last): | |
333 ... | |
334 NumberFormatError: '2,109,998' is not a valid decimal number |