timezone conversion: more unit tests

and code reorganization
This commit is contained in:
Don Mitchell
2013-06-17 14:48:18 -04:00
parent 9d464701f9
commit 085a590bdd
2 changed files with 40 additions and 11 deletions

View File

@@ -19,6 +19,23 @@ class Date(ModelType):
CURRENT_YEAR = datetime.datetime.now(UTC).year
DEFAULT_DATE0 = datetime.datetime(CURRENT_YEAR, 1, 1, tzinfo=UTC)
DEFAULT_DATE1 = datetime.datetime(CURRENT_YEAR, 2, 2, tzinfo=UTC)
def _parse_date_wo_default_month_day(self, field):
"""
Parse the field as an iso string but prevent dateutils from defaulting the day or month while
allowing it to default the other fields.
"""
# It's not trivial to replace dateutil b/c parsing timezones as Z, +03:30, -400 is hard in python
# however, we don't want dateutil to default the month or day (but some tests at least expect
# us to default year); so, we'll see if dateutil uses the defaults for these the hard way
result = dateutil.parser.parse(field, default=self.DEFAULT_DATE0)
result_other = dateutil.parser.parse(field, default=self.DEFAULT_DATE1)
if result != result_other:
log.warning("Field {0} is missing month or day".format(self._name, field))
return None
if result.tzinfo is None:
result = result.replace(tzinfo=UTC)
return result
def from_json(self, field):
"""
Parse an optional metadata key containing a time: if present, complain
@@ -30,17 +47,7 @@ class Date(ModelType):
elif field is "":
return None
elif isinstance(field, basestring):
# It's not trivial to replace dateutil b/c parsing timezones as Z, +03:30, -400 is hard in python
# however, we don't want dateutil to default the month or day (but some tests at least expect
# us to default year); so, we'll see if dateutil uses the defaults for these the hard way
result = dateutil.parser.parse(field, default=self.DEFAULT_DATE0)
result_other = dateutil.parser.parse(field, default=self.DEFAULT_DATE1)
if result != result_other:
log.warning("Field {0} is missing month or day".format(self._name, field))
return None
if result.tzinfo is None:
result = result.replace(tzinfo=UTC)
return result
return self._parse_date_wo_default_month_day(field)
elif isinstance(field, (int, long, float)):
return datetime.datetime.fromtimestamp(field / 1000, UTC)
elif isinstance(field, time.struct_time):