Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 604 lines (540 sloc) 27.952 kB
0407f90 catch `IOError` from pytz when timezone is unknown and monkey-patch a…
mgood authored
1 # -*- coding: utf-8 -*-
2 #
33456d6 Merged [7889] from 0.11-stable.
jonas authored
3 # Copyright (C) 2007-2009 Edgewall Software
0407f90 catch `IOError` from pytz when timezone is unknown and monkey-patch a…
mgood authored
4 # Copyright (C) 2007 Matt Good <trac@matt-good.net>
5 # All rights reserved.
6 #
7 # This software is licensed as described in the file COPYING, which
8 # you should have received as part of this distribution. The terms
9 # are also available at http://trac.edgewall.org/wiki/TracLicense.
10 #
11 # This software consists of voluntary contributions made by many
12 # individuals. For the exact contribution history, see the revision
13 # history and logs, available at http://trac.edgewall.org/log/.
14 #
15 # Author: Matt Good <trac@matt-good.net>
16
17 import datetime
b3033f8 Skip on unit-test that can't work on some versions of Windows (follow…
cboos authored
18 import os
0407f90 catch `IOError` from pytz when timezone is unknown and monkey-patch a…
mgood authored
19 import unittest
20
51ce973 Adds i18n datetime feature, configurable datetime format and parse in…
jomae authored
21 from trac.core import TracError
6c41f98 Provide a `to_datetime` utility function which can be used to convert…
cboos authored
22 from trac.util import datefmt
0407f90 catch `IOError` from pytz when timezone is unknown and monkey-patch a…
mgood authored
23
24 try:
25 import pytz
26 except ImportError:
b955604 @jun66j5 Normalize time across DST boundary
authored
27 pytz = None
28 try:
29 from babel import Locale
30 except ImportError:
31 Locale = None
32
33 if pytz is None:
0407f90 catch `IOError` from pytz when timezone is unknown and monkey-patch a…
mgood authored
34 PytzTestCase = None
35 else:
36 class PytzTestCase(unittest.TestCase):
37 def test_pytz_conversion(self):
6c41f98 Provide a `to_datetime` utility function which can be used to convert…
cboos authored
38 tz = datefmt.get_timezone('GMT +3:00')
0407f90 catch `IOError` from pytz when timezone is unknown and monkey-patch a…
mgood authored
39 self.assertEqual(datetime.timedelta(hours=3),
40 tz.utcoffset(None))
41
3465ddd replace POSIX timezones in pytz with ISO ones from Trac (fixes #5021)
mgood authored
42 def test_posix_conversion(self):
6c41f98 Provide a `to_datetime` utility function which can be used to convert…
cboos authored
43 tz = datefmt.get_timezone('Etc/GMT-4')
3465ddd replace POSIX timezones in pytz with ISO ones from Trac (fixes #5021)
mgood authored
44 self.assertEqual(datetime.timedelta(hours=4),
45 tz.utcoffset(None))
46 self.assertEqual('GMT +4:00', tz.zone)
47
c4aa5aa In pytz 2007, pytz.timezone can't deal with unicode input, so we norm…
cboos authored
48 def test_unicode_input(self):
6c41f98 Provide a `to_datetime` utility function which can be used to convert…
cboos authored
49 tz = datefmt.get_timezone(u'Etc/GMT-4')
c4aa5aa In pytz 2007, pytz.timezone can't deal with unicode input, so we norm…
cboos authored
50 self.assertEqual(datetime.timedelta(hours=4),
51 tz.utcoffset(None))
52 self.assertEqual('GMT +4:00', tz.zone)
0407f90 catch `IOError` from pytz when timezone is unknown and monkey-patch a…
mgood authored
53
01a49d5 0.12dev: Merged [8831] from 0.11-stable.
rblank authored
54 def test_parse_date(self):
55 tz = datefmt.get_timezone('Europe/Zurich')
56 t = datefmt.parse_date('2009-12-01T12:00:00', tz)
57 t_utc = datetime.datetime(2009, 12, 1, 11, 0, 0, 0, datefmt.utc)
58 self.assertEqual(t_utc, t)
59
60 def test_parse_date_dst(self):
61 tz = datefmt.get_timezone('Europe/Zurich')
62 t = datefmt.parse_date('2009-08-01T12:00:00', tz)
63 t_utc = datetime.datetime(2009, 8, 1, 10, 0, 0, 0, datefmt.utc)
64 self.assertEqual(t_utc, t)
65
b955604 @jun66j5 Normalize time across DST boundary
authored
66 def test_parse_date_across_dst_boundary(self):
67 tz = datefmt.get_timezone('Europe/Zurich')
68 # DST start - 31 March, 02:00
69 format = '%Y-%m-%d %H:%M:%S %Z%z'
70 expected = '2002-03-31 03:30:00 CEST+0200'
71 # iso8601
72 t = datefmt.parse_date('2002-03-31T02:30:00', tz)
73 self.assertEqual(expected, t.strftime(format))
74 # strptime
75 t = datetime.datetime(2002, 3, 31, 2, 30)
76 t = datefmt.parse_date(t.strftime('%x %X'), tz)
77 self.assertEqual(expected, t.strftime(format))
78 # i18n datetime
79 if Locale:
80 en_US = Locale.parse('en_US')
81 t = datefmt.parse_date('Mar 31, 2002 02:30', tz, en_US)
82 self.assertEqual(expected, t.strftime(format))
83
cc5535c @jun66j5 Normalize time across DST boundary in `to_datetime` with pytz timezone.
authored
84 def test_to_datetime_pytz_normalize(self):
85 tz = datefmt.get_timezone('Europe/Zurich')
86 date = datefmt.to_datetime(datetime.date(2002, 3, 31), tz)
87 format = '%Y-%m-%d %H:%M:%S %Z%z'
88 expected = '2002-03-31 00:00:00 CET+0100'
89 self.assertEqual(expected, date.strftime(format))
01a49d5 0.12dev: Merged [8831] from 0.11-stable.
rblank authored
90
6c41f98 Provide a `to_datetime` utility function which can be used to convert…
cboos authored
91 class DateFormatTestCase(unittest.TestCase):
92
93 def test_to_datetime(self):
f8b54ac Fix `test_to_datetime` unit-test. Thanks to ecarter for noticing this…
cboos authored
94 expected = datetime.datetime.fromtimestamp(23, datefmt.localtz)
6c41f98 Provide a `to_datetime` utility function which can be used to convert…
cboos authored
95 self.assertEqual(datefmt.to_datetime(23), expected)
96 self.assertEqual(datefmt.to_datetime(23L), expected)
97 self.assertEqual(datefmt.to_datetime(23.0), expected)
34b3487 util: Catch microsecond timestamps passed to `to_datetime()` and conv…
rblank authored
98
99 def test_to_datetime_microsecond_timestamps(self):
100 expected = datetime.datetime.fromtimestamp(2345.678912,
101 datefmt.localtz)
102 self.assertEqual(datefmt.to_datetime(2345678912), expected)
103 self.assertEqual(datefmt.to_datetime(2345678912L), expected)
104 self.assertEqual(datefmt.to_datetime(2345678912.0), expected)
105
b3033f8 Skip on unit-test that can't work on some versions of Windows (follow…
cboos authored
106 def test_to_datetime_microsecond_negative_timestamps(self):
3b2a4d1 util: Work around [http://bugs.python.org/issue1646728 Python issue 1…
rblank authored
107 # Work around issue1646728 in Python 2.4
108 expected = datetime.datetime.fromtimestamp(-2345, datefmt.localtz) \
109 - datetime.timedelta(seconds=.678912)
110
111 self.assertEqual(datefmt.to_datetime(-2345678912).microsecond,
112 321088) # 1000000 - 678912
34b3487 util: Catch microsecond timestamps passed to `to_datetime()` and conv…
rblank authored
113 self.assertEqual(datefmt.to_datetime(-2345678912), expected)
114 self.assertEqual(datefmt.to_datetime(-2345678912L), expected)
115 self.assertEqual(datefmt.to_datetime(-2345678912.0), expected)
b3033f8 Skip on unit-test that can't work on some versions of Windows (follow…
cboos authored
116 if os.name == 'nt':
117 del test_to_datetime_microsecond_negative_timestamps
118 # negative timestamps not supported on Windows:
119 # ValueError: timestamp out of range for platform localtime()/gmtime()
e4b0c7a 0.12dev: merged r8518 and r8519 from 0.11-stable.
cboos authored
120
121 def test_to_datetime_can_convert_dates(self):
122 expected = datetime.datetime(2009, 5, 2, tzinfo=datefmt.localtz)
123 self.assertEqual(datefmt.to_datetime(expected.date()), expected)
6c41f98 Provide a `to_datetime` utility function which can be used to convert…
cboos authored
124
125 def test_to_datetime_tz(self):
126 tz = datefmt.timezone('GMT +1:00')
6eb0a24 0.12dev: Autumn cleaning using pylint:
rblank authored
127 expected = datetime.datetime(1970, 1, 1, 1, 0, 23, 0, tz)
6c41f98 Provide a `to_datetime` utility function which can be used to convert…
cboos authored
128 self.assertEqual(datefmt.to_datetime(23, tz), expected)
129 self.assertEqual(datefmt.to_datetime(23L, tz), expected)
130 self.assertEqual(datefmt.to_datetime(23.0, tz), expected)
12e0aa7 0.12dev: Merged [8857] from 0.11-stable and prevented [8856] from bei…
rblank authored
131 tz = datefmt.timezone('GMT +4:00')
132 expected = datetime.datetime(1970, 1, 1, 4, 0, 23, 0, tz)
133 self.assertEqual(datefmt.to_datetime(23, tz), expected)
134 self.assertEqual(datefmt.to_datetime(23L, tz), expected)
135 self.assertEqual(datefmt.to_datetime(23.0, tz), expected)
6c41f98 Provide a `to_datetime` utility function which can be used to convert…
cboos authored
136
12e0aa7 0.12dev: Merged [8857] from 0.11-stable and prevented [8856] from bei…
rblank authored
137 def test_format_datetime_utc(self):
6eb0a24 0.12dev: Autumn cleaning using pylint:
rblank authored
138 t = datetime.datetime(1970, 1, 1, 1, 0, 23, 0, datefmt.utc)
2c3ea86 0.12dev: merged [7819-7824] from 0.11-stable
cboos authored
139 expected = '1970-01-01T01:00:23Z'
140 self.assertEqual(datefmt.format_datetime(t, '%Y-%m-%dT%H:%M:%SZ',
6c41f98 Provide a `to_datetime` utility function which can be used to convert…
cboos authored
141 datefmt.utc), expected)
142 self.assertEqual(datefmt.format_datetime(t, 'iso8601',
143 datefmt.utc), expected)
2c3ea86 0.12dev: merged [7819-7824] from 0.11-stable
cboos authored
144 self.assertEqual(datefmt.format_datetime(t, 'iso8601date',
145 datefmt.utc),
146 expected.split('T')[0])
147 self.assertEqual(datefmt.format_datetime(t, 'iso8601time',
148 datefmt.utc),
149 expected.split('T')[1])
6c41f98 Provide a `to_datetime` utility function which can be used to convert…
cboos authored
150
12e0aa7 0.12dev: Merged [8857] from 0.11-stable and prevented [8856] from bei…
rblank authored
151 def test_format_datetime_gmt01(self):
2c3ea86 0.12dev: merged [7819-7824] from 0.11-stable
cboos authored
152 gmt01 = datefmt.FixedOffset(60, 'GMT +1:00')
6eb0a24 0.12dev: Autumn cleaning using pylint:
rblank authored
153 t = datetime.datetime(1970, 1, 1, 1, 0, 23, 0, gmt01)
36323ec 0.12dev: Made ISO 8601 timestamps RFC 3339-compliant. Also, simplifie…
rblank authored
154 self.assertEqual('1970-01-01T01:00:23+0100',
155 datefmt.format_datetime(t, '%Y-%m-%dT%H:%M:%S%z',
156 gmt01))
157 expected = '1970-01-01T01:00:23+01:00'
6c41f98 Provide a `to_datetime` utility function which can be used to convert…
cboos authored
158 self.assertEqual(datefmt.format_datetime(t, 'iso8601',
2c3ea86 0.12dev: merged [7819-7824] from 0.11-stable
cboos authored
159 gmt01), expected)
160 self.assertEqual(datefmt.format_datetime(t, 'iso8601date', gmt01),
161 expected.split('T')[0])
162 self.assertEqual(datefmt.format_datetime(t, 'iso8601time', gmt01),
163 expected.split('T')[1])
e4b0c7a 0.12dev: merged r8518 and r8519 from 0.11-stable.
cboos authored
164
165 def test_format_date_accepts_date_instances(self):
166 a_date = datetime.date(2009, 8, 20)
167 self.assertEqual('2009-08-20',
168 datefmt.format_date(a_date, format='%Y-%m-%d'))
169
34008c7 0.13dev: Display time of timeline page in the locale setting.
jomae authored
170 def test_format_compatibility(self):
171 tz = datefmt.timezone('GMT +2:00')
172 t = datetime.datetime(2010, 8, 28, 11, 45, 56, 123456, datefmt.utc)
173 tz_t = datetime.datetime(2010, 8, 28, 13, 45, 56, 123456, tz)
174
175 # Converting babel's format to strftime format
176 self.assertEqual(tz_t.strftime('%x %H:%M').decode('utf-8'),
177 datefmt.format_datetime(t, 'short', tz))
178 self.assertEqual(tz_t.strftime('%x').decode('utf-8'),
179 datefmt.format_date(t, 'short', tz))
180 self.assertEqual(tz_t.strftime('%H:%M').decode('utf-8'),
181 datefmt.format_time(t, 'short', tz))
182 for f in ('medium', 'long', 'full'):
183 self.assertEqual(tz_t.strftime('%x %X').decode('utf-8'),
184 datefmt.format_datetime(t, f, tz))
185 self.assertEqual(tz_t.strftime('%x').decode('utf-8'),
186 datefmt.format_date(t, f, tz))
187 self.assertEqual(tz_t.strftime('%X').decode('utf-8'),
188 datefmt.format_time(t, f, tz))
189
01a49d5 0.12dev: Merged [8831] from 0.11-stable.
rblank authored
190
0ead4ae db: Converted all timestamp columns for resource tables from seconds …
rblank authored
191 class UTimestampTestCase(unittest.TestCase):
192
193 def test_sub_second(self):
194 t = datetime.datetime(2001, 2, 3, 4, 5, 6, 123456, datefmt.utc)
195 ts = datefmt.to_utimestamp(t)
196 self.assertEqual(981173106123456L, ts)
197 self.assertEqual(t, datefmt.from_utimestamp(ts))
198
199
51ce973 Adds i18n datetime feature, configurable datetime format and parse in…
jomae authored
200 class ISO8601TestCase(unittest.TestCase):
986bdd3 0.13dev: Convert babel's and `"iso8601"` format in user's selecting /…
jomae authored
201 def test_default(self):
51ce973 Adds i18n datetime feature, configurable datetime format and parse in…
jomae authored
202 tz = datefmt.timezone('GMT +2:00')
203 t = datetime.datetime(2010, 8, 28, 11, 45, 56, 123456, tz)
204 self.assertEqual('2010-08-28',
205 datefmt.format_date(t, tzinfo=tz, locale='iso8601'))
206 self.assertEqual('11:45:56+02:00',
207 datefmt.format_time(t, tzinfo=tz, locale='iso8601'))
208 self.assertEqual('2010-08-28T11:45:56+02:00',
209 datefmt.format_datetime(t, tzinfo=tz,
210 locale='iso8601'))
211
986bdd3 0.13dev: Convert babel's and `"iso8601"` format in user's selecting /…
jomae authored
212 def test_with_iso8601(self):
213 tz = datefmt.timezone('GMT +2:00')
214 t = datetime.datetime(2010, 8, 28, 11, 45, 56, 123456, tz)
215 self.assertEqual('2010-08-28',
216 datefmt.format_date(t, 'iso8601', tz, 'iso8601'))
217 self.assertEqual('11:45:56+02:00',
218 datefmt.format_time(t, 'iso8601', tz, 'iso8601'))
219 self.assertEqual('2010-08-28T11:45:56+02:00',
220 datefmt.format_datetime(t, 'iso8601', tz, 'iso8601'))
221
222 def test_with_babel_format(self):
223 tz = datefmt.timezone('GMT +2:00')
224 t = datetime.datetime(2010, 8, 28, 11, 45, 56, 123456, tz)
225 for f in ('short', 'medium', 'long', 'full'):
226 self.assertEqual('2010-08-28',
227 datefmt.format_date(t, f, tz, 'iso8601'))
34008c7 0.13dev: Display time of timeline page in the locale setting.
jomae authored
228 self.assertEqual('11:45',
229 datefmt.format_time(t, 'short', tz, 'iso8601'))
230 self.assertEqual('2010-08-28T11:45',
231 datefmt.format_datetime(t, 'short', tz, 'iso8601'))
232 self.assertEqual('11:45:56',
233 datefmt.format_time(t, 'medium', tz, 'iso8601'))
234 self.assertEqual('2010-08-28T11:45:56',
235 datefmt.format_datetime(t, 'medium', tz, 'iso8601'))
236 for f in ('long', 'full'):
986bdd3 0.13dev: Convert babel's and `"iso8601"` format in user's selecting /…
jomae authored
237 self.assertEqual('11:45:56+02:00',
238 datefmt.format_time(t, f, tz, 'iso8601'))
239 self.assertEqual('2010-08-28T11:45:56+02:00',
240 datefmt.format_datetime(t, f, tz, 'iso8601'))
241
51ce973 Adds i18n datetime feature, configurable datetime format and parse in…
jomae authored
242 def test_hint(self):
243 try:
244 datefmt.parse_date('***', locale='iso8601', hint='date')
245 except TracError, e:
246 self.assert_('"YYYY-MM-DD"' in unicode(e))
247 try:
248 datefmt.parse_date('***', locale='iso8601', hint='datetime')
249 except TracError, e:
250 self.assert_(u'"YYYY-MM-DDThh:mm:ss±hh:mm"' in unicode(e))
251 try:
252 datefmt.parse_date('***', locale='iso8601', hint='foobar')
253 except TracError, e:
254 self.assert_('"foobar"' in unicode(e))
255
256
b955604 @jun66j5 Normalize time across DST boundary
authored
257 if Locale is None:
51ce973 Adds i18n datetime feature, configurable datetime format and parse in…
jomae authored
258 I18nDateFormatTestCase = None
259 else:
260 class I18nDateFormatTestCase(unittest.TestCase):
261 def test_i18n_format_datetime(self):
262 tz = datefmt.timezone('GMT +2:00')
263 t = datetime.datetime(2010, 8, 28, 11, 45, 56, 123456, datefmt.utc)
264 en_US = Locale.parse('en_US')
265 self.assertEqual('Aug 28, 2010 1:45:56 PM',
266 datefmt.format_datetime(t, tzinfo=tz,
267 locale=en_US))
268 en_GB = Locale.parse('en_GB')
269 self.assertEqual('28 Aug 2010 13:45:56',
270 datefmt.format_datetime(t, tzinfo=tz,
271 locale=en_GB))
272 fr = Locale.parse('fr')
273 self.assertEqual(u'28 août 2010 13:45:56',
274 datefmt.format_datetime(t, tzinfo=tz, locale=fr))
275 ja = Locale.parse('ja')
276 self.assertEqual(u'2010/08/28 13:45:56',
277 datefmt.format_datetime(t, tzinfo=tz, locale=ja))
278 vi = Locale.parse('vi')
279 self.assertEqual(u'13:45:56 28-08-2010',
280 datefmt.format_datetime(t, tzinfo=tz, locale=vi))
281 zh_CN = Locale.parse('zh_CN')
282 self.assertEqual(u'2010-8-28 下午01:45:56',
283 datefmt.format_datetime(t, tzinfo=tz,
284 locale=zh_CN))
285
286 def test_i18n_format_date(self):
287 tz = datefmt.timezone('GMT +2:00')
288 t = datetime.datetime(2010, 8, 7, 11, 45, 56, 123456, datefmt.utc)
289 en_US = Locale.parse('en_US')
290 self.assertEqual('Aug 7, 2010',
291 datefmt.format_date(t, tzinfo=tz, locale=en_US))
292 en_GB = Locale.parse('en_GB')
293 self.assertEqual('7 Aug 2010',
294 datefmt.format_date(t, tzinfo=tz, locale=en_GB))
295 fr = Locale.parse('fr')
296 self.assertEqual(u'7 août 2010',
297 datefmt.format_date(t, tzinfo=tz, locale=fr))
298 ja = Locale.parse('ja')
299 self.assertEqual(u'2010/08/07',
300 datefmt.format_date(t, tzinfo=tz, locale=ja))
301 vi = Locale.parse('vi')
302 self.assertEqual(u'07-08-2010',
303 datefmt.format_date(t, tzinfo=tz, locale=vi))
304 zh_CN = Locale.parse('zh_CN')
305 self.assertEqual(u'2010-8-7',
306 datefmt.format_date(t, tzinfo=tz, locale=zh_CN))
307
308 def test_i18n_format_time(self):
309 tz = datefmt.timezone('GMT +2:00')
310 t = datetime.datetime(2010, 8, 28, 11, 45, 56, 123456, datefmt.utc)
311 en_US = Locale.parse('en_US')
312 en_GB = Locale.parse('en_GB')
313 fr = Locale.parse('fr')
314 ja = Locale.parse('ja')
315 vi = Locale.parse('vi')
316 zh_CN = Locale.parse('zh_CN')
317
318 self.assertEqual('1:45:56 PM',
319 datefmt.format_time(t, tzinfo=tz, locale=en_US))
320 self.assertEqual('13:45:56',
321 datefmt.format_time(t, tzinfo=tz, locale=en_GB))
322 self.assertEqual('13:45:56',
323 datefmt.format_time(t, tzinfo=tz, locale=fr))
324 self.assertEqual('13:45:56',
325 datefmt.format_time(t, tzinfo=tz, locale=ja))
326 self.assertEqual('13:45:56',
327 datefmt.format_time(t, tzinfo=tz, locale=vi))
328 self.assertEqual(u'下午01:45:56',
329 datefmt.format_time(t, tzinfo=tz, locale=zh_CN))
330
331 def test_i18n_datetime_hint(self):
332 en_US = Locale.parse('en_US')
333 en_GB = Locale.parse('en_GB')
334 fr = Locale.parse('fr')
335 ja = Locale.parse('ja')
336 vi = Locale.parse('vi')
337 zh_CN = Locale.parse('zh_CN')
338
339 self.assertEqual('MMM d, yyyy h:mm:ss a',
340 datefmt.get_datetime_format_hint(en_US))
341 self.assertEqual('d MMM yyyy HH:mm:ss',
342 datefmt.get_datetime_format_hint(en_GB))
343 self.assertEqual('d MMM yyyy HH:mm:ss',
344 datefmt.get_datetime_format_hint(fr))
345 self.assertEqual('yyyy/MM/dd H:mm:ss',
346 datefmt.get_datetime_format_hint(ja))
347 self.assertEqual('HH:mm:ss dd-MM-yyyy',
348 datefmt.get_datetime_format_hint(vi))
349 self.assertEqual('yyyy-M-d ahh:mm:ss',
350 datefmt.get_datetime_format_hint(zh_CN))
351
352 def test_i18n_date_hint(self):
353 en_US = Locale.parse('en_US')
354 en_GB = Locale.parse('en_GB')
355 fr = Locale.parse('fr')
356 ja = Locale.parse('ja')
357 vi = Locale.parse('vi')
358 zh_CN = Locale.parse('zh_CN')
359
360 self.assertEqual('MMM d, yyyy',
361 datefmt.get_date_format_hint(en_US))
362 self.assertEqual('d MMM yyyy',
363 datefmt.get_date_format_hint(en_GB))
364 self.assertEqual('d MMM yyyy',
365 datefmt.get_date_format_hint(fr))
366 self.assertEqual('yyyy/MM/dd',
367 datefmt.get_date_format_hint(ja))
368 self.assertEqual('dd-MM-yyyy',
369 datefmt.get_date_format_hint(vi))
370 self.assertEqual('yyyy-M-d',
371 datefmt.get_date_format_hint(zh_CN))
372
373 def test_i18n_parse_date_iso8609(self):
374 tz = datefmt.timezone('GMT +2:00')
375 dt = datetime.datetime(2010, 8, 28, 13, 45, 56, 0, tz)
376 d = datetime.datetime(2010, 8, 28, 0, 0, 0, 0, tz)
377 en_US = Locale.parse('en_US')
378 vi = Locale.parse('vi')
379
380 def iso8601(expected, text, tz, locale):
381 self.assertEqual(expected,
382 datefmt.parse_date(text, tz, locale))
383
384 iso8601(dt, '2010-08-28T15:45:56+0400', tz, en_US)
385 iso8601(dt, '2010-08-28T11:45:56+0000', tz, vi)
386 iso8601(dt, '2010-08-28T11:45:56Z', tz, vi)
387 iso8601(dt, '20100828T144556+0300', tz, en_US)
388 iso8601(dt, '20100828T114556Z', tz, vi)
389
390 iso8601(d, '2010-08-28+0200', tz, en_US)
391 # iso8601(d, '2010-08-28+0000', tz, vi)
392 # iso8601(d, '2010-08-28Z', tz, en_US)
393 iso8601(d, '2010-08-28', tz, vi)
394 iso8601(d, '20100828+0200', tz, en_US)
395 # iso8601(d, '20100828Z', tz, vi)
396
397 def test_i18n_parse_date_datetime(self):
398 tz = datefmt.timezone('GMT +2:00')
399 expected = datetime.datetime(2010, 8, 28, 13, 45, 56, 0, tz)
400 expected_minute = datetime.datetime(2010, 8, 28, 13, 45, 0, 0, tz)
401 en_US = Locale.parse('en_US')
402 en_GB = Locale.parse('en_GB')
403 fr = Locale.parse('fr')
404 ja = Locale.parse('ja')
405 vi = Locale.parse('vi')
406 zh_CN = Locale.parse('zh_CN')
407
408 self.assertEqual(expected,
409 datefmt.parse_date('Aug 28, 2010 1:45:56 PM', tz,
410 en_US))
411 self.assertEqual(expected,
412 datefmt.parse_date('8 28, 2010 1:45:56 PM', tz,
413 en_US))
414 self.assertEqual(expected,
415 datefmt.parse_date('28 Aug 2010 1:45:56 PM', tz,
416 en_US))
417 self.assertEqual(expected,
418 datefmt.parse_date('28 Aug 2010 PM 1:45:56', tz,
419 en_US))
420 self.assertEqual(expected,
421 datefmt.parse_date('28 Aug 2010 13:45:56', tz,
422 en_US))
423 self.assertEqual(expected_minute,
424 datefmt.parse_date('28 Aug 2010 PM 1:45', tz,
425 en_US))
426
427 self.assertEqual(expected,
428 datefmt.parse_date('28 Aug 2010 13:45:56', tz,
429 en_GB))
430
431 self.assertEqual(expected,
432 datefmt.parse_date(u'28 août 2010 13:45:56', tz,
433 fr))
434 self.assertEqual(expected,
435 datefmt.parse_date(u'août 28 2010 13:45:56', tz,
436 fr))
437 self.assertEqual(expected_minute,
438 datefmt.parse_date(u'août 28 2010 13:45', tz,
439 fr))
440
441 self.assertEqual(expected,
442 datefmt.parse_date('2010/08/28 13:45:56', tz, ja))
443 self.assertEqual(expected_minute,
444 datefmt.parse_date('2010/08/28 13:45', tz, ja))
445
446 self.assertEqual(expected,
447 datefmt.parse_date('13:45:56 28-08-2010', tz, vi))
448 self.assertEqual(expected_minute,
449 datefmt.parse_date('13:45 28-08-2010', tz, vi))
450
451 self.assertEqual(expected,
452 datefmt.parse_date(u'2010-8-28 下午01:45:56',
453 tz, zh_CN))
454 self.assertEqual(expected,
455 datefmt.parse_date(u'2010-8-28 01:45:56下午',
456 tz, zh_CN))
457 self.assertEqual(expected_minute,
458 datefmt.parse_date(u'2010-8-28 下午01:45', tz,
459 zh_CN))
460 self.assertEqual(expected_minute,
461 datefmt.parse_date(u'2010-8-28 01:45下午', tz,
462 zh_CN))
463
a2f4830 0.13dev: Follow-up to [10571], fixed parsing time string with AM/PM m…
jomae authored
464 def test_i18n_parse_date_datetime_meridiem(self):
465 tz = datefmt.timezone('GMT +2:00')
466 expected_am = datetime.datetime(2011, 2, 22, 0, 45, 56, 0, tz)
467 expected_pm = datetime.datetime(2011, 2, 22, 12, 45, 56, 0, tz)
468 en_US = Locale.parse('en_US')
469 zh_CN = Locale.parse('zh_CN')
470
471 self.assertEqual(expected_am,
472 datefmt.parse_date('Feb 22, 2011 0:45:56 AM', tz,
473 en_US))
474 self.assertEqual(expected_am,
475 datefmt.parse_date('Feb 22, 2011 12:45:56 AM', tz,
476 en_US))
477 self.assertEqual(expected_am,
478 datefmt.parse_date(u'2011-2-22 上午0:45:56', tz,
479 zh_CN))
480 self.assertEqual(expected_am,
481 datefmt.parse_date(u'2011-2-22 上午12:45:56', tz,
482 zh_CN))
483
484 self.assertEqual(expected_pm,
485 datefmt.parse_date('Feb 22, 2011 0:45:56 PM', tz,
486 en_US))
487 self.assertEqual(expected_pm,
488 datefmt.parse_date('Feb 22, 2011 12:45:56 PM', tz,
489 en_US))
490 self.assertEqual(expected_pm,
491 datefmt.parse_date(u'2011-2-22 下午0:45:56', tz,
492 zh_CN))
493 self.assertEqual(expected_pm,
494 datefmt.parse_date(u'2011-2-22 下午12:45:56', tz,
495 zh_CN))
496
51ce973 Adds i18n datetime feature, configurable datetime format and parse in…
jomae authored
497 def test_i18n_parse_date_date(self):
498 tz = datefmt.timezone('GMT +2:00')
499 expected = datetime.datetime(2010, 8, 28, 0, 0, 0, 0, tz)
500 en_US = Locale.parse('en_US')
501 en_GB = Locale.parse('en_GB')
502 fr = Locale.parse('fr')
503 ja = Locale.parse('ja')
504 vi = Locale.parse('vi')
505 zh_CN = Locale.parse('zh_CN')
506
507 self.assertEqual(expected,
508 datefmt.parse_date('Aug 28, 2010', tz, en_US))
509 self.assertEqual(expected,
510 datefmt.parse_date('28 Aug 2010', tz, en_GB))
511 self.assertEqual(expected,
512 datefmt.parse_date(u'28 août 2010', tz, fr))
513 self.assertEqual(expected,
514 datefmt.parse_date('2010/08/28', tz, ja))
515 self.assertEqual(expected,
516 datefmt.parse_date('28-08-2010', tz, vi))
517 self.assertEqual(expected,
518 datefmt.parse_date(u'2010-8-28', tz, zh_CN))
519
520 def test_i18n_parse_date_roundtrip(self):
521 tz = datefmt.timezone('GMT +2:00')
522 t = datetime.datetime(2010, 8, 28, 11, 45, 56, 123456, datefmt.utc)
523 expected = datetime.datetime(2010, 8, 28, 13, 45, 56, 0, tz)
524
525 def roundtrip(locale):
526 locale = Locale.parse(locale)
527 formatted = datefmt.format_datetime(t, tzinfo=tz,
528 locale=locale)
529 self.assertEqual(expected,
530 datefmt.parse_date(formatted, tz, locale))
531 self.assertEqual(formatted,
532 datefmt.format_datetime(expected, tzinfo=tz,
533 locale=locale))
534
535 roundtrip('ca')
536 roundtrip('cs')
537 roundtrip('de')
538 roundtrip('el')
539 roundtrip('en_GB')
540 roundtrip('en_US')
541 roundtrip('eo')
542 roundtrip('es')
543 roundtrip('es_AR')
544 roundtrip('fa')
545 roundtrip('fi')
546 roundtrip('fr')
547 roundtrip('gl')
548 roundtrip('he')
549 roundtrip('hu')
550 roundtrip('hy')
551 roundtrip('it')
552 roundtrip('ja')
553 roundtrip('ko')
554 roundtrip('nb')
555 roundtrip('nl')
556 roundtrip('pl')
557 roundtrip('pt')
558 roundtrip('pt_BR')
559 roundtrip('ro')
560 roundtrip('ru')
561 roundtrip('sl')
562 roundtrip('sv')
563 roundtrip('tr')
564 roundtrip('vi')
565 roundtrip('zh_CN')
566 roundtrip('zh_TW')
567
568 def test_format_compatibility(self):
569 tz = datefmt.timezone('GMT +2:00')
570 t = datetime.datetime(2010, 8, 28, 11, 45, 56, 123456, datefmt.utc)
a2f4830 0.13dev: Follow-up to [10571], fixed parsing time string with AM/PM m…
jomae authored
571 tz_t = datetime.datetime(2010, 8, 28, 13, 45, 56, 123456, tz)
51ce973 Adds i18n datetime feature, configurable datetime format and parse in…
jomae authored
572 en_US = Locale.parse('en_US')
573
574 # Converting default format to babel's format
575 self.assertEqual('Aug 28, 2010 1:45:56 PM',
576 datefmt.format_datetime(t, '%x %X', tz, en_US))
577 self.assertEqual('Aug 28, 2010',
578 datefmt.format_datetime(t, '%x', tz, en_US))
579 self.assertEqual('1:45:56 PM',
580 datefmt.format_datetime(t, '%X', tz, en_US))
581 self.assertEqual('Aug 28, 2010',
582 datefmt.format_date(t, '%x', tz, en_US))
583 self.assertEqual('1:45:56 PM',
584 datefmt.format_time(t, '%X', tz, en_US))
585
586
0407f90 catch `IOError` from pytz when timezone is unknown and monkey-patch a…
mgood authored
587 def suite():
588 suite = unittest.TestSuite()
589 if PytzTestCase:
590 suite.addTest(unittest.makeSuite(PytzTestCase, 'test'))
c4aa5aa In pytz 2007, pytz.timezone can't deal with unicode input, so we norm…
cboos authored
591 else:
592 print "SKIP: utils/tests/datefmt.py (no pytz installed)"
6c41f98 Provide a `to_datetime` utility function which can be used to convert…
cboos authored
593 suite.addTest(unittest.makeSuite(DateFormatTestCase))
0ead4ae db: Converted all timestamp columns for resource tables from seconds …
rblank authored
594 suite.addTest(unittest.makeSuite(UTimestampTestCase))
51ce973 Adds i18n datetime feature, configurable datetime format and parse in…
jomae authored
595 suite.addTest(unittest.makeSuite(ISO8601TestCase))
596 if I18nDateFormatTestCase:
597 suite.addTest(unittest.makeSuite(I18nDateFormatTestCase, 'test'))
598 else:
599 print "SKIP: utils/tests/datefmt.py (no babel installed)"
0407f90 catch `IOError` from pytz when timezone is unknown and monkey-patch a…
mgood authored
600 return suite
601
602 if __name__ == '__main__':
603 unittest.main(defaultTest='suite')
Something went wrong with that request. Please try again.