Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Parses ISO 8601 time zone designators to tzinfo objecs.
A time zone designator can be in the following format:
no designator indicates local time zone
Z UTC
+-hhmm basic hours and minutes
+-hh:mm extended hours and minutes
+-hh hours
'''
match = TZ_RE.match(tzstring)
if match:
groups = match.groupdict()
return build_tzinfo(groups['tzname'], groups['tzsign'],
int(groups['tzhour'] or 0),
int(groups['tzmin'] or 0))
raise ISO8601Error('%s not a valid time zone info' % tzstring)
int(second), microsecond.to_integral(), tzinfo)
if 'minute' in groups:
minute = Decimal(groups['minute'])
second = (minute - int(minute)) * 60
microsecond = (second - int(second)) * long(1e6)
return time(int(groups['hour']), int(minute), int(second),
microsecond.to_integral(), tzinfo)
else:
microsecond, second, minute = 0, 0, 0
hour = Decimal(groups['hour'])
minute = (hour - int(hour)) * 60
second = (minute - int(minute)) * 60
microsecond = (second - int(second)) * long(1e6)
return time(int(hour), int(minute), int(second),
microsecond.to_integral(), tzinfo)
raise ISO8601Error('Unrecognised ISO 8601 time format: %r' % timestring)
def assemble(cls, datum):
"""Parse *datum* as an ISO 8601-encoded time and return a
:class:`datetime` object. If the string is invalid, raise a
:exc:`ValidationError`.
"""
try:
return isodate.parse_datetime(datum)
except (ValueError, isodate.isoerror.ISO8601Error) as e:
raise ValidationError(e.args[0], datum)
tzinfo)
if 'minute' in groups:
minute = Decimal(groups['minute'])
second = (minute - int(minute)) * 60
microsecond = (second - int(second)) * int(1e6)
return time(int(groups['hour']), int(minute), int(second),
int(microsecond.to_integral()), tzinfo)
else:
microsecond, second, minute = 0, 0, 0
hour = Decimal(groups['hour'])
minute = (hour - int(hour)) * 60
second = (minute - int(minute)) * 60
microsecond = (second - int(second)) * int(1e6)
return time(int(hour), int(minute), int(second),
int(microsecond.to_integral()), tzinfo)
raise ISO8601Error('Unrecognised ISO 8601 time format: %r' % timestring)
from isodate.isostrf import DATE_CENTURY, DATE_EXT_COMPLETE
from isodate.isostrf import DATE_EXT_ORD_COMPLETE, DATE_EXT_WEEK
from isodate.isostrf import DATE_EXT_WEEK_COMPLETE, DATE_MONTH, DATE_YEAR
from isodate.isostrf import TIME_BAS_COMPLETE, TIME_BAS_MINUTE
from isodate.isostrf import TIME_EXT_COMPLETE, TIME_EXT_MINUTE
from isodate.isostrf import TIME_HOUR
from isodate.isostrf import TZ_BAS, TZ_EXT, TZ_HOUR
from isodate.isostrf import DT_BAS_COMPLETE, DT_EXT_COMPLETE
from isodate.isostrf import DT_BAS_ORD_COMPLETE, DT_EXT_ORD_COMPLETE
from isodate.isostrf import DT_BAS_WEEK_COMPLETE, DT_EXT_WEEK_COMPLETE
from isodate.isostrf import D_DEFAULT, D_WEEK, D_ALT_EXT, D_ALT_BAS
from isodate.isostrf import D_ALT_BAS_ORD, D_ALT_EXT_ORD
__all__ = (parse_date, date_isoformat, parse_time, time_isoformat,
parse_datetime, datetime_isoformat, parse_duration,
duration_isoformat, ISO8601Error, parse_tzinfo,
tz_isoformat, UTC, FixedOffset, LOCAL, Duration,
strftime, DATE_BAS_COMPLETE, DATE_BAS_ORD_COMPLETE,
DATE_BAS_WEEK, DATE_BAS_WEEK_COMPLETE, DATE_CENTURY,
DATE_EXT_COMPLETE, DATE_EXT_ORD_COMPLETE, DATE_EXT_WEEK,
DATE_EXT_WEEK_COMPLETE, DATE_MONTH, DATE_YEAR,
TIME_BAS_COMPLETE, TIME_BAS_MINUTE, TIME_EXT_COMPLETE,
TIME_EXT_MINUTE, TIME_HOUR, TZ_BAS, TZ_EXT, TZ_HOUR,
DT_BAS_COMPLETE, DT_EXT_COMPLETE, DT_BAS_ORD_COMPLETE,
DT_EXT_ORD_COMPLETE, DT_BAS_WEEK_COMPLETE,
DT_EXT_WEEK_COMPLETE, D_DEFAULT, D_WEEK, D_ALT_EXT,
D_ALT_BAS, D_ALT_BAS_ORD, D_ALT_EXT_ORD)
# if first week in year, do weeks-1
return ret + timedelta(weeks=int(groups['week']) -
(((isotuple[1] == 1) and 1) or 0),
days = -isotuple[2] + days)
elif 'day' in groups: # ordinal date
return ret + timedelta(days=int(groups['day'])-1)
else: # year date
return ret
# year-, month-, or complete date
if 'day' not in groups or groups['day'] is None:
day = 1
else:
day = int(groups['day'])
return date(sign * int(groups['year']),
int(groups['month']) or 1, day)
raise ISO8601Error('Unrecognised ISO 8601 date format: %r' % datestring)
"""
# DB value is null
if value is None:
return None
# DB value is empty
if value == '':
return None
if isinstance(value, isodate.duration.Duration):
return value
try:
duration = isodate.parse_duration(value)
except ISO8601Error:
raise ValidationError(self.default_error_messages['invalid'])
return duration
def assemble(self, datum):
"""Parse *datum* as an ISO 8601-encoded time and return a
:class:`datetime` object. If the string is invalid, raise a
:exc:`ValidationError`.
"""
try:
return isodate.parse_datetime(datum)
except (ValueError, isodate.isoerror.ISO8601Error) as e:
raise ValidationError(e.args[0], datum)
def compute_time_compare(granularity, periods):
if not granularity:
return None
# convert old db_engine_spec granularity to ISO duration
if granularity in db_engine_specs_map:
granularity = db_engine_specs_map[granularity]
try:
obj = isodate.parse_duration(granularity) * periods
except isodate.isoerror.ISO8601Error:
# if parse_human_timedelta can parse it, return it directly
delta = "{0} {1}{2}".format(periods, granularity, "s" if periods > 1 else "")
obj = parse_human_timedelta(delta)
if obj:
return delta
raise Exception("Unable to parse: {0}".format(granularity))
if isinstance(obj, isodate.duration.Duration):
return isodate_duration_to_string(obj)
elif isinstance(obj, datetime.timedelta):
return timedelta_to_string(obj)
# try alternative format:
if datestring.startswith("P"):
durdt = parse_datetime(datestring[1:])
if durdt.year != 0 or durdt.month != 0:
# create Duration
ret = Duration(days=durdt.day, seconds=durdt.second,
microseconds=durdt.microsecond,
minutes=durdt.minute, hours=durdt.hour,
months=durdt.month, years=durdt.year)
else: # FIXME: currently not possible in alternative format
# create timedelta
ret = timedelta(days=durdt.day, seconds=durdt.second,
microseconds=durdt.microsecond,
minutes=durdt.minute, hours=durdt.hour)
return ret
raise ISO8601Error("Unable to parse duration string %r" % datestring)
groups = match.groupdict()
for key, val in groups.items():
if key not in ('separator', 'sign'):
if val is None:
groups[key] = "0n"
#print groups[key]
groups[key] = float(groups[key][:-1].replace(',', '.'))
if groups["years"] == 0 and groups["months"] == 0:
ret = timedelta(days=groups["days"], hours=groups["hours"],
minutes=groups["minutes"], seconds=groups["seconds"],
weeks=groups["weeks"])
if groups["sign"] == '-':
ret = timedelta(0) - ret
else:
ret = Duration(years=groups["years"], months=groups["months"],
days=groups["days"], hours=groups["hours"],