annotate babel/dates.py @ 29:da1c9610e751

More work on timezones.
author cmlenz
date Mon, 04 Jun 2007 10:51:38 +0000
parents c0c92d11f1ab
children 0740b6d31799
rev   line source
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
2 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2007 Edgewall Software
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
4 # All rights reserved.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
5 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
9 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
13
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
14 """Locale dependent formatting and parsing of dates and times.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
15
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
16 The default locale for the functions in this module is determined by the
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
17 following environment variables, in that order:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
18
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
19 * ``LC_TIME``,
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
20 * ``LC_ALL``, and
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
21 * ``LANG``
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
22 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
23
29
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
24 from datetime import date, datetime, time, timedelta, tzinfo
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
25
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
26 from babel.core import Locale
29
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
27 from babel.util import default_locale, UTC
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
28
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
29 __all__ = ['format_date', 'format_datetime', 'format_time', 'parse_date',
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
30 'parse_datetime', 'parse_time']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
31 __docformat__ = 'restructuredtext en'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
32
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
33 LC_TIME = default_locale('LC_TIME')
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
34
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
35 def get_period_names(locale=LC_TIME):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
36 """Return the names for day periods (AM/PM) used by the locale.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
37
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
38 >>> get_period_names(locale='en_US')['am']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
39 u'AM'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
40
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
41 :param locale: the `Locale` object, or a locale string
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
42 :return: the dictionary of period names
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
43 :rtype: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
44 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
45 return Locale.parse(locale).periods
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
46
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
47 def get_day_names(width='wide', context='format', locale=LC_TIME):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
48 """Return the day names used by the locale for the specified format.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
49
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
50 >>> get_day_names('wide', locale='en_US')[1]
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
51 u'Tuesday'
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
52 >>> get_day_names('abbreviated', locale='es')[1]
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
53 u'mar'
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
54 >>> get_day_names('narrow', context='stand-alone', locale='de_DE')[1]
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
55 u'D'
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
56
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
57 :param width: the width to use, one of "wide", "abbreviated", or "narrow"
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
58 :param context: the context, either "format" or "stand-alone"
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
59 :param locale: the `Locale` object, or a locale string
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
60 :return: the dictionary of day names
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
61 :rtype: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
62 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
63 return Locale.parse(locale).days[context][width]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
64
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
65 def get_month_names(width='wide', context='format', locale=LC_TIME):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
66 """Return the month names used by the locale for the specified format.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
67
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
68 >>> get_month_names('wide', locale='en_US')[1]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
69 u'January'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
70 >>> get_month_names('abbreviated', locale='es')[1]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
71 u'ene'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
72 >>> get_month_names('narrow', context='stand-alone', locale='de_DE')[1]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
73 u'J'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
74
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
75 :param width: the width to use, one of "wide", "abbreviated", or "narrow"
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
76 :param context: the context, either "format" or "stand-alone"
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
77 :param locale: the `Locale` object, or a locale string
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
78 :return: the dictionary of month names
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
79 :rtype: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
80 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
81 return Locale.parse(locale).months[context][width]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
82
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
83 def get_quarter_names(width='wide', context='format', locale=LC_TIME):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
84 """Return the quarter names used by the locale for the specified format.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
85
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
86 >>> get_quarter_names('wide', locale='en_US')[1]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
87 u'1st quarter'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
88 >>> get_quarter_names('abbreviated', locale='de_DE')[1]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
89 u'Q1'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
90
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
91 :param width: the width to use, one of "wide", "abbreviated", or "narrow"
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
92 :param context: the context, either "format" or "stand-alone"
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
93 :param locale: the `Locale` object, or a locale string
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
94 :return: the dictionary of quarter names
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
95 :rtype: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
96 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
97 return Locale.parse(locale).quarters[context][width]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
98
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
99 def get_era_names(width='wide', locale=LC_TIME):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
100 """Return the era names used by the locale for the specified format.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
101
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
102 >>> get_era_names('wide', locale='en_US')[1]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
103 u'Anno Domini'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
104 >>> get_era_names('abbreviated', locale='de_DE')[1]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
105 u'n. Chr.'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
106
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
107 :param width: the width to use, either "wide" or "abbreviated"
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
108 :param locale: the `Locale` object, or a locale string
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
109 :return: the dictionary of era names
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
110 :rtype: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
111 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
112 return Locale.parse(locale).eras[width]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
113
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
114 def get_date_format(format='medium', locale=LC_TIME):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
115 """Return the date formatting patterns used by the locale for the specified
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
116 format.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
117
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
118 >>> get_date_format(locale='en_US')
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 8
diff changeset
119 <DateTimePattern u'MMM d, yyyy'>
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
120 >>> get_date_format('full', locale='de_DE')
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 8
diff changeset
121 <DateTimePattern u'EEEE, d. MMMM yyyy'>
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
122
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
123 :param format: the format to use, one of "full", "long", "medium", or
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
124 "short"
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
125 :param locale: the `Locale` object, or a locale string
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
126 :return: the date format pattern
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
127 :rtype: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
128 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
129 return Locale.parse(locale).date_formats[format]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
130
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
131 def get_time_format(format='medium', locale=LC_TIME):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
132 """Return the time formatting patterns used by the locale for the specified
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
133 format.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
134
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
135 >>> get_time_format(locale='en_US')
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 8
diff changeset
136 <DateTimePattern u'h:mm:ss a'>
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
137 >>> get_time_format('full', locale='de_DE')
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 8
diff changeset
138 <DateTimePattern u"H:mm' Uhr 'z">
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
139
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
140 :param format: the format to use, one of "full", "long", "medium", or
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
141 "short"
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
142 :param locale: the `Locale` object, or a locale string
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
143 :return: the time format pattern
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
144 :rtype: `dict`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
145 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
146 return Locale.parse(locale).time_formats[format]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
147
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
148 def format_date(date, format='medium', locale=LC_TIME):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
149 """Returns a date formatted according to the given pattern.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
150
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
151 >>> d = date(2007, 04, 01)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
152 >>> format_date(d, locale='en_US')
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
153 u'Apr 1, 2007'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
154 >>> format_date(d, format='full', locale='de_DE')
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
155 u'Sonntag, 1. April 2007'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
156
16
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
157 If you don't want to use the locale default formats, you can specify a
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
158 custom date pattern:
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
159
29
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
160 >>> format_date(d, "EEE, MMM d, ''yy", locale='en')
16
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
161 u"Sun, Apr 1, '07"
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
162
19
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 18
diff changeset
163 :param date: the ``date`` or ``datetime`` object
16
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
164 :param format: one of "full", "long", "medium", or "short", or a custom
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
165 date/time pattern
19
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 18
diff changeset
166 :param locale: a `Locale` object or a locale identifier
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
167 :rtype: `unicode`
19
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 18
diff changeset
168
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 18
diff changeset
169 :note: If the pattern contains time fields, an `AttributeError` will be
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 18
diff changeset
170 raised when trying to apply the formatting. This is also true if
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 18
diff changeset
171 the value of ``date`` parameter is actually a ``datetime`` object,
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 18
diff changeset
172 as this function automatically converts that to a ``date``.
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
173 """
18
77a68f88f6bc Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents: 16
diff changeset
174 if isinstance(date, datetime):
77a68f88f6bc Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents: 16
diff changeset
175 date = date.date()
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
176 locale = Locale.parse(locale)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
177 if format in ('full', 'long', 'medium', 'short'):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
178 format = get_date_format(format, locale=locale)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
179 pattern = parse_pattern(format)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
180 return parse_pattern(format).apply(date, locale)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
181
29
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
182 def format_datetime(datetime, format='medium', tzinfo=UTC, locale=LC_TIME):
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
183 """Returns a date formatted according to the given pattern.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
184
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
185 :param datetime: the ``date`` object
16
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
186 :param format: one of "full", "long", "medium", or "short", or a custom
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
187 date/time pattern
29
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
188 :param tzinfo: the timezone to apply to the time for display
19
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 18
diff changeset
189 :param locale: a `Locale` object or a locale identifier
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
190 :rtype: `unicode`
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
191 """
18
77a68f88f6bc Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents: 16
diff changeset
192 locale = Locale.parse(locale)
77a68f88f6bc Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents: 16
diff changeset
193 if format in ('full', 'long', 'medium', 'short'):
77a68f88f6bc Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents: 16
diff changeset
194 raise NotImplementedError
77a68f88f6bc Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents: 16
diff changeset
195 pattern = parse_pattern(format)
77a68f88f6bc Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents: 16
diff changeset
196 return parse_pattern(format).apply(datetime, locale)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
197
29
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
198 def format_time(time, format='medium', tzinfo=UTC, locale=LC_TIME):
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
199 """Returns a time formatted according to the given pattern.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
200
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
201 >>> t = time(15, 30)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
202 >>> format_time(t, locale='en_US')
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
203 u'3:30:00 PM'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
204 >>> format_time(t, format='short', locale='de_DE')
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
205 u'15:30'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
206
16
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
207 If you don't want to use the locale default formats, you can specify a
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
208 custom time pattern:
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
209
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
210 >>> format_time(t, "hh 'o''clock' a", locale='en')
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
211 u"03 o'clock PM"
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
212
29
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
213 For any pattern requiring the display of the time-zone, the third-party
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
214 ``pytz`` package is needed to explicitly specify the time-zone:
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
215
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
216 >>> from pytz import timezone
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
217 >>> cet = timezone('Europe/Berlin')
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
218 >>> format_time(t, format='full', tzinfo=cet, locale='de_DE')
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
219 u'15:30 Uhr MEZ'
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
220
19
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 18
diff changeset
221 :param time: the ``time`` or ``datetime`` object
16
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
222 :param format: one of "full", "long", "medium", or "short", or a custom
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
223 date/time pattern
29
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
224 :param tzinfo: the time-zone to apply to the time for display
19
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 18
diff changeset
225 :param locale: a `Locale` object or a locale identifier
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
226 :rtype: `unicode`
19
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 18
diff changeset
227
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 18
diff changeset
228 :note: If the pattern contains date fields, an `AttributeError` will be
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 18
diff changeset
229 raised when trying to apply the formatting. This is also true if
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 18
diff changeset
230 the value of ``time`` parameter is actually a ``datetime`` object,
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 18
diff changeset
231 as this function automatically converts that to a ``time``.
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
232 """
18
77a68f88f6bc Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents: 16
diff changeset
233 if isinstance(time, (int, long)):
77a68f88f6bc Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents: 16
diff changeset
234 time = datetime.fromtimestamp(time).time()
77a68f88f6bc Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents: 16
diff changeset
235 elif isinstance(time, datetime):
77a68f88f6bc Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents: 16
diff changeset
236 time = time.time()
29
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
237 if time.tzinfo is None:
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
238 time = time.replace(tzinfo=tzinfo)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
239 locale = Locale.parse(locale)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
240 if format in ('full', 'long', 'medium', 'short'):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
241 format = get_time_format(format, locale=locale)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
242 return parse_pattern(format).apply(time, locale)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
243
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
244 def parse_date(string, locale=LC_TIME):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
245 raise NotImplementedError
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
246
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
247 def parse_datetime(string, locale=LC_TIME):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
248 raise NotImplementedError
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
249
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
250 def parse_time(string, locale=LC_TIME):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
251 raise NotImplementedError
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
252
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
253
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 8
diff changeset
254 class DateTimePattern(object):
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
255
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
256 def __init__(self, pattern, format):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
257 self.pattern = pattern
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
258 self.format = format
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
259
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
260 def __repr__(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
261 return '<%s %r>' % (type(self).__name__, self.pattern)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
262
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
263 def __unicode__(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
264 return self.pattern
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
265
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
266 def __mod__(self, other):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
267 assert type(other) is DateTimeFormat
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
268 return self.format % other
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
269
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
270 def apply(self, datetime, locale):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
271 return self % DateTimeFormat(datetime, locale)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
272
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
273
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
274 class DateTimeFormat(object):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
275
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
276 def __init__(self, value, locale):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
277 assert isinstance(value, (date, datetime, time))
29
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
278 if isinstance(value, (datetime, time)) and value.tzinfo is None:
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
279 value = value.replace(tzinfo=UTC)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
280 self.value = value
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
281 self.locale = Locale.parse(locale)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
282
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
283 def __getitem__(self, name):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
284 # TODO: a number of fields missing here
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
285 char = name[0]
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
286 num = len(name)
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
287 if char == 'G':
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
288 return self.format_era(char, num)
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
289 elif char in ('y', 'Y'):
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
290 return self.format_year(char, num)
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
291 elif char in ('Q', 'q'):
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
292 return self.format_quarter(char, num)
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
293 elif char in ('M', 'L'):
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
294 return self.format_month(char, num)
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
295 elif char == 'd':
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
296 return self.format(self.value.day, num)
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
297 elif char in ('E', 'e', 'c'):
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
298 return self.format_weekday(char, num)
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
299 elif char == 'a':
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
300 return self.format_period(char)
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
301 elif char == 'h':
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
302 return self.format(self.value.hour % 12, num)
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
303 elif char == 'H':
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
304 return self.format(self.value.hour, num)
18
77a68f88f6bc Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents: 16
diff changeset
305 elif char == 'K':
77a68f88f6bc Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents: 16
diff changeset
306 return self.format(self.value.hour % 12 - 1, num)
77a68f88f6bc Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents: 16
diff changeset
307 elif char == 'k':
77a68f88f6bc Started documentation for date formatting, plus some code tweaks in that area.
cmlenz
parents: 16
diff changeset
308 return self.format(self.value.hour + 1, num)
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
309 elif char == 'm':
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
310 return self.format(self.value.minute, num)
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
311 elif char == 's':
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
312 return self.format(self.value.second, num)
29
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
313 elif char in ('z', 'Z', 'v'):
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
314 return self.format_timezone(char, num)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
315 else:
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
316 raise KeyError('Unsupported date/time field %r' % char)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
317
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
318 def format_era(self, char, num):
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
319 width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[max(3, num)]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
320 era = int(self.value.year >= 0)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
321 return get_era_names(width, self.locale)[era]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
322
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
323 def format_year(self, char, num):
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
324 if char.islower():
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
325 value = self.value.year
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
326 else:
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
327 value = self.value.isocalendar()[0]
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
328 year = self.format(value, num)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
329 if num == 2:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
330 year = year[-2:]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
331 return year
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
332
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
333 def format_month(self, char, num):
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
334 if num <= 2:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
335 return ('%%0%dd' % num) % self.value.month
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
336 width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[num]
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
337 context = {3: 'format', 4: 'format', 5: 'stand-alone'}[num]
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
338 return get_month_names(width, context, self.locale)[self.value.month]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
339
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
340 def format_weekday(self, char, num):
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
341 if num < 3:
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
342 if char.islower():
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
343 value = 7 - self.locale.first_week_day + self.value.weekday()
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
344 return self.format(value % 7 + 1, num)
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
345 num = 3
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
346 weekday = self.value.weekday()
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
347 width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[num]
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
348 context = {3: 'format', 4: 'format', 5: 'stand-alone'}[num]
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
349 return get_day_names(width, context, self.locale)[weekday]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
350
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
351 def format_period(self, char):
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
352 period = {0: 'am', 1: 'pm'}[int(self.value.hour > 12)]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
353 return get_period_names(locale=self.locale)[period]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
354
29
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
355 def format_timezone(self, char, num):
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
356 if char == 'z':
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
357 zone = self.value.tzinfo.zone
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
358 if num < 4:
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
359 return self.locale.time_zones[zone]['short'][
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
360 self.value.dst() and 'daylight' or 'standard'
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
361 ]
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
362 else:
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
363 return self.locale.time_zones[zone]['long'][
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
364 self.value.dst() and 'daylight' or 'standard'
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
365 ]
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
366
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
367 elif char == 'Z':
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
368 offset = self.value.utcoffset()
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
369 hours, seconds = divmod(offset.seconds, 3600)
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
370 minutes = seconds // 60
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
371 sign = '+'
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
372 if offset.seconds < 0:
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
373 sign = '-'
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
374 pattern = {3: '%s%02d%02d', 4: 'GMT %s%02d:%02d'}[max(3, num)]
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
375 return pattern % (sign, hours, minutes)
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
376
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
377 elif char == 'v':
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
378 raise NotImplementedError
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
379
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
380 def format(self, value, length):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
381 return ('%%0%dd' % length) % value
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
382
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
383
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
384 PATTERN_CHARS = {
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
385 'G': [1, 2, 3, 4, 5], # era
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
386 'y': None, 'Y': None, 'u': None, # year
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
387 'Q': [1, 2, 3, 4], 'q': [1, 2, 3, 4], # quarter
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
388 'M': [1, 2, 3, 4, 5], 'L': [1, 2, 3, 4, 5], # month
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
389 'w': [1, 2], 'W': [1], # week
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
390 'd': [1, 2], 'D': [1, 2, 3], 'F': [1], 'g': None, # day
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
391 'E': [1, 2, 3, 4, 5], 'e': [1, 2, 3, 4, 5], 'c': [1, 3, 4, 5], # week day
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
392 'a': [1], # period
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
393 'h': [1, 2], 'H': [1, 2], 'K': [1, 2], 'k': [1, 2], # hour
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
394 'm': [1, 2], # minute
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
395 's': [1, 2], 'S': None, 'A': None, # second
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
396 'z': [1, 2, 3, 4], 'Z': [1, 2, 3, 4], 'v': [1, 4] # zone
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
397 }
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
398
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
399 def parse_pattern(pattern):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
400 """Parse date, time, and datetime format patterns.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
401
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
402 >>> parse_pattern("MMMMd").format
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
403 u'%(MMMM)s%(d)s'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
404 >>> parse_pattern("MMM d, yyyy").format
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
405 u'%(MMM)s %(d)s, %(yyyy)s'
16
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
406
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
407 Pattern can contain literal strings in single quotes:
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
408
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
409 >>> parse_pattern("H:mm' Uhr 'z").format
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
410 u'%(H)s:%(mm)s Uhr %(z)s'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
411
16
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
412 An actual single quote can be used by using two adjacent single quote
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
413 characters:
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
414
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
415 >>> parse_pattern("hh' o''clock'").format
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
416 u"%(hh)s o'clock"
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
417
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
418 :param pattern: the formatting pattern to parse
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
419 """
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 8
diff changeset
420 if type(pattern) is DateTimePattern:
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
421 return pattern
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
422
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
423 result = []
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
424 quotebuf = None
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
425 charbuf = []
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
426 fieldchar = ['']
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
427 fieldnum = [0]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
428
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
429 def append_chars():
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
430 result.append(''.join(charbuf).replace('%', '%%'))
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
431 del charbuf[:]
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
432
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
433 def append_field():
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
434 limit = PATTERN_CHARS[fieldchar[0]]
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
435 if limit and fieldnum[0] not in limit:
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
436 raise ValueError('Invalid length for field: %r'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
437 % (fieldchar[0] * fieldnum[0]))
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
438 result.append('%%(%s)s' % (fieldchar[0] * fieldnum[0]))
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
439 fieldchar[0] = ''
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
440 fieldnum[0] = 0
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
441
16
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
442 for idx, char in enumerate(pattern.replace("''", '\0')):
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
443 if quotebuf is None:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
444 if char == "'": # quote started
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
445 if fieldchar[0]:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
446 append_field()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
447 elif charbuf:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
448 append_chars()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
449 quotebuf = []
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
450 elif char in PATTERN_CHARS:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
451 if charbuf:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
452 append_chars()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
453 if char == fieldchar[0]:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
454 fieldnum[0] += 1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
455 else:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
456 if fieldchar[0]:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
457 append_field()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
458 fieldchar[0] = char
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
459 fieldnum[0] = 1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
460 else:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
461 if fieldchar[0]:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
462 append_field()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
463 charbuf.append(char)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
464
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
465 elif quotebuf is not None:
16
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
466 if char == "'": # end of quote
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
467 charbuf.extend(quotebuf)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
468 quotebuf = None
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
469 else: # inside quote
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
470 quotebuf.append(char)
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
471
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
472 if fieldchar[0]:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
473 append_field()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
474 elif charbuf:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
475 append_chars()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
476
16
ed154241c08d Handle escape chars in datetime patterns.
cmlenz
parents: 15
diff changeset
477 return DateTimePattern(pattern, u''.join(result).replace('\0', "'"))
Copyright (C) 2012-2017 Edgewall Software