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