2010-09-28 16:18:12 +08:00
|
|
|
import unittest
|
2015-01-28 20:35:27 +08:00
|
|
|
from datetime import (
|
|
|
|
date as original_date, datetime as original_datetime,
|
|
|
|
time as original_time,
|
|
|
|
)
|
2010-09-27 23:14:30 +08:00
|
|
|
|
2014-12-17 07:30:26 +08:00
|
|
|
from django.utils.datetime_safe import date, datetime, time
|
2010-09-27 23:14:30 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2010-09-28 16:18:12 +08:00
|
|
|
class DatetimeTests(unittest.TestCase):
|
2010-09-27 23:14:30 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.just_safe = (1900, 1, 1)
|
|
|
|
self.just_unsafe = (1899, 12, 31, 23, 59, 59)
|
2014-12-17 07:30:26 +08:00
|
|
|
self.just_time = (11, 30, 59)
|
2010-09-27 23:14:30 +08:00
|
|
|
self.really_old = (20, 1, 1)
|
|
|
|
self.more_recent = (2006, 1, 1)
|
|
|
|
|
|
|
|
def test_compare_datetimes(self):
|
|
|
|
self.assertEqual(original_datetime(*self.more_recent), datetime(*self.more_recent))
|
|
|
|
self.assertEqual(original_datetime(*self.really_old), datetime(*self.really_old))
|
|
|
|
self.assertEqual(original_date(*self.more_recent), date(*self.more_recent))
|
|
|
|
self.assertEqual(original_date(*self.really_old), date(*self.really_old))
|
|
|
|
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertEqual(
|
|
|
|
original_date(*self.just_safe).strftime('%Y-%m-%d'), date(*self.just_safe).strftime('%Y-%m-%d')
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
original_datetime(*self.just_safe).strftime('%Y-%m-%d'), datetime(*self.just_safe).strftime('%Y-%m-%d')
|
|
|
|
)
|
2010-09-27 23:14:30 +08:00
|
|
|
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertEqual(
|
|
|
|
original_time(*self.just_time).strftime('%H:%M:%S'), time(*self.just_time).strftime('%H:%M:%S')
|
|
|
|
)
|
2014-12-17 07:30:26 +08:00
|
|
|
|
2010-09-27 23:14:30 +08:00
|
|
|
def test_safe_strftime(self):
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(date(*self.just_unsafe[:3]).strftime('%Y-%m-%d (weekday %w)'), '1899-12-31 (weekday 0)')
|
|
|
|
self.assertEqual(date(*self.just_safe).strftime('%Y-%m-%d (weekday %w)'), '1900-01-01 (weekday 1)')
|
2010-09-27 23:14:30 +08:00
|
|
|
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertEqual(
|
|
|
|
datetime(*self.just_unsafe).strftime('%Y-%m-%d %H:%M:%S (weekday %w)'), '1899-12-31 23:59:59 (weekday 0)'
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
datetime(*self.just_safe).strftime('%Y-%m-%d %H:%M:%S (weekday %w)'), '1900-01-01 00:00:00 (weekday 1)'
|
|
|
|
)
|
2010-09-27 23:14:30 +08:00
|
|
|
|
2014-12-17 07:30:26 +08:00
|
|
|
self.assertEqual(time(*self.just_time).strftime('%H:%M:%S AM'), '11:30:59 AM')
|
|
|
|
|
2010-09-27 23:14:30 +08:00
|
|
|
# %y will error before this date
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(date(*self.just_safe).strftime('%y'), '00')
|
|
|
|
self.assertEqual(datetime(*self.just_safe).strftime('%y'), '00')
|
2010-09-27 23:14:30 +08:00
|
|
|
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(date(1850, 8, 2).strftime("%Y/%m/%d was a %A"), '1850/08/02 was a Friday')
|
2010-09-27 23:14:30 +08:00
|
|
|
|
|
|
|
def test_zero_padding(self):
|
|
|
|
"""
|
|
|
|
Regression for #12524
|
|
|
|
|
|
|
|
Check that pre-1000AD dates are padded with zeros if necessary
|
|
|
|
"""
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(date(1, 1, 1).strftime("%Y/%m/%d was a %A"), '0001/01/01 was a Monday')
|