-
Notifications
You must be signed in to change notification settings - Fork 430
/
dates.py
219 lines (177 loc) · 8.1 KB
/
dates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://babel.edgewall.org/wiki/License.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://babel.edgewall.org/log/.
from datetime import date, datetime, time
import doctest
import unittest
from pytz import timezone
from babel import dates
class DateTimeFormatTestCase(unittest.TestCase):
def test_week_of_year_first(self):
d = date(2006, 1, 8)
fmt = dates.DateTimeFormat(d, locale='de_DE')
self.assertEqual('1', fmt['w'])
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('02', fmt['ww'])
def test_week_of_year_first_with_year(self):
d = date(2006, 1, 1)
fmt = dates.DateTimeFormat(d, locale='de_DE')
self.assertEqual('52', fmt['w'])
self.assertEqual('2005', fmt['YYYY'])
def test_week_of_year_last(self):
d = date(2005, 12, 26)
fmt = dates.DateTimeFormat(d, locale='de_DE')
self.assertEqual('52', fmt['w'])
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('53', fmt['ww'])
def test_week_of_month_first(self):
d = date(2006, 1, 8)
fmt = dates.DateTimeFormat(d, locale='de_DE')
self.assertEqual('1', fmt['W'])
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('2', fmt['W'])
def test_week_of_month_last(self):
d = date(2006, 1, 29)
fmt = dates.DateTimeFormat(d, locale='de_DE')
self.assertEqual('4', fmt['W'])
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('5', fmt['W'])
def test_day_of_year(self):
d = date(2007, 4, 1)
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('91', fmt['D'])
def test_day_of_year_first(self):
d = date(2007, 1, 1)
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('001', fmt['DDD'])
def test_day_of_year_last(self):
d = date(2007, 12, 31)
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('365', fmt['DDD'])
def test_day_of_week_in_month(self):
d = date(2007, 4, 15)
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('3', fmt['F'])
def test_day_of_week_in_month_first(self):
d = date(2007, 4, 1)
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('1', fmt['F'])
def test_day_of_week_in_month_last(self):
d = date(2007, 4, 29)
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('5', fmt['F'])
def test_local_day_of_week(self):
d = date(2007, 4, 1) # a sunday
fmt = dates.DateTimeFormat(d, locale='de_DE')
self.assertEqual('7', fmt['e']) # monday is first day of week
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('01', fmt['ee']) # sunday is first day of week
fmt = dates.DateTimeFormat(d, locale='dv_MV')
self.assertEqual('03', fmt['ee']) # friday is first day of week
d = date(2007, 4, 2) # a monday
fmt = dates.DateTimeFormat(d, locale='de_DE')
self.assertEqual('1', fmt['e']) # monday is first day of week
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('02', fmt['ee']) # sunday is first day of week
fmt = dates.DateTimeFormat(d, locale='dv_MV')
self.assertEqual('04', fmt['ee']) # friday is first day of week
def test_local_day_of_week_standalone(self):
d = date(2007, 4, 1) # a sunday
fmt = dates.DateTimeFormat(d, locale='de_DE')
self.assertEqual('7', fmt['c']) # monday is first day of week
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('1', fmt['c']) # sunday is first day of week
fmt = dates.DateTimeFormat(d, locale='dv_MV')
self.assertEqual('3', fmt['c']) # friday is first day of week
d = date(2007, 4, 2) # a monday
fmt = dates.DateTimeFormat(d, locale='de_DE')
self.assertEqual('1', fmt['c']) # monday is first day of week
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('2', fmt['c']) # sunday is first day of week
fmt = dates.DateTimeFormat(d, locale='dv_MV')
self.assertEqual('4', fmt['c']) # friday is first day of week
def test_fractional_seconds(self):
t = time(15, 30, 12, 34567)
fmt = dates.DateTimeFormat(t, locale='en_US')
self.assertEqual('3457', fmt['SSSS'])
def test_fractional_seconds_zero(self):
t = time(15, 30, 0)
fmt = dates.DateTimeFormat(t, locale='en_US')
self.assertEqual('0000', fmt['SSSS'])
def test_milliseconds_in_day(self):
t = time(15, 30, 12, 345000)
fmt = dates.DateTimeFormat(t, locale='en_US')
self.assertEqual('55812345', fmt['AAAA'])
def test_milliseconds_in_day_zero(self):
d = time(0, 0, 0)
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('0000', fmt['AAAA'])
def test_timezone_rfc822(self):
tz = timezone('Europe/Berlin')
t = time(15, 30, tzinfo=tz)
fmt = dates.DateTimeFormat(t, locale='de_DE')
self.assertEqual('+0100', fmt['Z'])
def test_timezone_gmt(self):
tz = timezone('Europe/Berlin')
t = time(15, 30, tzinfo=tz)
fmt = dates.DateTimeFormat(t, locale='de_DE')
self.assertEqual('GMT+01:00', fmt['ZZZZ'])
def test_timezone_no_uncommon(self):
tz = timezone('Europe/Paris')
dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz)
fmt = dates.DateTimeFormat(dt, locale='fr_CA')
self.assertEqual('France', fmt['v'])
def test_timezone_with_uncommon(self):
tz = timezone('Europe/Paris')
dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz)
fmt = dates.DateTimeFormat(dt, locale='fr_CA')
self.assertEqual('HEC', fmt['V'])
def test_timezone_location_format(self):
tz = timezone('Europe/Paris')
dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz)
fmt = dates.DateTimeFormat(dt, locale='fr_FR')
self.assertEqual('France', fmt['VVVV'])
def test_timezone_walltime_short(self):
tz = timezone('Europe/Paris')
t = time(15, 30, tzinfo=tz)
fmt = dates.DateTimeFormat(t, locale='fr_FR')
self.assertEqual('HEC', fmt['v'])
def test_timezone_walltime_long(self):
tz = timezone('Europe/Paris')
t = time(15, 30, tzinfo=tz)
fmt = dates.DateTimeFormat(t, locale='fr_FR')
self.assertEqual(u'Heure de l’Europe centrale', fmt['vvvv'])
class FormatDateTestCase(unittest.TestCase):
def test_with_time_fields_in_pattern(self):
self.assertRaises(AttributeError, dates.format_date, date(2007, 04, 01),
"yyyy-MM-dd HH:mm", locale='en_US')
def test_with_time_fields_in_pattern_and_datetime_param(self):
self.assertRaises(AttributeError, dates.format_date,
datetime(2007, 04, 01, 15, 30),
"yyyy-MM-dd HH:mm", locale='en_US')
class FormatTimeTestCase(unittest.TestCase):
def test_with_date_fields_in_pattern(self):
self.assertRaises(AttributeError, dates.format_time, date(2007, 04, 01),
"yyyy-MM-dd HH:mm", locale='en_US')
def test_with_date_fields_in_pattern_and_datetime_param(self):
self.assertRaises(AttributeError, dates.format_time,
datetime(2007, 04, 01, 15, 30),
"yyyy-MM-dd HH:mm", locale='en_US')
def suite():
suite = unittest.TestSuite()
suite.addTest(doctest.DocTestSuite(dates))
suite.addTest(unittest.makeSuite(DateTimeFormatTestCase))
suite.addTest(unittest.makeSuite(FormatDateTestCase))
suite.addTest(unittest.makeSuite(FormatTimeTestCase))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')