forked from j4mie/micromodels
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made ISO8601 the default for DateTimeField, DateField and TimeField
- Loading branch information
1 parent
b49d6b7
commit e99124d
Showing
11 changed files
with
457 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
__author__ = 'Phillip B Oldham' | ||
__author_email__ = 'phillip.oldham@gmail.com' | ||
__version__ = '0.1.7' | ||
__licence__ = 'MIT' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
Oops, something went wrong.