Commit 056b7d34 by Don Mitchell

More unit tests which found another timezone assumption :-)

parent 862a7d13
......@@ -22,6 +22,7 @@ from xmodule.modulestore.tests.factories import CourseFactory
from models.settings.course_metadata import CourseMetadata
from xmodule.modulestore.xml_importer import import_from_xml
from xmodule.modulestore.django import modulestore
import time
# YYYY-MM-DDThh:mm:ss.s+/-HH:MM
......@@ -39,6 +40,7 @@ class ConvertersTestCase(TestCase):
+ str(date2) + "!=" + str(expected_delta))
def test_iso_to_struct(self):
'''Test conversion from iso compatible date strings to struct_time'''
self.compare_dates(converters.jsdate_to_time("2013-01-01"),
converters.jsdate_to_time("2012-12-31"),
datetime.timedelta(days=1))
......@@ -54,10 +56,28 @@ class ConvertersTestCase(TestCase):
self.compare_dates(converters.jsdate_to_time("2013-01-01T00:00:00Z"),
converters.jsdate_to_time("2012-12-31T23:59:59Z"),
datetime.timedelta(seconds=1))
self.compare_dates(converters.jsdate_to_time("2012-12-31T23:00:01-01:00"),
self.compare_dates(
converters.jsdate_to_time("2012-12-31T23:00:01-01:00"),
converters.jsdate_to_time("2013-01-01T00:00:00+01:00"),
datetime.timedelta(hours=1, seconds=1))
def test_struct_to_iso(self):
'''
Test converting time reprs to iso dates
'''
self.assertEqual(
converters.time_to_isodate(
time.strptime("2012-12-31T23:59:59Z", "%Y-%m-%dT%H:%M:%SZ")),
"2012-12-31T23:59:59Z")
self.assertEqual(
converters.time_to_isodate(
jsdate_to_time("2012-12-31T23:59:59Z")),
"2012-12-31T23:59:59Z")
self.assertEqual(
converters.time_to_isodate(
jsdate_to_time("2012-12-31T23:00:01-01:00")),
"2013-01-01T00:00:01Z")
class CourseTestCase(ModuleStoreTestCase):
def setUp(self):
......
......@@ -4,9 +4,6 @@ import calendar
import dateutil.parser
tz = "{:+03d}:{:02d}".format(time.timezone / 3600, time.timezone % 3600)
def time_to_date(time_obj):
"""
Convert a time.time_struct to a true universal time (can pass to js Date
......@@ -18,9 +15,9 @@ def time_to_date(time_obj):
def time_to_isodate(source):
'''Convert to an iso date'''
if isinstance(source, time.struct_time):
return time.strftime('%Y-%m-%dT%H:%M:%S' + tz, source)
return time.strftime('%Y-%m-%dT%H:%M:%SZ', source)
elif isinstance(source, datetime):
return source.isoformat() + tz
return source.isoformat() + 'Z'
def jsdate_to_time(field):
......
......@@ -11,8 +11,10 @@ log = logging.getLogger(__name__)
class Date(ModelType):
tz = "{:+03d}:{:02d}".format(time.timezone / 3600, time.timezone % 3600)
'''
Date fields know how to parse and produce json (iso) compatible formats.
'''
# NB: these are copies of util.converters.*
def from_json(self, field):
"""
Parse an optional metadata key containing a time: if present, complain
......@@ -44,7 +46,7 @@ class Date(ModelType):
# struct_times are always utc
return time.strftime('%Y-%m-%dT%H:%M:%SZ', value)
elif isinstance(value, datetime.datetime):
return value.isoformat() + Date.tz
return value.isoformat() + 'Z'
TIMEDELTA_REGEX = re.compile(r'^((?P<days>\d+?) day(?:s?))?(\s)?((?P<hours>\d+?) hour(?:s?))?(\s)?((?P<minutes>\d+?) minute(?:s)?)?(\s)?((?P<seconds>\d+?) second(?:s)?)?$')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment