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