Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Bit of a hack, since datetime is a subclass of date we can't just
# test it with an isinstance(). And actually, we should not really
# care about the type, as long as it has the required attributes
if not all(hasattr(value, attr) for attr in ("hour", "minute", "second")):
value = datetime.datetime.combine(
value,
datetime.time(
getattr(value, "hour", 0),
getattr(value, "minute", 0),
getattr(value, "second", 0),
),
)
if getattr(value, "microsecond", 0):
return isodate.isostrf.strftime(value, "%Y-%m-%dT%H:%M:%S.%f%Z")
return isodate.isostrf.strftime(value, "%Y-%m-%dT%H:%M:%S%Z")
def triple_period_format_z(ds):
ws = []
for w in ds:
ws.append('/'.join([
w[0].strftime('%Y-%m-%dT%H:%M:%S'),
w[1].strftime('%Y-%m-%dT%H:%M:%S'),
strftime(w[2], D_DEFAULT)
]))
return ','.join(ws)
Format duration strings.
This method is just a wrapper around isodate.isostrf.strftime and uses
P%P (D_DEFAULT) as default format.
'''
# TODO: implement better decision for negative Durations.
# should be done in Duration class in consistent way with timedelta.
if (((isinstance(tduration, Duration) and
(tduration.years < 0 or tduration.months < 0 or
tduration.tdelta < timedelta(0))) or
(isinstance(tduration, timedelta) and
(tduration < timedelta(0))))):
ret = '-'
else:
ret = ''
ret += strftime(tduration, format)
return ret
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)
def datetime_isoformat(tdt, format=DATE_EXT_COMPLETE + 'T' +
TIME_EXT_COMPLETE + TZ_EXT):
'''
Format datetime strings.
This method is just a wrapper around isodate.isostrf.strftime and uses
Extended-Complete as default format.
'''
return strftime(tdt, format)
def xmlvalue(self, value):
if isinstance(value, six.string_types):
return value
if value.microsecond:
return isodate.isostrf.strftime(value, "%H:%M:%S.%f%Z")
return isodate.isostrf.strftime(value, "%H:%M:%S%Z")
def date_isoformat(tdate, format=DATE_EXT_COMPLETE, yeardigits=4):
'''
Format date strings.
This method is just a wrapper around isodate.isostrf.strftime and uses
Date-Extended-Complete as default format.
'''
return strftime(tdate, format, yeardigits)
'''
Format duration strings.
This method is just a wrapper around isodate.isostrf.strftime and uses
P%P (D_DEFAULT) as default format.
'''
# TODO: implement better decision for negative Durations.
# should be done in Duration class in consistent way with timedelta.
if ((isinstance(tduration, Duration) and (tduration.years < 0 or
tduration.months < 0 or
tduration.tdelta < timedelta(0)))
or (isinstance(tduration, timedelta) and (tduration < timedelta(0)))):
ret = '-'
else:
ret = ''
ret += strftime(tduration, format)
return ret
def xmlvalue(self, value):
if isinstance(value, six.string_types):
return value
if value.microsecond:
return isodate.isostrf.strftime(value, "%H:%M:%S.%f%Z")
return isodate.isostrf.strftime(value, "%H:%M:%S%Z")
def datetime_isoformat(tdt, format=DATE_EXT_COMPLETE + 'T' +
TIME_EXT_COMPLETE + TZ_EXT):
'''
Format datetime strings.
This method is just a wrapper around isodate.isostrf.strftime and uses
Extended-Complete as default format.
'''
return strftime(tdt, format)