Skip to content

Commit

Permalink
Made ISO8601 the default for DateTimeField, DateField and TimeField
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmoritz committed Aug 1, 2012
1 parent b49d6b7 commit e99124d
Show file tree
Hide file tree
Showing 11 changed files with 457 additions and 5 deletions.
17 changes: 12 additions & 5 deletions micromodels/fields.py
@@ -1,4 +1,6 @@
import datetime
from micromodels.packages import PySO8601


class BaseField(object):
"""Base class for all field types.
Expand Down Expand Up @@ -101,7 +103,7 @@ class DateTimeField(BaseField):
will be returned by :meth:`~micromodels.DateTimeField.to_serial`.
"""
def __init__(self, format, serial_format=None, **kwargs):
def __init__(self, format=None, serial_format=None, **kwargs):
super(DateTimeField, self).__init__(**kwargs)
self.format = format
self.serial_format = serial_format
Expand All @@ -115,6 +117,9 @@ def to_python(self):
# don't parse data that is already native
if isinstance(self.data, datetime.datetime):
return self.data
elif self.format is None:
# parse as iso8601
return PySO8601.parse(self.data)
else:
return datetime.datetime.strptime(self.data, self.format)

Expand All @@ -140,11 +145,13 @@ class TimeField(DateTimeField):

def to_python(self):
# don't parse data that is already native
if isinstance(self.data, datetime.time):
if isinstance(self.data, datetime.datetime):
return self.data

dt = super(TimeField, self).to_python()
return dt.time()
elif self.format is None:
# parse as iso8601
return PySO8601.parse_time(self.data).time()
else:
return datetime.datetime.strptime(self.data, self.format).time()


class WrappedObjectField(BaseField):
Expand Down
33 changes: 33 additions & 0 deletions micromodels/packages/PySO8601/__init__.py
@@ -0,0 +1,33 @@
from utility import *
from datetimestamps import parse_date, parse_time
from durations import parse_duration
from intervals import parse_interval
from timezones import Timezone
from __version__ import __version__

__all__ = ['parse',
'parse_date',
'parse_time',
'parse_duration',
'parse_interval',
'Timezone',
'ParseError',
'__version__']


def parse(representation):
"""Attempts to parse an ISO8601 formatted ``representation`` string,
which could be of any valid ISO8601 format (date, time, duration, interval).
Return value is specific to ``representation``.
"""
representation = str(representation).upper().strip()

if '/' in representation:
return parse_interval(representation)

if representation[0] is 'P':
return parse_duration(representation)

return parse_date(representation)

4 changes: 4 additions & 0 deletions micromodels/packages/PySO8601/__version__.py
@@ -0,0 +1,4 @@
__author__ = 'Phillip B Oldham'
__author_email__ = 'phillip.oldham@gmail.com'
__version__ = '0.1.7'
__licence__ = 'MIT'
147 changes: 147 additions & 0 deletions micromodels/packages/PySO8601/datetimestamps.py
@@ -0,0 +1,147 @@
"""
Years:
YYYY
Calendar Dates:
YYYY-MM-DD
YYYY-MM
YYYYMMDD
YYMMDD
Week Dates:
YYYY-Www-D
YYYY-Www
YYYYWwwD
YYYYWww
Ordinal Dates:
YYYY-DDD
YYYYDDD
Times:
hh:mm:ss
hh:mm
hhmmss
hhmm
<time>Z
<time>+|-hh:mm
<time>+|-hhmm
<time>+|-hh
"""

from datetime import datetime, date
import re

from utility import *
from timezones import Timezone

FRACTION = r'(?P<fraction>\.\d+)?'

TIMEZONE = r'(?P<timezone>Z|(\+|-)(\d{2})(:?\d{2})?)?$'

DATE_FORMATS = (
# Extended combined format
(re.compile(r'^(?P<matched>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})'+FRACTION+TIMEZONE), '%Y-%m-%dT%H:%M:%S'),
(re.compile(r'^(?P<matched>\d{4}-\d{2}-\d{2}T\d{2}:\d{2})'+TIMEZONE), '%Y-%m-%dT%H:%M'),

# Extended separate format
(re.compile(r'^(?P<matched>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})'+FRACTION+TIMEZONE), '%Y-%m-%d %H:%M:%S'),
(re.compile(r'^(?P<matched>\d{4}-\d{2}-\d{2} \d{2}:\d{2})'+TIMEZONE), '%Y-%m-%d %H:%M'),
(re.compile(r'^(?P<matched>\d{4}-\d{2}-\d{2} \d{2})'+TIMEZONE), '%Y-%m-%d %H'),

# Basic combined format
(re.compile(r'^(?P<matched>\d{8}T\d{2}:\d{2}:\d{2})'+FRACTION+TIMEZONE), '%Y%m%dT%H:%M:%S'),
(re.compile(r'^(?P<matched>\d{8}T\d{2}:\d{2})'+TIMEZONE), '%Y%m%dT%H:%M'),
(re.compile(r'^(?P<matched>\d{8}T\d{6})'+FRACTION+TIMEZONE), '%Y%m%dT%H%M%S'),
(re.compile(r'^(?P<matched>\d{8}T\d{4})'+TIMEZONE), '%Y%m%dT%H%M'),
(re.compile(r'^(?P<matched>\d{8}T\d{2})'+TIMEZONE), '%Y%m%dT%H'),

# Basic separate format
(re.compile(r'^(?P<matched>\d{8} \d{2}:\d{2}:\d{2})'+FRACTION+TIMEZONE), '%Y%m%d %H:%M:%S'),
(re.compile(r'^(?P<matched>\d{8} \d{2}:\d{2})'+TIMEZONE), '%Y%m%d %H:%M'),
(re.compile(r'^(?P<matched>\d{8} \d{6})'+TIMEZONE), '%Y%m%dT%H%M%S'),
(re.compile(r'^(?P<matched>\d{8} \d{4})'+TIMEZONE), '%Y%m%dT%H%M'),
(re.compile(r'^(?P<matched>\d{8} \d{2})'+TIMEZONE), '%Y%m%dT%H'),

# Week Dates
(re.compile(r'^(?P<matched>\d{4}-W\d{2}-\d)'+TIMEZONE), '%Y-W%U-%w'),
(re.compile(r'^(?P<matched>\d{4}-W\d{2})'+TIMEZONE), '%Y-W%U'),
(re.compile(r'^(?P<matched>\d{4}W\d{3})'+TIMEZONE), '%YW%U%w'),
(re.compile(r'^(?P<matched>\d{4}W\d{2})'+TIMEZONE), '%YW%U'),

# Ordinal Dates
(re.compile(r'^(?P<matched>\d{4}-\d{3})'+TIMEZONE), '%Y-%j'),
(re.compile(r'^(?P<matched>\d{7})'+TIMEZONE), '%Y%j'),

# Dates
(re.compile(r'^(?P<matched>\d{4}-\d{2}-\d{2})'), '%Y-%m-%d'),
(re.compile(r'^(?P<matched>\d{4}-\d{2})'), '%Y-%m'),
(re.compile(r'^(?P<matched>\d{8})'), '%Y%m%d'),
(re.compile(r'^(?P<matched>\d{6})'), '%y%m%d'),
(re.compile(r'^(?P<matched>\d{4})'), '%Y'),

)

TIME_FORMATS = (
# Times
(re.compile(r'^(?P<matched>\d{2}:\d{2}:\d{2})'+FRACTION+TIMEZONE), '%H:%M:%S'),
(re.compile(r'^(?P<matched>\d{2}\d{2}\d{2})'+FRACTION+TIMEZONE), '%H%M%S'),
(re.compile(r'^(?P<matched>\d{2}:\d{2})'+TIMEZONE), '%H:%M'),
(re.compile(r'^(?P<matched>\d{4})'+TIMEZONE), '%H%M'),

)


def parse_date(datestring):
"""Attepmts to parse an ISO8601 formatted ``datestring``.
Returns a ``datetime.datetime`` object.
"""
datestring = str(datestring).strip()

if not datestring[0].isdigit():
raise ParseError()

if 'W' in datestring.upper():
try:
datestring = datestring[:-1] + str(int(datestring[-1:]) -1)
except:
pass

for regex, pattern in DATE_FORMATS:
if regex.match(datestring):
found = regex.search(datestring).groupdict()
dt = datetime.utcnow().strptime(found['matched'], pattern)

if 'fraction' in found and found['fraction'] is not None:
dt = dt.replace(microsecond=int(found['fraction'][1:]))

if 'timezone' in found and found['timezone'] is not None:
dt = dt.replace(tzinfo=Timezone(found.get('timezone', '')))

return dt

return parse_time(datestring)


def parse_time(timestring):
"""Attepmts to parse an ISO8601 formatted ``timestring``.
Returns a ``datetime.datetime`` object.
"""
timestring = str(timestring).strip()

for regex, pattern in TIME_FORMATS:
if regex.match(timestring):
found = regex.search(timestring).groupdict()

dt = datetime.utcnow().strptime(found['matched'], pattern)
dt = datetime.combine(date.today(), dt.time())

if 'fraction' in found and found['fraction'] is not None:
dt = dt.replace(microsecond=int(found['fraction'][1:]))

if 'timezone' in found and found['timezone'] is not None:
dt = dt.replace(tzinfo=Timezone(found.get('timezone', '')))

return dt

raise ParseError()


93 changes: 93 additions & 0 deletions micromodels/packages/PySO8601/durations.py
@@ -0,0 +1,93 @@
import datetime
import re

from utility import *

WEEK_DURATION = re.compile(r'''# start
^P # duration designator
(\d+) # capture the number of weeks
W$ # week designator
''', re.VERBOSE)

SIMPLE_DURATION = re.compile(r"""# start
^P # duration designator
((?P<years>\d*[\.,]?\d+)Y)? # year designator
((?P<months>\d*[\.,]?\d+)M)? # month designator
((?P<days>\d*[\.,]?\d+)D)? # day designator
(?P<time>T)? # time designator
(?(time) # time designator lookup;
# skip next section if
# reference doesn't exist
((?P<hours>\d*[\.,]?\d+)H)? # hour designator
((?P<minutes>\d*[\.,]?\d+)M)? # minute designator
((?P<seconds>\d*[\.,]?\d+)S)? # second designator
)$
""", re.VERBOSE)

COMBINED_DURATION = re.compile(r"""# start
^P # duration designator
(?P<years>\d{4})? # year designator
-? # separator
(?P<months>\d{2})? # month designator
-? # separator
(?P<days>\d{2})? # day designator
(?P<time>[T|\s])? # time designator
(?(time) # time designator lookup;
# skip next section if
# reference doesn't exist
(?P<hours>\d{2}) # hour designator
:? # separator
(?P<minutes>\d{2})? # minute designator
(?(minutes) # minutes designator lookup
:? # separator
(?P<seconds>\d{2})? # second designator
)
)$
""", re.VERBOSE)

ELEMENTS = {
'years': 0,
'months': 0,
'days': 0,
'hours': 0,
'minutes': 0,
'seconds': 0,
}


def parse_duration(duration):
"""Attepmts to parse an ISO8601 formatted ``duration``.
Returns a ``datetime.timedelta`` object.
"""
duration = str(duration).upper().strip()

elements = ELEMENTS.copy()

for pattern in (SIMPLE_DURATION, COMBINED_DURATION):
if pattern.match(duration):
found = pattern.match(duration).groupdict()
del found['time']

elements.update(dict((k, int(v or 0))
for k, v
in found.iteritems()))

return datetime.timedelta(days=(elements['days'] +
_months_to_days(elements['months']) +
_years_to_days(elements['years'])),
hours=elements['hours'],
minutes=elements['minutes'],
seconds=elements['seconds'])

return ParseError()


DAYS_IN_YEAR = 365
MONTHS_IN_YEAR = 12

def _years_to_days(years):
return years * DAYS_IN_YEAR

def _months_to_days(months):
return (months * DAYS_IN_YEAR) / MONTHS_IN_YEAR
29 changes: 29 additions & 0 deletions micromodels/packages/PySO8601/intervals.py
@@ -0,0 +1,29 @@
import datetime
import re

from utility import *
from durations import parse_duration
from datetimestamps import parse_date

def parse_interval(interval):
"""Attepmts to parse an ISO8601 formatted ``interval``.
Returns a tuple of ``datetime.datetime`` and ``datetime.timedelta`` objects, order dependent on ``interval``.
"""
a, b = str(interval).upper().strip().split('/')

if a[0] is 'P' and b[0] is 'P':
raise ParseError()

if a[0] is 'P':
a = parse_duration(a)
else:
a = parse_date(a)

if b[0] is 'P':
b = parse_duration(b)
else:
b = parse_date(b)

return a, b

19 changes: 19 additions & 0 deletions micromodels/packages/PySO8601/licence
@@ -0,0 +1,19 @@
Copyright (c) 2011 Phillip B Oldham

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

0 comments on commit e99124d

Please sign in to comment.