utils: Add some ISO 8601 forms to get_date()

Support the forms listed by the W3C [1].  I also removed the
'%Y-%d-%m' form, which can be confused with the '%Y-%m-%d' ISO form.
The new ISO forms can use 'Z' to designate UTC or '[+-]HHMM' to
specify offsets from UTC.  Other time zone designators are not
supported.

The '%z' directive has only been supported since Python 3.2 [2], so if
you're running Pelican on Python 2.7, you're stuck with 'Z' for UTC.
Conveniently, we get ValueErrors for both invalid directives and
data/format missmatches, so we don't need special handling for the 2.7
case inside get_date().

[1]: http://www.w3.org/TR/NOTE-datetime
[2]: http://bugs.python.org/issue6641
This commit is contained in:
W. Trevor King 2013-06-11 21:40:13 -04:00
commit 228fc82fc9
3 changed files with 74 additions and 34 deletions

View file

@ -6,7 +6,7 @@ import os
import datetime
import time
import locale
from sys import platform
from sys import platform, version_info
from tempfile import mkdtemp
from pelican.generators import TemplatePagesGenerator
@ -37,26 +37,44 @@ class TestUtils(LoggedTestCase):
def test_get_date(self):
# valid ones
date = datetime.datetime(year=2012, month=11, day=22)
date_hour = datetime.datetime(year=2012, month=11, day=22, hour=22,
minute=11)
date_hour_sec = datetime.datetime(year=2012, month=11, day=22, hour=22,
minute=11, second=10)
dates = {'2012-11-22': date,
'2012/11/22': date,
'2012-11-22 22:11': date_hour,
'2012/11/22 22:11': date_hour,
'22-11-2012': date,
'22/11/2012': date,
'22.11.2012': date,
'2012-22-11': date,
'22.11.2012 22:11': date_hour,
'2012-11-22 22:11:10': date_hour_sec}
date_hour = datetime.datetime(
year=2012, month=11, day=22, hour=22, minute=11)
date_hour_sec = datetime.datetime(
year=2012, month=11, day=22, hour=22, minute=11, second=10)
date_hour_sec_z = datetime.datetime(
year=2012, month=11, day=22, hour=22, minute=11, second=10,
tzinfo=datetime.timezone.utc)
date_hour_sec_0430 = datetime.datetime(
year=2012, month=11, day=22, hour=22, minute=11, second=10,
tzinfo=datetime.timezone(datetime.timedelta(hours=4, minutes=30)))
date_hour_sec_frac_z = datetime.datetime(
year=2012, month=11, day=22, hour=22, minute=11, second=10,
microsecond=123000, tzinfo=datetime.timezone.utc)
dates = {
'2012-11-22': date,
'2012/11/22': date,
'2012-11-22 22:11': date_hour,
'2012/11/22 22:11': date_hour,
'22-11-2012': date,
'22/11/2012': date,
'22.11.2012': date,
'22.11.2012 22:11': date_hour,
'2012-11-22 22:11:10': date_hour_sec,
'2012-11-22T22:11:10Z': date_hour_sec_z,
'2012-11-22T22:11:10+0430': date_hour_sec_0430,
'2012-11-22T22:11:10.123Z': date_hour_sec_frac_z,
}
# invalid ones
invalid_dates = ['2010-110-12', 'yay']
if version_info < (3, 2):
dates.pop('2012-11-22T22:11:10-0500')
invalid_dates.append('2012-11-22T22:11:10-0500')
for value, expected in dates.items():
self.assertEqual(utils.get_date(value), expected, value)
# invalid ones
invalid_dates = ('2010-110-12', 'yay')
for item in invalid_dates:
self.assertRaises(ValueError, utils.get_date, item)