annotate babel/tests/dates.py @ 511:f2098e9c05b4 stable-0.9.x

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 61cb2233fa49
children
rev   line source
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
2 #
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 1
diff changeset
3 # Copyright (C) 2007 Edgewall Software
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
4 # All rights reserved.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
5 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
9 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
13
19
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
14 from datetime import date, datetime, time
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
15 import doctest
511
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
16 import new
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
17 import unittest
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
18
29
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
19 from pytz import timezone
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
20
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
21 from babel import dates
511
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
22 from babel.util import FixedOffsetTimezone
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
23
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
24
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
25 class DateTimeFormatTestCase(unittest.TestCase):
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
26
397
61cb2233fa49 Ported [436] to 0.9.x branch.
cmlenz
parents: 379
diff changeset
27 def test_quarter_format(self):
61cb2233fa49 Ported [436] to 0.9.x branch.
cmlenz
parents: 379
diff changeset
28 d = date(2006, 6, 8)
61cb2233fa49 Ported [436] to 0.9.x branch.
cmlenz
parents: 379
diff changeset
29 fmt = dates.DateTimeFormat(d, locale='en_US')
61cb2233fa49 Ported [436] to 0.9.x branch.
cmlenz
parents: 379
diff changeset
30 self.assertEqual('2', fmt['Q'])
61cb2233fa49 Ported [436] to 0.9.x branch.
cmlenz
parents: 379
diff changeset
31 self.assertEqual('2nd quarter', fmt['QQQQ'])
61cb2233fa49 Ported [436] to 0.9.x branch.
cmlenz
parents: 379
diff changeset
32 d = date(2006, 12, 31)
61cb2233fa49 Ported [436] to 0.9.x branch.
cmlenz
parents: 379
diff changeset
33 fmt = dates.DateTimeFormat(d, locale='en_US')
61cb2233fa49 Ported [436] to 0.9.x branch.
cmlenz
parents: 379
diff changeset
34 self.assertEqual('Q4', fmt['QQQ'])
61cb2233fa49 Ported [436] to 0.9.x branch.
cmlenz
parents: 379
diff changeset
35
346
faf0ead3a132 Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents: 276
diff changeset
36 def test_month_context(self):
faf0ead3a132 Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents: 276
diff changeset
37 d = date(2006, 1, 8)
faf0ead3a132 Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents: 276
diff changeset
38 fmt = dates.DateTimeFormat(d, locale='cs_CZ')
faf0ead3a132 Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents: 276
diff changeset
39 self.assertEqual('1', fmt['MMM'])
faf0ead3a132 Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents: 276
diff changeset
40 fmt = dates.DateTimeFormat(d, locale='cs_CZ')
faf0ead3a132 Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents: 276
diff changeset
41 self.assertEqual('1.', fmt['LLL'])
faf0ead3a132 Merged revisions [358:360], [364:370], [373:378], [380:382] from [source:trunk].
cmlenz
parents: 276
diff changeset
42
379
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 349
diff changeset
43 def test_abbreviated_month_alias(self):
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 349
diff changeset
44 d = date(2006, 3, 8)
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 349
diff changeset
45 fmt = dates.DateTimeFormat(d, locale='de_DE')
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 349
diff changeset
46 self.assertEqual(u'Mär', fmt['LLL'])
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 349
diff changeset
47
240
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
48 def test_week_of_year_first(self):
239
4afe02d81083 Again, properly implement week-in-year (#46).
cmlenz
parents: 238
diff changeset
49 d = date(2006, 1, 8)
4afe02d81083 Again, properly implement week-in-year (#46).
cmlenz
parents: 238
diff changeset
50 fmt = dates.DateTimeFormat(d, locale='de_DE')
4afe02d81083 Again, properly implement week-in-year (#46).
cmlenz
parents: 238
diff changeset
51 self.assertEqual('1', fmt['w'])
215
d37628e752c4 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
4afe02d81083 Again, properly implement week-in-year (#46).
cmlenz
parents: 238
diff changeset
53 self.assertEqual('02', fmt['ww'])
215
d37628e752c4 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
059b74719f6b 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):
059b74719f6b 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)
059b74719f6b 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')
059b74719f6b 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'])
059b74719f6b 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'])
059b74719f6b 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
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
61 def test_week_of_year_last(self):
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
62 d = date(2005, 12, 26)
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
63 fmt = dates.DateTimeFormat(d, locale='de_DE')
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
64 self.assertEqual('52', fmt['w'])
215
d37628e752c4 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')
240
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
66 self.assertEqual('53', fmt['ww'])
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
67
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
68 def test_week_of_month_first(self):
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
69 d = date(2006, 1, 8)
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
70 fmt = dates.DateTimeFormat(d, locale='de_DE')
215
d37628e752c4 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
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
72 fmt = dates.DateTimeFormat(d, locale='en_US')
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
73 self.assertEqual('2', fmt['W'])
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
74
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
75 def test_week_of_month_last(self):
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
76 d = date(2006, 1, 29)
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
77 fmt = dates.DateTimeFormat(d, locale='de_DE')
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
78 self.assertEqual('4', fmt['W'])
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
79 fmt = dates.DateTimeFormat(d, locale='en_US')
e47245bf65b3 More work on #46 (week-of-year/week-of-month).
cmlenz
parents: 239
diff changeset
80 self.assertEqual('5', fmt['W'])
215
d37628e752c4 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
898b9450f274 Implement day-of-year date format field. Closes #49.
cmlenz
parents: 217
diff changeset
82 def test_day_of_year(self):
898b9450f274 Implement day-of-year date format field. Closes #49.
cmlenz
parents: 217
diff changeset
83 d = date(2007, 4, 1)
898b9450f274 Implement day-of-year date format field. Closes #49.
cmlenz
parents: 217
diff changeset
84 fmt = dates.DateTimeFormat(d, locale='en_US')
898b9450f274 Implement day-of-year date format field. Closes #49.
cmlenz
parents: 217
diff changeset
85 self.assertEqual('91', fmt['D'])
898b9450f274 Implement day-of-year date format field. Closes #49.
cmlenz
parents: 217
diff changeset
86
898b9450f274 Implement day-of-year date format field. Closes #49.
cmlenz
parents: 217
diff changeset
87 def test_day_of_year_first(self):
898b9450f274 Implement day-of-year date format field. Closes #49.
cmlenz
parents: 217
diff changeset
88 d = date(2007, 1, 1)
898b9450f274 Implement day-of-year date format field. Closes #49.
cmlenz
parents: 217
diff changeset
89 fmt = dates.DateTimeFormat(d, locale='en_US')
898b9450f274 Implement day-of-year date format field. Closes #49.
cmlenz
parents: 217
diff changeset
90 self.assertEqual('001', fmt['DDD'])
898b9450f274 Implement day-of-year date format field. Closes #49.
cmlenz
parents: 217
diff changeset
91
898b9450f274 Implement day-of-year date format field. Closes #49.
cmlenz
parents: 217
diff changeset
92 def test_day_of_year_last(self):
898b9450f274 Implement day-of-year date format field. Closes #49.
cmlenz
parents: 217
diff changeset
93 d = date(2007, 12, 31)
898b9450f274 Implement day-of-year date format field. Closes #49.
cmlenz
parents: 217
diff changeset
94 fmt = dates.DateTimeFormat(d, locale='en_US')
898b9450f274 Implement day-of-year date format field. Closes #49.
cmlenz
parents: 217
diff changeset
95 self.assertEqual('365', fmt['DDD'])
898b9450f274 Implement day-of-year date format field. Closes #49.
cmlenz
parents: 217
diff changeset
96
241
9fedbb6f9871 Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents: 240
diff changeset
97 def test_day_of_week_in_month(self):
9fedbb6f9871 Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents: 240
diff changeset
98 d = date(2007, 4, 15)
9fedbb6f9871 Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents: 240
diff changeset
99 fmt = dates.DateTimeFormat(d, locale='en_US')
9fedbb6f9871 Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents: 240
diff changeset
100 self.assertEqual('3', fmt['F'])
9fedbb6f9871 Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents: 240
diff changeset
101
9fedbb6f9871 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_first(self):
9fedbb6f9871 Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents: 240
diff changeset
103 d = date(2007, 4, 1)
9fedbb6f9871 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')
9fedbb6f9871 Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents: 240
diff changeset
105 self.assertEqual('1', fmt['F'])
9fedbb6f9871 Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents: 240
diff changeset
106
9fedbb6f9871 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_last(self):
9fedbb6f9871 Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents: 240
diff changeset
108 d = date(2007, 4, 29)
9fedbb6f9871 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')
9fedbb6f9871 Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents: 240
diff changeset
110 self.assertEqual('5', fmt['F'])
9fedbb6f9871 Implement day-of-week-in-month field in date formatting. Closes #50.
cmlenz
parents: 240
diff changeset
111
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
112 def test_local_day_of_week(self):
217
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
113 d = date(2007, 4, 1) # a sunday
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
114 fmt = dates.DateTimeFormat(d, locale='de_DE')
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
115 self.assertEqual('7', fmt['e']) # monday is first day of week
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
116 fmt = dates.DateTimeFormat(d, locale='en_US')
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
117 self.assertEqual('01', fmt['ee']) # sunday is first day of week
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
118 fmt = dates.DateTimeFormat(d, locale='dv_MV')
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
119 self.assertEqual('03', fmt['ee']) # friday is first day of week
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
120
217
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
121 d = date(2007, 4, 2) # a monday
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
122 fmt = dates.DateTimeFormat(d, locale='de_DE')
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
123 self.assertEqual('1', fmt['e']) # monday is first day of week
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
124 fmt = dates.DateTimeFormat(d, locale='en_US')
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
125 self.assertEqual('02', fmt['ee']) # sunday is first day of week
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
126 fmt = dates.DateTimeFormat(d, locale='dv_MV')
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
127 self.assertEqual('04', fmt['ee']) # friday is first day of week
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
128
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
129 def test_local_day_of_week_standalone(self):
217
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
130 d = date(2007, 4, 1) # a sunday
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
131 fmt = dates.DateTimeFormat(d, locale='de_DE')
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
132 self.assertEqual('7', fmt['c']) # monday is first day of week
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
133 fmt = dates.DateTimeFormat(d, locale='en_US')
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
134 self.assertEqual('1', fmt['c']) # sunday is first day of week
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
135 fmt = dates.DateTimeFormat(d, locale='dv_MV')
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
136 self.assertEqual('3', fmt['c']) # friday is first day of week
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
137
217
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
138 d = date(2007, 4, 2) # a monday
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
139 fmt = dates.DateTimeFormat(d, locale='de_DE')
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
140 self.assertEqual('1', fmt['c']) # monday is first day of week
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
141 fmt = dates.DateTimeFormat(d, locale='en_US')
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
142 self.assertEqual('2', fmt['c']) # sunday is first day of week
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
143 fmt = dates.DateTimeFormat(d, locale='dv_MV')
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
144 self.assertEqual('4', fmt['c']) # friday is first day of week
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
145
216
1f3c3924b1b5 Support for fractional seconds field in date formatting. Closes #47.
cmlenz
parents: 215
diff changeset
146 def test_fractional_seconds(self):
217
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
147 t = time(15, 30, 12, 34567)
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
148 fmt = dates.DateTimeFormat(t, locale='en_US')
216
1f3c3924b1b5 Support for fractional seconds field in date formatting. Closes #47.
cmlenz
parents: 215
diff changeset
149 self.assertEqual('3457', fmt['SSSS'])
1f3c3924b1b5 Support for fractional seconds field in date formatting. Closes #47.
cmlenz
parents: 215
diff changeset
150
1f3c3924b1b5 Support for fractional seconds field in date formatting. Closes #47.
cmlenz
parents: 215
diff changeset
151 def test_fractional_seconds_zero(self):
217
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
152 t = time(15, 30, 0)
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
153 fmt = dates.DateTimeFormat(t, locale='en_US')
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
154 self.assertEqual('0000', fmt['SSSS'])
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
155
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
156 def test_milliseconds_in_day(self):
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
157 t = time(15, 30, 12, 345000)
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
158 fmt = dates.DateTimeFormat(t, locale='en_US')
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
159 self.assertEqual('55812345', fmt['AAAA'])
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
160
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
161 def test_milliseconds_in_day_zero(self):
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
162 d = time(0, 0, 0)
216
1f3c3924b1b5 Support for fractional seconds field in date formatting. Closes #47.
cmlenz
parents: 215
diff changeset
163 fmt = dates.DateTimeFormat(d, locale='en_US')
217
4de6f4604830 Implement milliseconds in day (#48).
cmlenz
parents: 216
diff changeset
164 self.assertEqual('0000', fmt['AAAA'])
216
1f3c3924b1b5 Support for fractional seconds field in date formatting. Closes #47.
cmlenz
parents: 215
diff changeset
165
29
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
166 def test_timezone_rfc822(self):
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
167 tz = timezone('Europe/Berlin')
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
168 t = time(15, 30, tzinfo=tz)
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
169 fmt = dates.DateTimeFormat(t, locale='de_DE')
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
170 self.assertEqual('+0100', fmt['Z'])
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
171
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
172 def test_timezone_gmt(self):
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
173 tz = timezone('Europe/Berlin')
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
174 t = time(15, 30, tzinfo=tz)
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
175 fmt = dates.DateTimeFormat(t, locale='de_DE')
233
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
176 self.assertEqual('GMT+01:00', fmt['ZZZZ'])
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
177
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
178 def test_timezone_no_uncommon(self):
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
179 tz = timezone('Europe/Paris')
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
180 dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz)
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
181 fmt = dates.DateTimeFormat(dt, locale='fr_CA')
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
182 self.assertEqual('France', fmt['v'])
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
183
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
184 def test_timezone_with_uncommon(self):
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
185 tz = timezone('Europe/Paris')
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
186 dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz)
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
187 fmt = dates.DateTimeFormat(dt, locale='fr_CA')
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
188 self.assertEqual('HEC', fmt['V'])
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
189
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
190 def test_timezone_location_format(self):
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
191 tz = timezone('Europe/Paris')
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
192 dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz)
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
193 fmt = dates.DateTimeFormat(dt, locale='fr_FR')
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
194 self.assertEqual('France', fmt['VVVV'])
29
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
195
129
90bf48c74714 Implement wall-time time-zone display.
cmlenz
parents: 29
diff changeset
196 def test_timezone_walltime_short(self):
90bf48c74714 Implement wall-time time-zone display.
cmlenz
parents: 29
diff changeset
197 tz = timezone('Europe/Paris')
90bf48c74714 Implement wall-time time-zone display.
cmlenz
parents: 29
diff changeset
198 t = time(15, 30, tzinfo=tz)
233
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
199 fmt = dates.DateTimeFormat(t, locale='fr_FR')
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
200 self.assertEqual('HEC', fmt['v'])
129
90bf48c74714 Implement wall-time time-zone display.
cmlenz
parents: 29
diff changeset
201
90bf48c74714 Implement wall-time time-zone display.
cmlenz
parents: 29
diff changeset
202 def test_timezone_walltime_long(self):
90bf48c74714 Implement wall-time time-zone display.
cmlenz
parents: 29
diff changeset
203 tz = timezone('Europe/Paris')
90bf48c74714 Implement wall-time time-zone display.
cmlenz
parents: 29
diff changeset
204 t = time(15, 30, tzinfo=tz)
233
bc22f5aef216 Upgraded to CLDR 1.5 and improved timezone formatting.
cmlenz
parents: 221
diff changeset
205 fmt = dates.DateTimeFormat(t, locale='fr_FR')
379
1c0915da48c6 Ported [407:415/trunk] back to 0.9.x branch.
cmlenz
parents: 349
diff changeset
206 self.assertEqual(u'heure d’Europe centrale', fmt['vvvv'])
29
da1c9610e751 More work on timezones.
cmlenz
parents: 19
diff changeset
207
276
e4dc98533a26 Merged [301:303] from [source:trunk] via svnmerge.
cmlenz
parents: 242
diff changeset
208 def test_hour_formatting(self):
e4dc98533a26 Merged [301:303] from [source:trunk] via svnmerge.
cmlenz
parents: 242
diff changeset
209 l = 'en_US'
e4dc98533a26 Merged [301:303] from [source:trunk] via svnmerge.
cmlenz
parents: 242
diff changeset
210 t = time(0, 0, 0)
e4dc98533a26 Merged [301:303] from [source:trunk] via svnmerge.
cmlenz
parents: 242
diff changeset
211 self.assertEqual(dates.format_time(t, 'h a', locale=l), '12 AM')
e4dc98533a26 Merged [301:303] from [source:trunk] via svnmerge.
cmlenz
parents: 242
diff changeset
212 self.assertEqual(dates.format_time(t, 'H', locale=l), '0')
e4dc98533a26 Merged [301:303] from [source:trunk] via svnmerge.
cmlenz
parents: 242
diff changeset
213 self.assertEqual(dates.format_time(t, 'k', locale=l), '24')
e4dc98533a26 Merged [301:303] from [source:trunk] via svnmerge.
cmlenz
parents: 242
diff changeset
214 self.assertEqual(dates.format_time(t, 'K a', locale=l), '0 AM')
e4dc98533a26 Merged [301:303] from [source:trunk] via svnmerge.
cmlenz
parents: 242
diff changeset
215 t = time(12, 0, 0)
e4dc98533a26 Merged [301:303] from [source:trunk] via svnmerge.
cmlenz
parents: 242
diff changeset
216 self.assertEqual(dates.format_time(t, 'h a', locale=l), '12 PM')
e4dc98533a26 Merged [301:303] from [source:trunk] via svnmerge.
cmlenz
parents: 242
diff changeset
217 self.assertEqual(dates.format_time(t, 'H', locale=l), '12')
e4dc98533a26 Merged [301:303] from [source:trunk] via svnmerge.
cmlenz
parents: 242
diff changeset
218 self.assertEqual(dates.format_time(t, 'k', locale=l), '12')
e4dc98533a26 Merged [301:303] from [source:trunk] via svnmerge.
cmlenz
parents: 242
diff changeset
219 self.assertEqual(dates.format_time(t, 'K a', locale=l), '0 PM')
e4dc98533a26 Merged [301:303] from [source:trunk] via svnmerge.
cmlenz
parents: 242
diff changeset
220
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
221
19
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
222 class FormatDateTestCase(unittest.TestCase):
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
223
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
224 def test_with_time_fields_in_pattern(self):
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
225 self.assertRaises(AttributeError, dates.format_date, date(2007, 04, 01),
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
226 "yyyy-MM-dd HH:mm", locale='en_US')
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
227
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
228 def test_with_time_fields_in_pattern_and_datetime_param(self):
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
229 self.assertRaises(AttributeError, dates.format_date,
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
230 datetime(2007, 04, 01, 15, 30),
c0c92d11f1ab 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')
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
232
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
233
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
234 class FormatTimeTestCase(unittest.TestCase):
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
235
349
7e2983180f6d Ported [385] to 0.9.x branch.
cmlenz
parents: 346
diff changeset
236 def test_with_naive_datetime_and_tzinfo(self):
7e2983180f6d Ported [385] to 0.9.x branch.
cmlenz
parents: 346
diff changeset
237 string = dates.format_time(datetime(2007, 4, 1, 15, 30),
7e2983180f6d Ported [385] to 0.9.x branch.
cmlenz
parents: 346
diff changeset
238 'long', tzinfo=timezone('US/Eastern'),
7e2983180f6d Ported [385] to 0.9.x branch.
cmlenz
parents: 346
diff changeset
239 locale='en')
7e2983180f6d Ported [385] to 0.9.x branch.
cmlenz
parents: 346
diff changeset
240 self.assertEqual('11:30:00 AM EDT', string)
7e2983180f6d Ported [385] to 0.9.x branch.
cmlenz
parents: 346
diff changeset
241
19
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
242 def test_with_date_fields_in_pattern(self):
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
243 self.assertRaises(AttributeError, dates.format_time, date(2007, 04, 01),
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
244 "yyyy-MM-dd HH:mm", locale='en_US')
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
245
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
246 def test_with_date_fields_in_pattern_and_datetime_param(self):
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
247 self.assertRaises(AttributeError, dates.format_time,
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
248 datetime(2007, 04, 01, 15, 30),
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
249 "yyyy-MM-dd HH:mm", locale='en_US')
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
250
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
251
511
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
252 class TimeZoneAdjustTestCase(unittest.TestCase):
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
253 def _utc(self):
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
254 UTC = FixedOffsetTimezone(0, 'UTC')
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
255 def fake_localize(self, dt, is_dst=False):
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
256 raise NotImplementedError()
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
257 UTC.localize = new.instancemethod(fake_localize, UTC, UTC.__class__)
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
258 # This is important to trigger the actual bug (#257)
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
259 self.assertEqual(False, hasattr(UTC, 'normalize'))
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
260 return UTC
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
261
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
262 def test_can_format_time_with_non_pytz_timezone(self):
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
263 # regression test for #257
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
264 utc = self._utc()
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
265 t = datetime(2007, 4, 1, 15, 30, tzinfo=utc)
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
266 formatted_time = dates.format_time(t, 'long', tzinfo=utc, locale='en')
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
267 self.assertEqual('3:30:00 PM +0000', formatted_time)
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
268
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
269
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
270 def suite():
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
271 suite = unittest.TestSuite()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
272 suite.addTest(doctest.DocTestSuite(dates))
15
76985c08a339 Minor date formatting improvements.
cmlenz
parents: 12
diff changeset
273 suite.addTest(unittest.makeSuite(DateTimeFormatTestCase))
19
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
274 suite.addTest(unittest.makeSuite(FormatDateTestCase))
c0c92d11f1ab Moved some datetime tests from doctest to unittest, to avoid breaking docutils/epydoc doctest block detection.
cmlenz
parents: 16
diff changeset
275 suite.addTest(unittest.makeSuite(FormatTimeTestCase))
511
f2098e9c05b4 Fix bad check in format_time (closes #257), reported with patch and tests by jomae
fschwarz
parents: 397
diff changeset
276 suite.addTest(unittest.makeSuite(TimeZoneAdjustTestCase))
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
277 return suite
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
278
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
279 if __name__ == '__main__':
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
280 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software