elif char == 's':
return self.format(self.value.second, num)
elif char == 'S':
- return self.format_frac_seconds(self.value.microsecond, num)
+ return self.format_frac_seconds(num)
+ elif char == 'A':
+ return self.format_milliseconds_in_day(num)
elif char in ('z', 'Z', 'v'):
return self.format_timezone(char, num)
else:
period = {0: 'am', 1: 'pm'}[int(self.value.hour > 12)]
return get_period_names(locale=self.locale)[period]
- def format_frac_seconds(self, char, num):
+ def format_frac_seconds(self, num):
value = str(self.value.microsecond)
return self.format(round(float('.%s' % value), num) * 10**num, num)
+ def format_milliseconds_in_day(self, num):
+ msecs = self.value.microsecond // 1000 + self.value.second * 1000 + \
+ self.value.minute * 60000 + self.value.hour * 3600000
+ return self.format(msecs, num)
+
def format_timezone(self, char, num):
if char in ('z', 'v'):
if hasattr(self.value.tzinfo, 'zone'):
class DateTimeFormatTestCase(unittest.TestCase):
def test_week_of_year(self):
- d = datetime(2007, 4, 1)
+ d = date(2007, 4, 1)
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('13', fmt['w'])
def test_week_of_month(self):
- d = datetime(2007, 4, 1)
+ d = date(2007, 4, 1)
fmt = dates.DateTimeFormat(d, locale='en_US')
self.assertEqual('1', fmt['W'])
def test_local_day_of_week(self):
- d = datetime(2007, 4, 1) # a sunday
+ 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')
fmt = dates.DateTimeFormat(d, locale='dv_MV')
self.assertEqual('03', fmt['ee']) # friday is first day of week
- d = datetime(2007, 4, 2) # a monday
+ 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('04', fmt['ee']) # friday is first day of week
def test_local_day_of_week_standalone(self):
- d = datetime(2007, 4, 1) # a sunday
+ 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')
fmt = dates.DateTimeFormat(d, locale='dv_MV')
self.assertEqual('3', fmt['c']) # friday is first day of week
- d = datetime(2007, 4, 2) # a monday
+ 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('4', fmt['c']) # friday is first day of week
def test_fractional_seconds(self):
- d = time(15, 30, 12, 34567)
- fmt = dates.DateTimeFormat(d, locale='en_US')
+ t = time(15, 30, 12, 34567)
+ fmt = dates.DateTimeFormat(t, locale='en_US')
self.assertEqual('3457', fmt['SSSS'])
def test_fractional_seconds_zero(self):
- d = time(15, 30, 0)
- fmt = dates.DateTimeFormat(d, locale='en_US')
+ 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)