Mercurial > babel > mirror
annotate babel/tests/dates.py @ 577:e57abed6542a trunk
fix format_date() with a datetime parameter (#282)
author | fschwarz |
---|---|
date | Mon, 30 Jul 2012 21:48:55 +0000 |
parents | 33153e2550a8 |
children | 5ba68e40d1d0 |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
530 | 3 # Copyright (C) 2007-2011 Edgewall Software |
1 | 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 | |
519
045782926b0c
fix expected output from !FormatTimedeltaTestCase in babel.tests.dates and ensure it will be executed during the normal test run (see #259)
fschwarz
parents:
512
diff
changeset
|
14 from datetime import date, datetime, time, timedelta |
1 | 15 import doctest |
512 | 16 import new |
1 | 17 import unittest |
18 | |
29 | 19 from pytz import timezone |
20 | |
1 | 21 from babel import dates |
512 | 22 from babel.util import FixedOffsetTimezone |
1 | 23 |
15 | 24 |
25 class DateTimeFormatTestCase(unittest.TestCase): | |
26 | |
396 | 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 | |
344
eb12741b65df
Fix for incorrect month context lookup in date formatting. Closes #75. Thanks to Andrew Stromnov for reporting the problem and providing a patch.
cmlenz
parents:
274
diff
changeset
|
36 def test_month_context(self): |
eb12741b65df
Fix for incorrect month context lookup in date formatting. Closes #75. Thanks to Andrew Stromnov for reporting the problem and providing a patch.
cmlenz
parents:
274
diff
changeset
|
37 d = date(2006, 1, 8) |
eb12741b65df
Fix for incorrect month context lookup in date formatting. Closes #75. Thanks to Andrew Stromnov for reporting the problem and providing a patch.
cmlenz
parents:
274
diff
changeset
|
38 fmt = dates.DateTimeFormat(d, locale='cs_CZ') |
eb12741b65df
Fix for incorrect month context lookup in date formatting. Closes #75. Thanks to Andrew Stromnov for reporting the problem and providing a patch.
cmlenz
parents:
274
diff
changeset
|
39 self.assertEqual('1', fmt['MMM']) |
eb12741b65df
Fix for incorrect month context lookup in date formatting. Closes #75. Thanks to Andrew Stromnov for reporting the problem and providing a patch.
cmlenz
parents:
274
diff
changeset
|
40 fmt = dates.DateTimeFormat(d, locale='cs_CZ') |
eb12741b65df
Fix for incorrect month context lookup in date formatting. Closes #75. Thanks to Andrew Stromnov for reporting the problem and providing a patch.
cmlenz
parents:
274
diff
changeset
|
41 self.assertEqual('1.', fmt['LLL']) |
eb12741b65df
Fix for incorrect month context lookup in date formatting. Closes #75. Thanks to Andrew Stromnov for reporting the problem and providing a patch.
cmlenz
parents:
274
diff
changeset
|
42 |
375
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
348
diff
changeset
|
43 def test_abbreviated_month_alias(self): |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
348
diff
changeset
|
44 d = date(2006, 3, 8) |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
348
diff
changeset
|
45 fmt = dates.DateTimeFormat(d, locale='de_DE') |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
348
diff
changeset
|
46 self.assertEqual(u'Mär', fmt['LLL']) |
4eca63af0a12
Implement support for aliases in the CLDR data. Closes #68. Also, update to CLDR 1.6, and a much improved `dump_data` script.
cmlenz
parents:
348
diff
changeset
|
47 |
240 | 48 def test_week_of_year_first(self): |
239 | 49 d = date(2006, 1, 8) |
50 fmt = dates.DateTimeFormat(d, locale='de_DE') | |
51 self.assertEqual('1', fmt['w']) | |
215
7fcdcdf8972d
Dummy/stub implementation for week-in-year and week-in-month date format fields. Also, treat extended year the same as the regular year field, not even ICU seems to handle it specially.
cmlenz
parents:
129
diff
changeset
|
52 fmt = dates.DateTimeFormat(d, locale='en_US') |
239 | 53 self.assertEqual('02', fmt['ww']) |
215
7fcdcdf8972d
Dummy/stub implementation for week-in-year and week-in-month date format fields. Also, treat extended year the same as the regular year field, not even ICU seems to handle it specially.
cmlenz
parents:
129
diff
changeset
|
54 |
242
910a6fe4471d
Finish implementation of day-of-year and day-of-month for now, and fix implementation of year-of-week-of-year field. Closes #46. I suspect there are still cases not entirely covered by this implementation, but those can be filed as separate tickets.
cmlenz
parents:
241
diff
changeset
|
55 def test_week_of_year_first_with_year(self): |
910a6fe4471d
Finish implementation of day-of-year and day-of-month for now, and fix implementation of year-of-week-of-year field. Closes #46. I suspect there are still cases not entirely covered by this implementation, but those can be filed as separate tickets.
cmlenz
parents:
241
diff
changeset
|
56 d = date(2006, 1, 1) |
910a6fe4471d
Finish implementation of day-of-year and day-of-month for now, and fix implementation of year-of-week-of-year field. Closes #46. I suspect there are still cases not entirely covered by this implementation, but those can be filed as separate tickets.
cmlenz
parents:
241
diff
changeset
|
57 fmt = dates.DateTimeFormat(d, locale='de_DE') |
910a6fe4471d
Finish implementation of day-of-year and day-of-month for now, and fix implementation of year-of-week-of-year field. Closes #46. I suspect there are still cases not entirely covered by this implementation, but those can be filed as separate tickets.
cmlenz
parents:
241
diff
changeset
|
58 self.assertEqual('52', fmt['w']) |
910a6fe4471d
Finish implementation of day-of-year and day-of-month for now, and fix implementation of year-of-week-of-year field. Closes #46. I suspect there are still cases not entirely covered by this implementation, but those can be filed as separate tickets.
cmlenz
parents:
241
diff
changeset
|
59 self.assertEqual('2005', fmt['YYYY']) |
910a6fe4471d
Finish implementation of day-of-year and day-of-month for now, and fix implementation of year-of-week-of-year field. Closes #46. I suspect there are still cases not entirely covered by this implementation, but those can be filed as separate tickets.
cmlenz
parents:
241
diff
changeset
|
60 |
240 | 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']) | |
215
7fcdcdf8972d
Dummy/stub implementation for week-in-year and week-in-month date format fields. Also, treat extended year the same as the regular year field, not even ICU seems to handle it specially.
cmlenz
parents:
129
diff
changeset
|
65 fmt = dates.DateTimeFormat(d, locale='en_US') |
551 | 66 self.assertEqual('52', fmt['w']) |
240 | 67 |
68 def test_week_of_month_first(self): | |
69 d = date(2006, 1, 8) | |
70 fmt = dates.DateTimeFormat(d, locale='de_DE') | |
215
7fcdcdf8972d
Dummy/stub implementation for week-in-year and week-in-month date format fields. Also, treat extended year the same as the regular year field, not even ICU seems to handle it specially.
cmlenz
parents:
129
diff
changeset
|
71 self.assertEqual('1', fmt['W']) |
240 | 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']) | |
215
7fcdcdf8972d
Dummy/stub implementation for week-in-year and week-in-month date format fields. Also, treat extended year the same as the regular year field, not even ICU seems to handle it specially.
cmlenz
parents:
129
diff
changeset
|
81 |
221
2af0d1085dd4
Implement day-of-year date format field. Closes #49.
cmlenz
parents:
217
diff
changeset
|
82 def test_day_of_year(self): |
2af0d1085dd4
Implement day-of-year date format field. Closes #49.
cmlenz
parents:
217
diff
changeset
|
83 d = date(2007, 4, 1) |
2af0d1085dd4
Implement day-of-year date format field. Closes #49.
cmlenz
parents:
217
diff
changeset
|
84 fmt = dates.DateTimeFormat(d, locale='en_US') |
2af0d1085dd4
Implement day-of-year date format field. Closes #49.
cmlenz
parents:
217
diff
changeset
|
85 self.assertEqual('91', fmt['D']) |
577
e57abed6542a
fix format_date() with a datetime parameter (#282)
fschwarz
parents:
551
diff
changeset
|
86 |
e57abed6542a
fix format_date() with a datetime parameter (#282)
fschwarz
parents:
551
diff
changeset
|
87 def test_day_of_year_works_with_datetime(self): |
e57abed6542a
fix format_date() with a datetime parameter (#282)
fschwarz
parents:
551
diff
changeset
|
88 d = datetime(2007, 4, 1) |
e57abed6542a
fix format_date() with a datetime parameter (#282)
fschwarz
parents:
551
diff
changeset
|
89 fmt = dates.DateTimeFormat(d, locale='en_US') |
e57abed6542a
fix format_date() with a datetime parameter (#282)
fschwarz
parents:
551
diff
changeset
|
90 self.assertEqual('91', fmt['D']) |
221
2af0d1085dd4
Implement day-of-year date format field. Closes #49.
cmlenz
parents:
217
diff
changeset
|
91 |
2af0d1085dd4
Implement day-of-year date format field. Closes #49.
cmlenz
parents:
217
diff
changeset
|
92 def test_day_of_year_first(self): |
2af0d1085dd4
Implement day-of-year date format field. Closes #49.
cmlenz
parents:
217
diff
changeset
|
93 d = date(2007, 1, 1) |
2af0d1085dd4
Implement day-of-year date format field. Closes #49.
cmlenz
parents:
217
diff
changeset
|
94 fmt = dates.DateTimeFormat(d, locale='en_US') |
2af0d1085dd4
Implement day-of-year date format field. Closes #49.
cmlenz
parents:
217
diff
changeset
|
95 self.assertEqual('001', fmt['DDD']) |
2af0d1085dd4
Implement day-of-year date format field. Closes #49.
cmlenz
parents:
217
diff
changeset
|
96 |
2af0d1085dd4
Implement day-of-year date format field. Closes #49.
cmlenz
parents:
217
diff
changeset
|
97 def test_day_of_year_last(self): |
2af0d1085dd4
Implement day-of-year date format field. Closes #49.
cmlenz
parents:
217
diff
changeset
|
98 d = date(2007, 12, 31) |
2af0d1085dd4
Implement day-of-year date format field. Closes #49.
cmlenz
parents:
217
diff
changeset
|
99 fmt = dates.DateTimeFormat(d, locale='en_US') |
2af0d1085dd4
Implement day-of-year date format field. Closes #49.
cmlenz
parents:
217
diff
changeset
|
100 self.assertEqual('365', fmt['DDD']) |
2af0d1085dd4
Implement day-of-year date format field. Closes #49.
cmlenz
parents:
217
diff
changeset
|
101 |
241
15acce6db1da
Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents:
240
diff
changeset
|
102 def test_day_of_week_in_month(self): |
15acce6db1da
Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents:
240
diff
changeset
|
103 d = date(2007, 4, 15) |
15acce6db1da
Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents:
240
diff
changeset
|
104 fmt = dates.DateTimeFormat(d, locale='en_US') |
15acce6db1da
Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents:
240
diff
changeset
|
105 self.assertEqual('3', fmt['F']) |
15acce6db1da
Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents:
240
diff
changeset
|
106 |
15acce6db1da
Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents:
240
diff
changeset
|
107 def test_day_of_week_in_month_first(self): |
15acce6db1da
Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents:
240
diff
changeset
|
108 d = date(2007, 4, 1) |
15acce6db1da
Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents:
240
diff
changeset
|
109 fmt = dates.DateTimeFormat(d, locale='en_US') |
15acce6db1da
Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents:
240
diff
changeset
|
110 self.assertEqual('1', fmt['F']) |
15acce6db1da
Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents:
240
diff
changeset
|
111 |
15acce6db1da
Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents:
240
diff
changeset
|
112 def test_day_of_week_in_month_last(self): |
15acce6db1da
Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents:
240
diff
changeset
|
113 d = date(2007, 4, 29) |
15acce6db1da
Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents:
240
diff
changeset
|
114 fmt = dates.DateTimeFormat(d, locale='en_US') |
15acce6db1da
Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents:
240
diff
changeset
|
115 self.assertEqual('5', fmt['F']) |
15acce6db1da
Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents:
240
diff
changeset
|
116 |
15 | 117 def test_local_day_of_week(self): |
217 | 118 d = date(2007, 4, 1) # a sunday |
15 | 119 fmt = dates.DateTimeFormat(d, locale='de_DE') |
120 self.assertEqual('7', fmt['e']) # monday is first day of week | |
121 fmt = dates.DateTimeFormat(d, locale='en_US') | |
122 self.assertEqual('01', fmt['ee']) # sunday is first day of week | |
123 fmt = dates.DateTimeFormat(d, locale='dv_MV') | |
124 self.assertEqual('03', fmt['ee']) # friday is first day of week | |
125 | |
217 | 126 d = date(2007, 4, 2) # a monday |
15 | 127 fmt = dates.DateTimeFormat(d, locale='de_DE') |
128 self.assertEqual('1', fmt['e']) # monday is first day of week | |
129 fmt = dates.DateTimeFormat(d, locale='en_US') | |
130 self.assertEqual('02', fmt['ee']) # sunday is first day of week | |
131 fmt = dates.DateTimeFormat(d, locale='dv_MV') | |
132 self.assertEqual('04', fmt['ee']) # friday is first day of week | |
133 | |
134 def test_local_day_of_week_standalone(self): | |
217 | 135 d = date(2007, 4, 1) # a sunday |
15 | 136 fmt = dates.DateTimeFormat(d, locale='de_DE') |
137 self.assertEqual('7', fmt['c']) # monday is first day of week | |
138 fmt = dates.DateTimeFormat(d, locale='en_US') | |
139 self.assertEqual('1', fmt['c']) # sunday is first day of week | |
140 fmt = dates.DateTimeFormat(d, locale='dv_MV') | |
141 self.assertEqual('3', fmt['c']) # friday is first day of week | |
142 | |
217 | 143 d = date(2007, 4, 2) # a monday |
15 | 144 fmt = dates.DateTimeFormat(d, locale='de_DE') |
145 self.assertEqual('1', fmt['c']) # monday is first day of week | |
146 fmt = dates.DateTimeFormat(d, locale='en_US') | |
147 self.assertEqual('2', fmt['c']) # sunday is first day of week | |
148 fmt = dates.DateTimeFormat(d, locale='dv_MV') | |
149 self.assertEqual('4', fmt['c']) # friday is first day of week | |
150 | |
216
72813ca69416
Support for fractional seconds field in date formatting. Closes #47.
cmlenz
parents:
215
diff
changeset
|
151 def test_fractional_seconds(self): |
217 | 152 t = time(15, 30, 12, 34567) |
153 fmt = dates.DateTimeFormat(t, locale='en_US') | |
216
72813ca69416
Support for fractional seconds field in date formatting. Closes #47.
cmlenz
parents:
215
diff
changeset
|
154 self.assertEqual('3457', fmt['SSSS']) |
72813ca69416
Support for fractional seconds field in date formatting. Closes #47.
cmlenz
parents:
215
diff
changeset
|
155 |
72813ca69416
Support for fractional seconds field in date formatting. Closes #47.
cmlenz
parents:
215
diff
changeset
|
156 def test_fractional_seconds_zero(self): |
217 | 157 t = time(15, 30, 0) |
158 fmt = dates.DateTimeFormat(t, locale='en_US') | |
159 self.assertEqual('0000', fmt['SSSS']) | |
160 | |
161 def test_milliseconds_in_day(self): | |
162 t = time(15, 30, 12, 345000) | |
163 fmt = dates.DateTimeFormat(t, locale='en_US') | |
164 self.assertEqual('55812345', fmt['AAAA']) | |
165 | |
166 def test_milliseconds_in_day_zero(self): | |
167 d = time(0, 0, 0) | |
216
72813ca69416
Support for fractional seconds field in date formatting. Closes #47.
cmlenz
parents:
215
diff
changeset
|
168 fmt = dates.DateTimeFormat(d, locale='en_US') |
217 | 169 self.assertEqual('0000', fmt['AAAA']) |
216
72813ca69416
Support for fractional seconds field in date formatting. Closes #47.
cmlenz
parents:
215
diff
changeset
|
170 |
29 | 171 def test_timezone_rfc822(self): |
172 tz = timezone('Europe/Berlin') | |
173 t = time(15, 30, tzinfo=tz) | |
174 fmt = dates.DateTimeFormat(t, locale='de_DE') | |
175 self.assertEqual('+0100', fmt['Z']) | |
176 | |
177 def test_timezone_gmt(self): | |
178 tz = timezone('Europe/Berlin') | |
179 t = time(15, 30, tzinfo=tz) | |
180 fmt = dates.DateTimeFormat(t, locale='de_DE') | |
233
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
181 self.assertEqual('GMT+01:00', fmt['ZZZZ']) |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
182 |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
183 def test_timezone_no_uncommon(self): |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
184 tz = timezone('Europe/Paris') |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
185 dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz) |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
186 fmt = dates.DateTimeFormat(dt, locale='fr_CA') |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
187 self.assertEqual('France', fmt['v']) |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
188 |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
189 def test_timezone_with_uncommon(self): |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
190 tz = timezone('Europe/Paris') |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
191 dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz) |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
192 fmt = dates.DateTimeFormat(dt, locale='fr_CA') |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
193 self.assertEqual('HEC', fmt['V']) |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
194 |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
195 def test_timezone_location_format(self): |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
196 tz = timezone('Europe/Paris') |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
197 dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz) |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
198 fmt = dates.DateTimeFormat(dt, locale='fr_FR') |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
199 self.assertEqual('France', fmt['VVVV']) |
29 | 200 |
129 | 201 def test_timezone_walltime_short(self): |
202 tz = timezone('Europe/Paris') | |
203 t = time(15, 30, tzinfo=tz) | |
233
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
204 fmt = dates.DateTimeFormat(t, locale='fr_FR') |
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
205 self.assertEqual('HEC', fmt['v']) |
129 | 206 |
207 def test_timezone_walltime_long(self): | |
208 tz = timezone('Europe/Paris') | |
209 t = time(15, 30, tzinfo=tz) | |
233
da97a3138239
Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents:
221
diff
changeset
|
210 fmt = dates.DateTimeFormat(t, locale='fr_FR') |
430 | 211 self.assertEqual(u'Heure de l\u2019Europe centrale', fmt['vvvv']) |
29 | 212 |
273 | 213 def test_hour_formatting(self): |
214 l = 'en_US' | |
215 t = time(0, 0, 0) | |
216 self.assertEqual(dates.format_time(t, 'h a', locale=l), '12 AM') | |
217 self.assertEqual(dates.format_time(t, 'H', locale=l), '0') | |
218 self.assertEqual(dates.format_time(t, 'k', locale=l), '24') | |
219 self.assertEqual(dates.format_time(t, 'K a', locale=l), '0 AM') | |
220 t = time(12, 0, 0) | |
221 self.assertEqual(dates.format_time(t, 'h a', locale=l), '12 PM') | |
222 self.assertEqual(dates.format_time(t, 'H', locale=l), '12') | |
274 | 223 self.assertEqual(dates.format_time(t, 'k', locale=l), '12') |
273 | 224 self.assertEqual(dates.format_time(t, 'K a', locale=l), '0 PM') |
225 | |
15 | 226 |
19
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
227 class FormatDateTestCase(unittest.TestCase): |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
228 |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
229 def test_with_time_fields_in_pattern(self): |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
230 self.assertRaises(AttributeError, dates.format_date, date(2007, 04, 01), |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
231 "yyyy-MM-dd HH:mm", locale='en_US') |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
232 |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
233 def test_with_time_fields_in_pattern_and_datetime_param(self): |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
234 self.assertRaises(AttributeError, dates.format_date, |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
235 datetime(2007, 04, 01, 15, 30), |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
236 "yyyy-MM-dd HH:mm", locale='en_US') |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
237 |
577
e57abed6542a
fix format_date() with a datetime parameter (#282)
fschwarz
parents:
551
diff
changeset
|
238 def test_with_day_of_year_in_pattern_and_datetime_param(self): |
e57abed6542a
fix format_date() with a datetime parameter (#282)
fschwarz
parents:
551
diff
changeset
|
239 # format_date should work on datetimes just as well (see #282) |
e57abed6542a
fix format_date() with a datetime parameter (#282)
fschwarz
parents:
551
diff
changeset
|
240 d = datetime(2007, 04, 01) |
e57abed6542a
fix format_date() with a datetime parameter (#282)
fschwarz
parents:
551
diff
changeset
|
241 self.assertEqual('14', dates.format_date(d, 'w', locale='en_US')) |
e57abed6542a
fix format_date() with a datetime parameter (#282)
fschwarz
parents:
551
diff
changeset
|
242 |
19
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
243 |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
244 class FormatTimeTestCase(unittest.TestCase): |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
245 |
348
76bfb34282af
Fixes for timezone calculations in time formatting (#83).
cmlenz
parents:
344
diff
changeset
|
246 def test_with_naive_datetime_and_tzinfo(self): |
76bfb34282af
Fixes for timezone calculations in time formatting (#83).
cmlenz
parents:
344
diff
changeset
|
247 string = dates.format_time(datetime(2007, 4, 1, 15, 30), |
76bfb34282af
Fixes for timezone calculations in time formatting (#83).
cmlenz
parents:
344
diff
changeset
|
248 'long', tzinfo=timezone('US/Eastern'), |
76bfb34282af
Fixes for timezone calculations in time formatting (#83).
cmlenz
parents:
344
diff
changeset
|
249 locale='en') |
76bfb34282af
Fixes for timezone calculations in time formatting (#83).
cmlenz
parents:
344
diff
changeset
|
250 self.assertEqual('11:30:00 AM EDT', string) |
76bfb34282af
Fixes for timezone calculations in time formatting (#83).
cmlenz
parents:
344
diff
changeset
|
251 |
19
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
252 def test_with_date_fields_in_pattern(self): |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
253 self.assertRaises(AttributeError, dates.format_time, date(2007, 04, 01), |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
254 "yyyy-MM-dd HH:mm", locale='en_US') |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
255 |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
256 def test_with_date_fields_in_pattern_and_datetime_param(self): |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
257 self.assertRaises(AttributeError, dates.format_time, |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
258 datetime(2007, 04, 01, 15, 30), |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
259 "yyyy-MM-dd HH:mm", locale='en_US') |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
260 |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
261 |
395
a0457635279d
The `format_timedelta` function now returns, for example, ?1 day? instead of ?0 days? if the granularity is `day` and the delta is less than a day but greater than zero.
cmlenz
parents:
375
diff
changeset
|
262 class FormatTimedeltaTestCase(unittest.TestCase): |
a0457635279d
The `format_timedelta` function now returns, for example, ?1 day? instead of ?0 days? if the granularity is `day` and the delta is less than a day but greater than zero.
cmlenz
parents:
375
diff
changeset
|
263 |
a0457635279d
The `format_timedelta` function now returns, for example, ?1 day? instead of ?0 days? if the granularity is `day` and the delta is less than a day but greater than zero.
cmlenz
parents:
375
diff
changeset
|
264 def test_zero_seconds(self): |
a0457635279d
The `format_timedelta` function now returns, for example, ?1 day? instead of ?0 days? if the granularity is `day` and the delta is less than a day but greater than zero.
cmlenz
parents:
375
diff
changeset
|
265 string = dates.format_timedelta(timedelta(seconds=0), locale='en') |
519
045782926b0c
fix expected output from !FormatTimedeltaTestCase in babel.tests.dates and ensure it will be executed during the normal test run (see #259)
fschwarz
parents:
512
diff
changeset
|
266 self.assertEqual('0 secs', string) |
395
a0457635279d
The `format_timedelta` function now returns, for example, ?1 day? instead of ?0 days? if the granularity is `day` and the delta is less than a day but greater than zero.
cmlenz
parents:
375
diff
changeset
|
267 string = dates.format_timedelta(timedelta(seconds=0), |
a0457635279d
The `format_timedelta` function now returns, for example, ?1 day? instead of ?0 days? if the granularity is `day` and the delta is less than a day but greater than zero.
cmlenz
parents:
375
diff
changeset
|
268 granularity='hour', locale='en') |
519
045782926b0c
fix expected output from !FormatTimedeltaTestCase in babel.tests.dates and ensure it will be executed during the normal test run (see #259)
fschwarz
parents:
512
diff
changeset
|
269 self.assertEqual('0 hrs', string) |
395
a0457635279d
The `format_timedelta` function now returns, for example, ?1 day? instead of ?0 days? if the granularity is `day` and the delta is less than a day but greater than zero.
cmlenz
parents:
375
diff
changeset
|
270 |
a0457635279d
The `format_timedelta` function now returns, for example, ?1 day? instead of ?0 days? if the granularity is `day` and the delta is less than a day but greater than zero.
cmlenz
parents:
375
diff
changeset
|
271 def test_small_value_with_granularity(self): |
a0457635279d
The `format_timedelta` function now returns, for example, ?1 day? instead of ?0 days? if the granularity is `day` and the delta is less than a day but greater than zero.
cmlenz
parents:
375
diff
changeset
|
272 string = dates.format_timedelta(timedelta(seconds=42), |
a0457635279d
The `format_timedelta` function now returns, for example, ?1 day? instead of ?0 days? if the granularity is `day` and the delta is less than a day but greater than zero.
cmlenz
parents:
375
diff
changeset
|
273 granularity='hour', locale='en') |
519
045782926b0c
fix expected output from !FormatTimedeltaTestCase in babel.tests.dates and ensure it will be executed during the normal test run (see #259)
fschwarz
parents:
512
diff
changeset
|
274 self.assertEqual('1 hr', string) |
395
a0457635279d
The `format_timedelta` function now returns, for example, ?1 day? instead of ?0 days? if the granularity is `day` and the delta is less than a day but greater than zero.
cmlenz
parents:
375
diff
changeset
|
275 |
a0457635279d
The `format_timedelta` function now returns, for example, ?1 day? instead of ?0 days? if the granularity is `day` and the delta is less than a day but greater than zero.
cmlenz
parents:
375
diff
changeset
|
276 |
512 | 277 class TimeZoneAdjustTestCase(unittest.TestCase): |
278 def _utc(self): | |
279 UTC = FixedOffsetTimezone(0, 'UTC') | |
280 def fake_localize(self, dt, is_dst=False): | |
281 raise NotImplementedError() | |
282 UTC.localize = new.instancemethod(fake_localize, UTC, UTC.__class__) | |
283 # This is important to trigger the actual bug (#257) | |
284 self.assertEqual(False, hasattr(UTC, 'normalize')) | |
285 return UTC | |
286 | |
287 def test_can_format_time_with_non_pytz_timezone(self): | |
288 # regression test for #257 | |
289 utc = self._utc() | |
290 t = datetime(2007, 4, 1, 15, 30, tzinfo=utc) | |
291 formatted_time = dates.format_time(t, 'long', tzinfo=utc, locale='en') | |
292 self.assertEqual('3:30:00 PM +0000', formatted_time) | |
293 | |
294 | |
1 | 295 def suite(): |
296 suite = unittest.TestSuite() | |
297 suite.addTest(doctest.DocTestSuite(dates)) | |
15 | 298 suite.addTest(unittest.makeSuite(DateTimeFormatTestCase)) |
19
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
299 suite.addTest(unittest.makeSuite(FormatDateTestCase)) |
d8352fbaca65
Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents:
16
diff
changeset
|
300 suite.addTest(unittest.makeSuite(FormatTimeTestCase)) |
519
045782926b0c
fix expected output from !FormatTimedeltaTestCase in babel.tests.dates and ensure it will be executed during the normal test run (see #259)
fschwarz
parents:
512
diff
changeset
|
301 suite.addTest(unittest.makeSuite(FormatTimedeltaTestCase)) |
512 | 302 suite.addTest(unittest.makeSuite(TimeZoneAdjustTestCase)) |
1 | 303 return suite |
304 | |
395
a0457635279d
The `format_timedelta` function now returns, for example, ?1 day? instead of ?0 days? if the granularity is `day` and the delta is less than a day but greater than zero.
cmlenz
parents:
375
diff
changeset
|
305 |
1 | 306 if __name__ == '__main__': |
307 unittest.main(defaultTest='suite') |