Mercurial > babel > old > mirror
annotate 0.9.x/babel/tests/dates.py @ 513:3a00f8293d47 stable
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
author | fschwarz |
---|---|
date | Fri, 04 Mar 2011 22:34:14 +0000 |
parents | 26733b2c995d |
children |
rev | line source |
---|---|
263 | 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 from datetime import date, datetime, time | |
15 import doctest | |
513
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
16 import new |
263 | 17 import unittest |
18 | |
19 from pytz import timezone | |
20 | |
21 from babel import dates | |
513
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
22 from babel.util import FixedOffsetTimezone |
263 | 23 |
24 | |
25 class DateTimeFormatTestCase(unittest.TestCase): | |
26 | |
399 | 27 def test_quarter_format(self): |
28 d = date(2006, 6, 8) | |
29 fmt = dates.DateTimeFormat(d, locale='en_US') | |
30 self.assertEqual('2', fmt['Q']) | |
31 self.assertEqual('2nd quarter', fmt['QQQQ']) | |
32 d = date(2006, 12, 31) | |
33 fmt = dates.DateTimeFormat(d, locale='en_US') | |
34 self.assertEqual('Q4', fmt['QQQ']) | |
35 | |
348
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
278
diff
changeset
|
36 def test_month_context(self): |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
278
diff
changeset
|
37 d = date(2006, 1, 8) |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
278
diff
changeset
|
38 fmt = dates.DateTimeFormat(d, locale='cs_CZ') |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
278
diff
changeset
|
39 self.assertEqual('1', fmt['MMM']) |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
278
diff
changeset
|
40 fmt = dates.DateTimeFormat(d, locale='cs_CZ') |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
278
diff
changeset
|
41 self.assertEqual('1.', fmt['LLL']) |
05975a0e7021
Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents:
278
diff
changeset
|
42 |
381 | 43 def test_abbreviated_month_alias(self): |
44 d = date(2006, 3, 8) | |
45 fmt = dates.DateTimeFormat(d, locale='de_DE') | |
46 self.assertEqual(u'Mär', fmt['LLL']) | |
47 | |
263 | 48 def test_week_of_year_first(self): |
49 d = date(2006, 1, 8) | |
50 fmt = dates.DateTimeFormat(d, locale='de_DE') | |
51 self.assertEqual('1', fmt['w']) | |
52 fmt = dates.DateTimeFormat(d, locale='en_US') | |
53 self.assertEqual('02', fmt['ww']) | |
54 | |
55 def test_week_of_year_first_with_year(self): | |
56 d = date(2006, 1, 1) | |
57 fmt = dates.DateTimeFormat(d, locale='de_DE') | |
58 self.assertEqual('52', fmt['w']) | |
59 self.assertEqual('2005', fmt['YYYY']) | |
60 | |
61 def test_week_of_year_last(self): | |
62 d = date(2005, 12, 26) | |
63 fmt = dates.DateTimeFormat(d, locale='de_DE') | |
64 self.assertEqual('52', fmt['w']) | |
65 fmt = dates.DateTimeFormat(d, locale='en_US') | |
66 self.assertEqual('53', fmt['ww']) | |
67 | |
68 def test_week_of_month_first(self): | |
69 d = date(2006, 1, 8) | |
70 fmt = dates.DateTimeFormat(d, locale='de_DE') | |
71 self.assertEqual('1', fmt['W']) | |
72 fmt = dates.DateTimeFormat(d, locale='en_US') | |
73 self.assertEqual('2', fmt['W']) | |
74 | |
75 def test_week_of_month_last(self): | |
76 d = date(2006, 1, 29) | |
77 fmt = dates.DateTimeFormat(d, locale='de_DE') | |
78 self.assertEqual('4', fmt['W']) | |
79 fmt = dates.DateTimeFormat(d, locale='en_US') | |
80 self.assertEqual('5', fmt['W']) | |
81 | |
82 def test_day_of_year(self): | |
83 d = date(2007, 4, 1) | |
84 fmt = dates.DateTimeFormat(d, locale='en_US') | |
85 self.assertEqual('91', fmt['D']) | |
86 | |
87 def test_day_of_year_first(self): | |
88 d = date(2007, 1, 1) | |
89 fmt = dates.DateTimeFormat(d, locale='en_US') | |
90 self.assertEqual('001', fmt['DDD']) | |
91 | |
92 def test_day_of_year_last(self): | |
93 d = date(2007, 12, 31) | |
94 fmt = dates.DateTimeFormat(d, locale='en_US') | |
95 self.assertEqual('365', fmt['DDD']) | |
96 | |
97 def test_day_of_week_in_month(self): | |
98 d = date(2007, 4, 15) | |
99 fmt = dates.DateTimeFormat(d, locale='en_US') | |
100 self.assertEqual('3', fmt['F']) | |
101 | |
102 def test_day_of_week_in_month_first(self): | |
103 d = date(2007, 4, 1) | |
104 fmt = dates.DateTimeFormat(d, locale='en_US') | |
105 self.assertEqual('1', fmt['F']) | |
106 | |
107 def test_day_of_week_in_month_last(self): | |
108 d = date(2007, 4, 29) | |
109 fmt = dates.DateTimeFormat(d, locale='en_US') | |
110 self.assertEqual('5', fmt['F']) | |
111 | |
112 def test_local_day_of_week(self): | |
113 d = date(2007, 4, 1) # a sunday | |
114 fmt = dates.DateTimeFormat(d, locale='de_DE') | |
115 self.assertEqual('7', fmt['e']) # monday is first day of week | |
116 fmt = dates.DateTimeFormat(d, locale='en_US') | |
117 self.assertEqual('01', fmt['ee']) # sunday is first day of week | |
118 fmt = dates.DateTimeFormat(d, locale='dv_MV') | |
119 self.assertEqual('03', fmt['ee']) # friday is first day of week | |
120 | |
121 d = date(2007, 4, 2) # a monday | |
122 fmt = dates.DateTimeFormat(d, locale='de_DE') | |
123 self.assertEqual('1', fmt['e']) # monday is first day of week | |
124 fmt = dates.DateTimeFormat(d, locale='en_US') | |
125 self.assertEqual('02', fmt['ee']) # sunday is first day of week | |
126 fmt = dates.DateTimeFormat(d, locale='dv_MV') | |
127 self.assertEqual('04', fmt['ee']) # friday is first day of week | |
128 | |
129 def test_local_day_of_week_standalone(self): | |
130 d = date(2007, 4, 1) # a sunday | |
131 fmt = dates.DateTimeFormat(d, locale='de_DE') | |
132 self.assertEqual('7', fmt['c']) # monday is first day of week | |
133 fmt = dates.DateTimeFormat(d, locale='en_US') | |
134 self.assertEqual('1', fmt['c']) # sunday is first day of week | |
135 fmt = dates.DateTimeFormat(d, locale='dv_MV') | |
136 self.assertEqual('3', fmt['c']) # friday is first day of week | |
137 | |
138 d = date(2007, 4, 2) # a monday | |
139 fmt = dates.DateTimeFormat(d, locale='de_DE') | |
140 self.assertEqual('1', fmt['c']) # monday is first day of week | |
141 fmt = dates.DateTimeFormat(d, locale='en_US') | |
142 self.assertEqual('2', fmt['c']) # sunday is first day of week | |
143 fmt = dates.DateTimeFormat(d, locale='dv_MV') | |
144 self.assertEqual('4', fmt['c']) # friday is first day of week | |
145 | |
146 def test_fractional_seconds(self): | |
147 t = time(15, 30, 12, 34567) | |
148 fmt = dates.DateTimeFormat(t, locale='en_US') | |
149 self.assertEqual('3457', fmt['SSSS']) | |
150 | |
151 def test_fractional_seconds_zero(self): | |
152 t = time(15, 30, 0) | |
153 fmt = dates.DateTimeFormat(t, locale='en_US') | |
154 self.assertEqual('0000', fmt['SSSS']) | |
155 | |
156 def test_milliseconds_in_day(self): | |
157 t = time(15, 30, 12, 345000) | |
158 fmt = dates.DateTimeFormat(t, locale='en_US') | |
159 self.assertEqual('55812345', fmt['AAAA']) | |
160 | |
161 def test_milliseconds_in_day_zero(self): | |
162 d = time(0, 0, 0) | |
163 fmt = dates.DateTimeFormat(d, locale='en_US') | |
164 self.assertEqual('0000', fmt['AAAA']) | |
165 | |
166 def test_timezone_rfc822(self): | |
167 tz = timezone('Europe/Berlin') | |
168 t = time(15, 30, tzinfo=tz) | |
169 fmt = dates.DateTimeFormat(t, locale='de_DE') | |
170 self.assertEqual('+0100', fmt['Z']) | |
171 | |
172 def test_timezone_gmt(self): | |
173 tz = timezone('Europe/Berlin') | |
174 t = time(15, 30, tzinfo=tz) | |
175 fmt = dates.DateTimeFormat(t, locale='de_DE') | |
176 self.assertEqual('GMT+01:00', fmt['ZZZZ']) | |
177 | |
178 def test_timezone_no_uncommon(self): | |
179 tz = timezone('Europe/Paris') | |
180 dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz) | |
181 fmt = dates.DateTimeFormat(dt, locale='fr_CA') | |
182 self.assertEqual('France', fmt['v']) | |
183 | |
184 def test_timezone_with_uncommon(self): | |
185 tz = timezone('Europe/Paris') | |
186 dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz) | |
187 fmt = dates.DateTimeFormat(dt, locale='fr_CA') | |
188 self.assertEqual('HEC', fmt['V']) | |
189 | |
190 def test_timezone_location_format(self): | |
191 tz = timezone('Europe/Paris') | |
192 dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz) | |
193 fmt = dates.DateTimeFormat(dt, locale='fr_FR') | |
194 self.assertEqual('France', fmt['VVVV']) | |
195 | |
196 def test_timezone_walltime_short(self): | |
197 tz = timezone('Europe/Paris') | |
198 t = time(15, 30, tzinfo=tz) | |
199 fmt = dates.DateTimeFormat(t, locale='fr_FR') | |
200 self.assertEqual('HEC', fmt['v']) | |
201 | |
202 def test_timezone_walltime_long(self): | |
203 tz = timezone('Europe/Paris') | |
204 t = time(15, 30, tzinfo=tz) | |
205 fmt = dates.DateTimeFormat(t, locale='fr_FR') | |
381 | 206 self.assertEqual(u'heure d’Europe centrale', fmt['vvvv']) |
263 | 207 |
278 | 208 def test_hour_formatting(self): |
209 l = 'en_US' | |
210 t = time(0, 0, 0) | |
211 self.assertEqual(dates.format_time(t, 'h a', locale=l), '12 AM') | |
212 self.assertEqual(dates.format_time(t, 'H', locale=l), '0') | |
213 self.assertEqual(dates.format_time(t, 'k', locale=l), '24') | |
214 self.assertEqual(dates.format_time(t, 'K a', locale=l), '0 AM') | |
215 t = time(12, 0, 0) | |
216 self.assertEqual(dates.format_time(t, 'h a', locale=l), '12 PM') | |
217 self.assertEqual(dates.format_time(t, 'H', locale=l), '12') | |
218 self.assertEqual(dates.format_time(t, 'k', locale=l), '12') | |
219 self.assertEqual(dates.format_time(t, 'K a', locale=l), '0 PM') | |
220 | |
263 | 221 |
222 class FormatDateTestCase(unittest.TestCase): | |
223 | |
224 def test_with_time_fields_in_pattern(self): | |
225 self.assertRaises(AttributeError, dates.format_date, date(2007, 04, 01), | |
226 "yyyy-MM-dd HH:mm", locale='en_US') | |
227 | |
228 def test_with_time_fields_in_pattern_and_datetime_param(self): | |
229 self.assertRaises(AttributeError, dates.format_date, | |
230 datetime(2007, 04, 01, 15, 30), | |
231 "yyyy-MM-dd HH:mm", locale='en_US') | |
232 | |
233 | |
234 class FormatTimeTestCase(unittest.TestCase): | |
235 | |
351 | 236 def test_with_naive_datetime_and_tzinfo(self): |
237 string = dates.format_time(datetime(2007, 4, 1, 15, 30), | |
238 'long', tzinfo=timezone('US/Eastern'), | |
239 locale='en') | |
240 self.assertEqual('11:30:00 AM EDT', string) | |
241 | |
263 | 242 def test_with_date_fields_in_pattern(self): |
243 self.assertRaises(AttributeError, dates.format_time, date(2007, 04, 01), | |
244 "yyyy-MM-dd HH:mm", locale='en_US') | |
245 | |
246 def test_with_date_fields_in_pattern_and_datetime_param(self): | |
247 self.assertRaises(AttributeError, dates.format_time, | |
248 datetime(2007, 04, 01, 15, 30), | |
249 "yyyy-MM-dd HH:mm", locale='en_US') | |
250 | |
251 | |
513
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
252 class TimeZoneAdjustTestCase(unittest.TestCase): |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
253 def _utc(self): |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
254 UTC = FixedOffsetTimezone(0, 'UTC') |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
255 def fake_localize(self, dt, is_dst=False): |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
256 raise NotImplementedError() |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
257 UTC.localize = new.instancemethod(fake_localize, UTC, UTC.__class__) |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
258 # This is important to trigger the actual bug (#257) |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
259 self.assertEqual(False, hasattr(UTC, 'normalize')) |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
260 return UTC |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
261 |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
262 def test_can_format_time_with_non_pytz_timezone(self): |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
263 # regression test for #257 |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
264 utc = self._utc() |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
265 t = datetime(2007, 4, 1, 15, 30, tzinfo=utc) |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
266 formatted_time = dates.format_time(t, 'long', tzinfo=utc, locale='en') |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
267 self.assertEqual('3:30:00 PM +0000', formatted_time) |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
268 |
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
269 |
263 | 270 def suite(): |
271 suite = unittest.TestSuite() | |
272 suite.addTest(doctest.DocTestSuite(dates)) | |
273 suite.addTest(unittest.makeSuite(DateTimeFormatTestCase)) | |
274 suite.addTest(unittest.makeSuite(FormatDateTestCase)) | |
275 suite.addTest(unittest.makeSuite(FormatTimeTestCase)) | |
513
3a00f8293d47
Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents:
399
diff
changeset
|
276 suite.addTest(unittest.makeSuite(TimeZoneAdjustTestCase)) |
263 | 277 return suite |
278 | |
279 if __name__ == '__main__': | |
280 unittest.main(defaultTest='suite') |