Mercurial > babel > mirror
annotate babel/dates.py @ 72:e0bb7dce49ea trunk
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
author | cmlenz |
---|---|
date | Fri, 08 Jun 2007 19:10:51 +0000 |
parents | 29b88754e13a |
children | 0f641136aa6b |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
3 # Copyright (C) 2007 Edgewall Software | |
4 # All rights reserved. | |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
8 # are also available at http://babel.edgewall.org/wiki/License. | |
9 # | |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
12 # history and logs, available at http://babel.edgewall.org/log/. | |
13 | |
14 """Locale dependent formatting and parsing of dates and times. | |
15 | |
16 The default locale for the functions in this module is determined by the | |
17 following environment variables, in that order: | |
18 | |
19 * ``LC_TIME``, | |
20 * ``LC_ALL``, and | |
21 * ``LANG`` | |
22 """ | |
23 | |
29 | 24 from datetime import date, datetime, time, timedelta, tzinfo |
38
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
25 import re |
1 | 26 |
72
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
46
diff
changeset
|
27 from babel.core import default_locale, Locale |
39
008de2f257a6
Move function for determining the system default locale to `babel.core`, and make it available as a class method on `Locale`.
cmlenz
parents:
38
diff
changeset
|
28 from babel.util import UTC |
1 | 29 |
30 __all__ = ['format_date', 'format_datetime', 'format_time', 'parse_date', | |
31 'parse_datetime', 'parse_time'] | |
32 __docformat__ = 'restructuredtext en' | |
33 | |
72
e0bb7dce49ea
More explicit module-level function names in `babel.core`. Added `Locale.negotiate` class method.
cmlenz
parents:
46
diff
changeset
|
34 LC_TIME = default_locale('LC_TIME') |
1 | 35 |
33 | 36 # Aliases for use in scopes where the modules are shadowed by local variables |
37 date_ = date | |
38 datetime_ = datetime | |
39 time_ = time | |
40 | |
1 | 41 def get_period_names(locale=LC_TIME): |
42 """Return the names for day periods (AM/PM) used by the locale. | |
43 | |
44 >>> get_period_names(locale='en_US')['am'] | |
45 u'AM' | |
46 | |
47 :param locale: the `Locale` object, or a locale string | |
48 :return: the dictionary of period names | |
49 :rtype: `dict` | |
50 """ | |
51 return Locale.parse(locale).periods | |
52 | |
53 def get_day_names(width='wide', context='format', locale=LC_TIME): | |
54 """Return the day names used by the locale for the specified format. | |
55 | |
56 >>> get_day_names('wide', locale='en_US')[1] | |
15 | 57 u'Tuesday' |
1 | 58 >>> get_day_names('abbreviated', locale='es')[1] |
15 | 59 u'mar' |
1 | 60 >>> get_day_names('narrow', context='stand-alone', locale='de_DE')[1] |
15 | 61 u'D' |
1 | 62 |
63 :param width: the width to use, one of "wide", "abbreviated", or "narrow" | |
64 :param context: the context, either "format" or "stand-alone" | |
65 :param locale: the `Locale` object, or a locale string | |
66 :return: the dictionary of day names | |
67 :rtype: `dict` | |
68 """ | |
69 return Locale.parse(locale).days[context][width] | |
70 | |
71 def get_month_names(width='wide', context='format', locale=LC_TIME): | |
72 """Return the month names used by the locale for the specified format. | |
73 | |
74 >>> get_month_names('wide', locale='en_US')[1] | |
75 u'January' | |
76 >>> get_month_names('abbreviated', locale='es')[1] | |
77 u'ene' | |
78 >>> get_month_names('narrow', context='stand-alone', locale='de_DE')[1] | |
79 u'J' | |
80 | |
81 :param width: the width to use, one of "wide", "abbreviated", or "narrow" | |
82 :param context: the context, either "format" or "stand-alone" | |
83 :param locale: the `Locale` object, or a locale string | |
84 :return: the dictionary of month names | |
85 :rtype: `dict` | |
86 """ | |
87 return Locale.parse(locale).months[context][width] | |
88 | |
89 def get_quarter_names(width='wide', context='format', locale=LC_TIME): | |
90 """Return the quarter names used by the locale for the specified format. | |
91 | |
92 >>> get_quarter_names('wide', locale='en_US')[1] | |
93 u'1st quarter' | |
94 >>> get_quarter_names('abbreviated', locale='de_DE')[1] | |
95 u'Q1' | |
96 | |
97 :param width: the width to use, one of "wide", "abbreviated", or "narrow" | |
98 :param context: the context, either "format" or "stand-alone" | |
99 :param locale: the `Locale` object, or a locale string | |
100 :return: the dictionary of quarter names | |
101 :rtype: `dict` | |
102 """ | |
103 return Locale.parse(locale).quarters[context][width] | |
104 | |
105 def get_era_names(width='wide', locale=LC_TIME): | |
106 """Return the era names used by the locale for the specified format. | |
107 | |
108 >>> get_era_names('wide', locale='en_US')[1] | |
109 u'Anno Domini' | |
110 >>> get_era_names('abbreviated', locale='de_DE')[1] | |
111 u'n. Chr.' | |
112 | |
113 :param width: the width to use, either "wide" or "abbreviated" | |
114 :param locale: the `Locale` object, or a locale string | |
115 :return: the dictionary of era names | |
116 :rtype: `dict` | |
117 """ | |
118 return Locale.parse(locale).eras[width] | |
119 | |
120 def get_date_format(format='medium', locale=LC_TIME): | |
121 """Return the date formatting patterns used by the locale for the specified | |
122 format. | |
123 | |
124 >>> get_date_format(locale='en_US') | |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
8
diff
changeset
|
125 <DateTimePattern u'MMM d, yyyy'> |
1 | 126 >>> get_date_format('full', locale='de_DE') |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
8
diff
changeset
|
127 <DateTimePattern u'EEEE, d. MMMM yyyy'> |
1 | 128 |
129 :param format: the format to use, one of "full", "long", "medium", or | |
130 "short" | |
131 :param locale: the `Locale` object, or a locale string | |
132 :return: the date format pattern | |
33 | 133 :rtype: `DateTimePattern` |
1 | 134 """ |
135 return Locale.parse(locale).date_formats[format] | |
136 | |
33 | 137 def get_datetime_format(format='medium', locale=LC_TIME): |
138 """Return the datetime formatting patterns used by the locale for the | |
139 specified format. | |
140 | |
141 >>> get_datetime_format(locale='en_US') | |
142 u'{1} {0}' | |
143 | |
144 :param format: the format to use, one of "full", "long", "medium", or | |
145 "short" | |
146 :param locale: the `Locale` object, or a locale string | |
147 :return: the datetime format pattern | |
148 :rtype: `unicode` | |
149 """ | |
150 patterns = Locale.parse(locale).datetime_formats | |
151 if format not in patterns: | |
152 format = None | |
153 return patterns[format] | |
154 | |
1 | 155 def get_time_format(format='medium', locale=LC_TIME): |
156 """Return the time formatting patterns used by the locale for the specified | |
157 format. | |
158 | |
159 >>> get_time_format(locale='en_US') | |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
8
diff
changeset
|
160 <DateTimePattern u'h:mm:ss a'> |
1 | 161 >>> get_time_format('full', locale='de_DE') |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
8
diff
changeset
|
162 <DateTimePattern u"H:mm' Uhr 'z"> |
1 | 163 |
164 :param format: the format to use, one of "full", "long", "medium", or | |
165 "short" | |
166 :param locale: the `Locale` object, or a locale string | |
167 :return: the time format pattern | |
33 | 168 :rtype: `DateTimePattern` |
1 | 169 """ |
170 return Locale.parse(locale).time_formats[format] | |
171 | |
33 | 172 def format_date(date=None, format='medium', locale=LC_TIME): |
1 | 173 """Returns a date formatted according to the given pattern. |
174 | |
175 >>> d = date(2007, 04, 01) | |
176 >>> format_date(d, locale='en_US') | |
177 u'Apr 1, 2007' | |
178 >>> format_date(d, format='full', locale='de_DE') | |
179 u'Sonntag, 1. April 2007' | |
180 | |
16 | 181 If you don't want to use the locale default formats, you can specify a |
182 custom date pattern: | |
183 | |
29 | 184 >>> format_date(d, "EEE, MMM d, ''yy", locale='en') |
16 | 185 u"Sun, Apr 1, '07" |
186 | |
33 | 187 :param date: the ``date`` or ``datetime`` object; if `None`, the current |
188 date is used | |
16 | 189 :param format: one of "full", "long", "medium", or "short", or a custom |
190 date/time pattern | |
19
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
18
diff
changeset
|
191 :param locale: a `Locale` object or a locale identifier |
1 | 192 :rtype: `unicode` |
19
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
18
diff
changeset
|
193 |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
18
diff
changeset
|
194 :note: If the pattern contains time fields, an `AttributeError` will be |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
18
diff
changeset
|
195 raised when trying to apply the formatting. This is also true if |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
18
diff
changeset
|
196 the value of ``date`` parameter is actually a ``datetime`` object, |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
18
diff
changeset
|
197 as this function automatically converts that to a ``date``. |
1 | 198 """ |
33 | 199 if date is None: |
200 date = date_.today() | |
201 elif isinstance(date, datetime): | |
18
990909fdf98b
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
16
diff
changeset
|
202 date = date.date() |
34 | 203 |
1 | 204 locale = Locale.parse(locale) |
205 if format in ('full', 'long', 'medium', 'short'): | |
206 format = get_date_format(format, locale=locale) | |
207 pattern = parse_pattern(format) | |
208 return parse_pattern(format).apply(date, locale) | |
209 | |
34 | 210 def format_datetime(datetime=None, format='medium', tzinfo=None, |
211 locale=LC_TIME): | |
1 | 212 """Returns a date formatted according to the given pattern. |
213 | |
33 | 214 >>> dt = datetime(2007, 04, 01, 15, 30) |
215 >>> format_datetime(dt, locale='en_US') | |
216 u'Apr 1, 2007 3:30:00 PM' | |
217 | |
34 | 218 For any pattern requiring the display of the time-zone, the third-party |
219 ``pytz`` package is needed to explicitly specify the time-zone: | |
220 | |
221 >>> from pytz import timezone | |
222 >>> format_datetime(dt, 'full', tzinfo=timezone('Europe/Berlin'), | |
223 ... locale='de_DE') | |
224 u'Sonntag, 1. April 2007 17:30 Uhr MESZ' | |
225 >>> format_datetime(dt, "yyyy.MM.dd G 'at' HH:mm:ss zzz", | |
226 ... tzinfo=timezone('US/Eastern'), locale='en') | |
227 u'2007.04.01 AD at 11:30:00 EDT' | |
228 | |
33 | 229 :param datetime: the `datetime` object; if `None`, the current date and |
230 time is used | |
16 | 231 :param format: one of "full", "long", "medium", or "short", or a custom |
232 date/time pattern | |
29 | 233 :param tzinfo: the timezone to apply to the time for display |
19
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
18
diff
changeset
|
234 :param locale: a `Locale` object or a locale identifier |
1 | 235 :rtype: `unicode` |
236 """ | |
33 | 237 if datetime is None: |
238 datetime = datetime_.now() | |
34 | 239 elif isinstance(datetime, (int, long)): |
240 datetime = datetime.fromtimestamp(datetime) | |
241 elif isinstance(datetime, time): | |
242 datetime = datetime_.combine(date.today(), datetime) | |
243 if datetime.tzinfo is None: | |
244 datetime = datetime.replace(tzinfo=UTC) | |
245 if tzinfo is not None: | |
246 datetime = datetime.astimezone(tzinfo) | |
247 if hasattr(tzinfo, 'normalize'): | |
248 datetime = tzinfo.normalize(datetime) | |
249 | |
18
990909fdf98b
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
16
diff
changeset
|
250 locale = Locale.parse(locale) |
990909fdf98b
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
16
diff
changeset
|
251 if format in ('full', 'long', 'medium', 'short'): |
33 | 252 return get_datetime_format(format, locale=locale) \ |
34 | 253 .replace('{0}', format_time(datetime, format, tzinfo=None, |
33 | 254 locale=locale)) \ |
255 .replace('{1}', format_date(datetime, format, locale=locale)) | |
256 else: | |
257 return parse_pattern(format).apply(datetime, locale) | |
1 | 258 |
34 | 259 def format_time(time=None, format='medium', tzinfo=None, locale=LC_TIME): |
1 | 260 """Returns a time formatted according to the given pattern. |
261 | |
262 >>> t = time(15, 30) | |
263 >>> format_time(t, locale='en_US') | |
264 u'3:30:00 PM' | |
265 >>> format_time(t, format='short', locale='de_DE') | |
266 u'15:30' | |
267 | |
16 | 268 If you don't want to use the locale default formats, you can specify a |
269 custom time pattern: | |
270 | |
271 >>> format_time(t, "hh 'o''clock' a", locale='en') | |
272 u"03 o'clock PM" | |
273 | |
29 | 274 For any pattern requiring the display of the time-zone, the third-party |
275 ``pytz`` package is needed to explicitly specify the time-zone: | |
276 | |
34 | 277 >>> from pytz import timezone, utc |
278 >>> t = time(15, 30, tzinfo=utc) | |
279 >>> format_time(t, format='full', tzinfo=timezone('Europe/Berlin'), | |
280 ... locale='de_DE') | |
281 u'17:30 Uhr MESZ' | |
282 >>> format_time(t, "hh 'o''clock' a, zzzz", tzinfo=timezone('US/Eastern'), | |
283 ... locale='en') | |
284 u"11 o'clock AM, Eastern Daylight Time" | |
29 | 285 |
33 | 286 :param time: the ``time`` or ``datetime`` object; if `None`, the current |
287 time is used | |
16 | 288 :param format: one of "full", "long", "medium", or "short", or a custom |
289 date/time pattern | |
29 | 290 :param tzinfo: the time-zone to apply to the time for display |
19
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
18
diff
changeset
|
291 :param locale: a `Locale` object or a locale identifier |
1 | 292 :rtype: `unicode` |
19
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
18
diff
changeset
|
293 |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
18
diff
changeset
|
294 :note: If the pattern contains date fields, an `AttributeError` will be |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
18
diff
changeset
|
295 raised when trying to apply the formatting. This is also true if |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
18
diff
changeset
|
296 the value of ``time`` parameter is actually a ``datetime`` object, |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
18
diff
changeset
|
297 as this function automatically converts that to a ``time``. |
1 | 298 """ |
33 | 299 if time is None: |
300 time = datetime.now().time() | |
301 elif isinstance(time, (int, long)): | |
18
990909fdf98b
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
16
diff
changeset
|
302 time = datetime.fromtimestamp(time).time() |
990909fdf98b
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
16
diff
changeset
|
303 elif isinstance(time, datetime): |
34 | 304 time = time.timetz() |
29 | 305 if time.tzinfo is None: |
34 | 306 time = time.replace(tzinfo=UTC) |
307 if tzinfo is not None: | |
308 dt = datetime.combine(date.today(), time).astimezone(tzinfo) | |
309 if hasattr(tzinfo, 'normalize'): | |
310 dt = tzinfo.normalize(dt) | |
311 time = dt.timetz() | |
312 | |
1 | 313 locale = Locale.parse(locale) |
314 if format in ('full', 'long', 'medium', 'short'): | |
315 format = get_time_format(format, locale=locale) | |
316 return parse_pattern(format).apply(time, locale) | |
317 | |
318 def parse_date(string, locale=LC_TIME): | |
38
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
319 """Parse a date from a string. |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
320 |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
321 This function uses the date format for the locale as a hint to determine |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
322 the order in which the date fields appear in the string. |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
323 |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
324 >>> parse_date('4/1/04', locale='en_US') |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
325 datetime.date(2004, 4, 1) |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
326 >>> parse_date('01.04.2004', locale='de_DE') |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
327 datetime.date(2004, 4, 1) |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
328 |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
329 :param string: the string containing the date |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
330 :param locale: a `Locale` object or a locale identifier |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
331 :return: the parsed date |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
332 :rtype: `date` |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
333 """ |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
334 # TODO: try ISO format first? |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
335 format = get_date_format(locale=locale).pattern.lower() |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
336 year_idx = format.index('y') |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
337 month_idx = format.index('m') |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
338 if month_idx < 0: |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
339 month_idx = format.index('l') |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
340 day_idx = format.index('d') |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
341 |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
342 indexes = [(year_idx, 'Y'), (month_idx, 'M'), (day_idx, 'D')] |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
343 indexes.sort() |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
344 indexes = dict([(item[1], idx) for idx, item in enumerate(indexes)]) |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
345 |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
346 # FIXME: this currently only supports numbers, but should also support month |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
347 # names, both in the requested locale, and english |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
348 |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
349 numbers = re.findall('(\d+)', string) |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
350 year = numbers[indexes['Y']] |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
351 if len(year) == 2: |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
352 year = 2000 + int(year) |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
353 else: |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
354 year = int(year) |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
355 month = int(numbers[indexes['M']]) |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
356 day = int(numbers[indexes['D']]) |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
357 if month > 12: |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
358 month, day = day, month |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
359 return date(year, month, day) |
1 | 360 |
361 def parse_datetime(string, locale=LC_TIME): | |
46 | 362 """Parse a date and time from a string. |
363 | |
364 This function uses the date and time formats for the locale as a hint to | |
365 determine the order in which the time fields appear in the string. | |
366 | |
367 :param string: the string containing the date and time | |
368 :param locale: a `Locale` object or a locale identifier | |
369 :return: the parsed date/time | |
370 :rtype: `datetime` | |
371 """ | |
1 | 372 raise NotImplementedError |
373 | |
374 def parse_time(string, locale=LC_TIME): | |
46 | 375 """Parse a time from a string. |
38
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
376 |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
377 This function uses the time format for the locale as a hint to determine |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
378 the order in which the time fields appear in the string. |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
379 |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
380 >>> parse_time('15:30:00', locale='en_US') |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
381 datetime.time(15, 30) |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
382 |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
383 :param string: the string containing the time |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
384 :param locale: a `Locale` object or a locale identifier |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
385 :return: the parsed time |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
386 :rtype: `time` |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
387 """ |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
388 # TODO: try ISO format first? |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
389 format = get_time_format(locale=locale).pattern.lower() |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
390 hour_idx = format.index('h') |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
391 if hour_idx < 0: |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
392 hour_idx = format.index('k') |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
393 min_idx = format.index('m') |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
394 sec_idx = format.index('s') |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
395 |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
396 indexes = [(hour_idx, 'H'), (min_idx, 'M'), (sec_idx, 'S')] |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
397 indexes.sort() |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
398 indexes = dict([(item[1], idx) for idx, item in enumerate(indexes)]) |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
399 |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
400 # FIXME: support 12 hour clock, and 0-based hour specification |
46 | 401 # and seconds should be optional, maybe minutes too |
402 # oh, and time-zones, of course | |
38
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
403 |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
404 numbers = re.findall('(\d+)', string) |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
405 hour = int(numbers[indexes['H']]) |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
406 minute = int(numbers[indexes['M']]) |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
407 second = int(numbers[indexes['S']]) |
7ae4722af473
Started implementation of datetime parsing, using a very basic approach for now.
cmlenz
parents:
34
diff
changeset
|
408 return time(hour, minute, second) |
1 | 409 |
410 | |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
8
diff
changeset
|
411 class DateTimePattern(object): |
1 | 412 |
413 def __init__(self, pattern, format): | |
414 self.pattern = pattern | |
415 self.format = format | |
416 | |
417 def __repr__(self): | |
418 return '<%s %r>' % (type(self).__name__, self.pattern) | |
419 | |
420 def __unicode__(self): | |
421 return self.pattern | |
422 | |
423 def __mod__(self, other): | |
424 assert type(other) is DateTimeFormat | |
425 return self.format % other | |
426 | |
427 def apply(self, datetime, locale): | |
428 return self % DateTimeFormat(datetime, locale) | |
429 | |
430 | |
431 class DateTimeFormat(object): | |
432 | |
433 def __init__(self, value, locale): | |
434 assert isinstance(value, (date, datetime, time)) | |
29 | 435 if isinstance(value, (datetime, time)) and value.tzinfo is None: |
436 value = value.replace(tzinfo=UTC) | |
1 | 437 self.value = value |
438 self.locale = Locale.parse(locale) | |
439 | |
440 def __getitem__(self, name): | |
441 # TODO: a number of fields missing here | |
15 | 442 char = name[0] |
443 num = len(name) | |
444 if char == 'G': | |
445 return self.format_era(char, num) | |
446 elif char in ('y', 'Y'): | |
447 return self.format_year(char, num) | |
448 elif char in ('Q', 'q'): | |
449 return self.format_quarter(char, num) | |
450 elif char in ('M', 'L'): | |
451 return self.format_month(char, num) | |
452 elif char == 'd': | |
453 return self.format(self.value.day, num) | |
454 elif char in ('E', 'e', 'c'): | |
455 return self.format_weekday(char, num) | |
456 elif char == 'a': | |
457 return self.format_period(char) | |
458 elif char == 'h': | |
459 return self.format(self.value.hour % 12, num) | |
460 elif char == 'H': | |
461 return self.format(self.value.hour, num) | |
18
990909fdf98b
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
16
diff
changeset
|
462 elif char == 'K': |
990909fdf98b
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
16
diff
changeset
|
463 return self.format(self.value.hour % 12 - 1, num) |
990909fdf98b
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
16
diff
changeset
|
464 elif char == 'k': |
990909fdf98b
Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents:
16
diff
changeset
|
465 return self.format(self.value.hour + 1, num) |
15 | 466 elif char == 'm': |
467 return self.format(self.value.minute, num) | |
468 elif char == 's': | |
469 return self.format(self.value.second, num) | |
29 | 470 elif char in ('z', 'Z', 'v'): |
471 return self.format_timezone(char, num) | |
1 | 472 else: |
15 | 473 raise KeyError('Unsupported date/time field %r' % char) |
1 | 474 |
15 | 475 def format_era(self, char, num): |
1 | 476 width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[max(3, num)] |
477 era = int(self.value.year >= 0) | |
478 return get_era_names(width, self.locale)[era] | |
479 | |
15 | 480 def format_year(self, char, num): |
481 if char.islower(): | |
482 value = self.value.year | |
483 else: | |
484 value = self.value.isocalendar()[0] | |
1 | 485 year = self.format(value, num) |
486 if num == 2: | |
487 year = year[-2:] | |
488 return year | |
489 | |
15 | 490 def format_month(self, char, num): |
1 | 491 if num <= 2: |
492 return ('%%0%dd' % num) % self.value.month | |
493 width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[num] | |
15 | 494 context = {3: 'format', 4: 'format', 5: 'stand-alone'}[num] |
1 | 495 return get_month_names(width, context, self.locale)[self.value.month] |
496 | |
15 | 497 def format_weekday(self, char, num): |
498 if num < 3: | |
499 if char.islower(): | |
500 value = 7 - self.locale.first_week_day + self.value.weekday() | |
501 return self.format(value % 7 + 1, num) | |
502 num = 3 | |
503 weekday = self.value.weekday() | |
504 width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[num] | |
505 context = {3: 'format', 4: 'format', 5: 'stand-alone'}[num] | |
1 | 506 return get_day_names(width, context, self.locale)[weekday] |
507 | |
15 | 508 def format_period(self, char): |
1 | 509 period = {0: 'am', 1: 'pm'}[int(self.value.hour > 12)] |
510 return get_period_names(locale=self.locale)[period] | |
511 | |
29 | 512 def format_timezone(self, char, num): |
513 if char == 'z': | |
34 | 514 if hasattr(self.value.tzinfo, 'zone'): |
515 zone = self.value.tzinfo.zone | |
29 | 516 else: |
34 | 517 zone = self.value.tzinfo.tzname(self.value) |
518 | |
519 # Get the canonical time-zone code | |
520 zone = self.locale.zone_aliases.get(zone, zone) | |
521 | |
522 # Try explicitly translated zone names first | |
523 display = self.locale.time_zones.get(zone) | |
524 if display: | |
525 if 'long' in display: | |
526 width = {3: 'short', 4: 'long'}[max(3, num)] | |
527 dst = self.value.dst() and 'daylight' or 'standard' | |
528 return display[width][dst] | |
529 elif 'city' in display: | |
530 return display['city'] | |
531 | |
532 else: | |
533 return zone.split('/', 1)[1] | |
29 | 534 |
535 elif char == 'Z': | |
536 offset = self.value.utcoffset() | |
34 | 537 seconds = offset.days * 24 * 60 * 60 + offset.seconds |
538 hours, seconds = divmod(seconds, 3600) | |
539 pattern = {3: '%+03d%02d', 4: 'GMT %+03d:%02d'}[max(3, num)] | |
540 return pattern % (hours, seconds // 60) | |
29 | 541 |
542 elif char == 'v': | |
543 raise NotImplementedError | |
544 | |
1 | 545 def format(self, value, length): |
546 return ('%%0%dd' % length) % value | |
547 | |
548 | |
549 PATTERN_CHARS = { | |
15 | 550 'G': [1, 2, 3, 4, 5], # era |
551 'y': None, 'Y': None, 'u': None, # year | |
552 'Q': [1, 2, 3, 4], 'q': [1, 2, 3, 4], # quarter | |
553 'M': [1, 2, 3, 4, 5], 'L': [1, 2, 3, 4, 5], # month | |
554 'w': [1, 2], 'W': [1], # week | |
555 'd': [1, 2], 'D': [1, 2, 3], 'F': [1], 'g': None, # day | |
556 'E': [1, 2, 3, 4, 5], 'e': [1, 2, 3, 4, 5], 'c': [1, 3, 4, 5], # week day | |
557 'a': [1], # period | |
558 'h': [1, 2], 'H': [1, 2], 'K': [1, 2], 'k': [1, 2], # hour | |
559 'm': [1, 2], # minute | |
560 's': [1, 2], 'S': None, 'A': None, # second | |
561 'z': [1, 2, 3, 4], 'Z': [1, 2, 3, 4], 'v': [1, 4] # zone | |
1 | 562 } |
563 | |
564 def parse_pattern(pattern): | |
565 """Parse date, time, and datetime format patterns. | |
566 | |
567 >>> parse_pattern("MMMMd").format | |
568 u'%(MMMM)s%(d)s' | |
569 >>> parse_pattern("MMM d, yyyy").format | |
570 u'%(MMM)s %(d)s, %(yyyy)s' | |
16 | 571 |
572 Pattern can contain literal strings in single quotes: | |
573 | |
1 | 574 >>> parse_pattern("H:mm' Uhr 'z").format |
575 u'%(H)s:%(mm)s Uhr %(z)s' | |
576 | |
16 | 577 An actual single quote can be used by using two adjacent single quote |
578 characters: | |
579 | |
580 >>> parse_pattern("hh' o''clock'").format | |
581 u"%(hh)s o'clock" | |
582 | |
1 | 583 :param pattern: the formatting pattern to parse |
584 """ | |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
8
diff
changeset
|
585 if type(pattern) is DateTimePattern: |
1 | 586 return pattern |
587 | |
588 result = [] | |
589 quotebuf = None | |
590 charbuf = [] | |
591 fieldchar = [''] | |
592 fieldnum = [0] | |
593 | |
594 def append_chars(): | |
595 result.append(''.join(charbuf).replace('%', '%%')) | |
596 del charbuf[:] | |
597 | |
598 def append_field(): | |
599 limit = PATTERN_CHARS[fieldchar[0]] | |
15 | 600 if limit and fieldnum[0] not in limit: |
1 | 601 raise ValueError('Invalid length for field: %r' |
602 % (fieldchar[0] * fieldnum[0])) | |
603 result.append('%%(%s)s' % (fieldchar[0] * fieldnum[0])) | |
604 fieldchar[0] = '' | |
605 fieldnum[0] = 0 | |
606 | |
16 | 607 for idx, char in enumerate(pattern.replace("''", '\0')): |
1 | 608 if quotebuf is None: |
609 if char == "'": # quote started | |
610 if fieldchar[0]: | |
611 append_field() | |
612 elif charbuf: | |
613 append_chars() | |
614 quotebuf = [] | |
615 elif char in PATTERN_CHARS: | |
616 if charbuf: | |
617 append_chars() | |
618 if char == fieldchar[0]: | |
619 fieldnum[0] += 1 | |
620 else: | |
621 if fieldchar[0]: | |
622 append_field() | |
623 fieldchar[0] = char | |
624 fieldnum[0] = 1 | |
625 else: | |
626 if fieldchar[0]: | |
627 append_field() | |
628 charbuf.append(char) | |
629 | |
630 elif quotebuf is not None: | |
16 | 631 if char == "'": # end of quote |
1 | 632 charbuf.extend(quotebuf) |
633 quotebuf = None | |
634 else: # inside quote | |
635 quotebuf.append(char) | |
636 | |
637 if fieldchar[0]: | |
638 append_field() | |
639 elif charbuf: | |
640 append_chars() | |
641 | |
16 | 642 return DateTimePattern(pattern, u''.join(result).replace('\0', "'")) |