Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
parser.read(StringIO(data.INITIAL_GAME))
parser.flush()
assert parser.games[0].packets[0].ts == time(2, 59, 14, 608862)
# Test with an initial datetime
parser2 = LogParser()
parser2._current_date = datetime(2015, 1, 1)
parser2.read(StringIO(data.INITIAL_GAME))
parser2.flush()
assert parser2.games[0].packets[0].ts == datetime(2015, 1, 1, 2, 59, 14, 608862)
# Same test, with timezone
parser2 = LogParser()
parser2._current_date = parse_datetime("2015-01-01T02:58:00+0200")
parser2.read(StringIO(data.INITIAL_GAME))
parser2.flush()
ts = parser2.games[0].packets[0].ts
assert ts.year == 2015
assert ts.hour == 2
assert ts.second == 14
assert ts.tzinfo
assert ts.utcoffset() == timedelta(hours=2)
def test_parse_timestamp(self):
utc = build_utcoffset('UTC', timedelta(hours=0))
result = Ask._parse_timestamp('2017-07-08T07:38:00Z')
self.assertEqual(datetime(2017, 7, 8, 7, 38, 0, 0, utc), result)
result = Ask._parse_timestamp(1234567890)
self.assertEqual(datetime(2009, 2, 13, 23, 31, 30), result)
with self.assertRaises(ValueError):
Ask._parse_timestamp(None)
def on_restriction(rules, checkin, duration):
"""
Returns True if restrictions are consistent with the checkin
and duration given in argument. False otherwise
:param rules: list of rules (dict)
:param checkin: checkin time
:param duration: duration in hour. Float accepted
"""
checkin = parse_datetime(checkin)
duration = timedelta(hours=duration)
checkin_end = checkin + duration # datetime
month = checkin.date().month # month as number
isodow = checkin.isoweekday() # 1->7
year = checkin.year # 2015
day = checkin.strftime('%d') # 07
# analyze each rule and stop iteration on conflict
for rule in rules:
# first test season day/month
start_day, start_month = ('-' or rule['season_start']).split('-')
end_day, end_month = ('-' or rule['season_end']).split('-')
season_match = season_matching(
start_day,
def _parse_duration_combined(durationstr, relative):
#Period of the form PT<time>
#Split the string in to its component parts
datepart, timepart = durationstr[1:].split('T') #We skip the 'P'
datevalue = parse_date(datepart)
timevalue = parse_time(timepart)
if relative is True:
return dateutil.relativedelta.relativedelta(years=datevalue.year, months=datevalue.month, days=datevalue.day, hours=timevalue.hour, minutes=timevalue.minute, seconds=timevalue.second, microseconds=timevalue.microsecond)
else:
totaldays = datevalue.year * 365 + datevalue.month * 30 + datevalue.day
return datetime.timedelta(days=totaldays, hours=timevalue.hour, minutes=timevalue.minute, seconds=timevalue.second, microseconds=timevalue.microsecond)
</time>
def parse_datetime(isodatetimestr, delimiter='T'):
#Given a string in ISO 8601 date time format, return a datetime.datetime
#object that corresponds to the given date time.
#By default, the ISO 8601 specified T delimiter is used to split the
#date and time (T<time>). Fixed offset tzdata will be included
#if UTC offset is given in the input string.
isodatestr, isotimestr = isodatetimestr.split(delimiter)
datepart = parse_date(isodatestr)
timepart = parse_time(isotimestr)
return datetime.datetime.combine(datepart, timepart)
</time>
if len(isodatestr) <= 4:
return DateResolution.Year
#An ISO string may be a calendar represntation if:
# 1) When split on a hyphen, the sizes of the parts are 4, 2, 2 or 4, 2
# 2) There are no hyphens, and the length is 8
datestrsplit = isodatestr.split('-')
#Check case 1
if len(datestrsplit) == 2:
if len(datestrsplit[0]) == 4 and len(datestrsplit[1]) == 2:
return DateResolution.Month
if len(datestrsplit) == 3:
if len(datestrsplit[0]) == 4 and len(datestrsplit[1]) == 2 and len(datestrsplit[2]) == 2:
return DateResolution.Day
#Check case 2
if len(isodatestr) == 8 and isodatestr.find('-') == -1:
return DateResolution.Day
#An ISO string may be a ordinal date representation if:
# 1) When split on a hyphen, the sizes of the parts are 4, 3
# 2) There are no hyphens, and the length is 7
#Check case 1
if len(datestrsplit) == 2:
if len(datestrsplit[0]) == 4 and len(datestrsplit[1]) == 3:
return DateResolution.Ordinal
#Check case 2
if len(isodatestr) == 7 and isodatestr.find('-') == -1:
#YYYYWwwD
#YYYY-DDD
#YYYYDDD
if isodatestr.startswith('+') or isodatestr.startswith('-'):
raise NotImplementedError('ISO 8601 extended year representation not supported.')
isodatestrlen = len(isodatestr)
if isodatestr.find('W') != -1:
#Handle ISO 8601 week date format
hyphens_present = 1 if isodatestr.find('-') != -1 else 0
week_date_len = 7 + hyphens_present
weekday_date_len = 8 + 2*hyphens_present
if isodatestrlen == week_date_len:
#YYYY-Www
#YYYYWww
return DateResolution.Week
elif isodatestrlen == weekday_date_len:
#YYYY-Www-D
#YYYYWwwD
return DateResolution.Weekday
else:
raise ValueError('String is not a valid ISO 8601 week date.')
#If the size of the string of 4 or less, assume its a truncated year representation
if len(isodatestr) <= 4:
return DateResolution.Year
#An ISO string may be a calendar represntation if:
# 1) When split on a hyphen, the sizes of the parts are 4, 2, 2 or 4, 2
# 2) There are no hyphens, and the length is 8
datestrsplit = isodatestr.split('-')
#required context.
firstpart, secondpart = isointervalstr.split(intervaldelimiter)
if firstpart[0] == 'P':
#/
#Notice that these are not returned 'in order' (earlier to later), this
#is to maintain consistency with parsing / durations, as
#well as making repeating interval code cleaner. Users who desire
#durations to be in order can use the 'sorted' operator.
#We need to figure out if is a date, or a datetime
if secondpart.find(datetimedelimiter) != -1:
# is a datetime
duration = parse_duration(firstpart, relative=relative)
enddatetime = parse_datetime(secondpart, delimiter=datetimedelimiter)
return (enddatetime, enddatetime - duration)
else:
# must just be a date
duration = parse_duration(firstpart, relative=relative)
enddate = parse_date(secondpart)
#See if we need to upconvert to datetime to preserve resolution
if firstpart.find(datetimedelimiter) != -1:
return (enddate, datetime.combine(enddate, datetime.min.time()) - duration)
else:
return (enddate, enddate - duration)
elif secondpart[0] == 'P':
#/
#We need to figure out if is a date, or a datetime
if firstpart.find(datetimedelimiter) != -1:
return TimeResolution.Seconds
elif timestr.count(':') == 1:
#hh:mm
return TimeResolution.Minutes
#Format must be hhmmss, hhmm, or hh
if timestr.find('.') == -1:
#No time fractions
timestrlen = len(timestr)
else:
#The lowest order element is a fraction
timestrlen = len(timestr.split('.')[0])
if timestrlen == 6:
#hhmmss
return TimeResolution.Seconds
elif timestrlen == 4:
#hhmm
return TimeResolution.Minutes
elif timestrlen == 2:
#hh
return TimeResolution.Hours
else:
raise ValueError('String is not a valid ISO 8601 time.')
#hh:mm:ss±hhmm
#hhmmss±hhmm
#hh:mm±hhmm
#hhmm±hhmm
#hh±hhmm
#hh:mm:ss±hh
#hhmmss±hh
#hh:mm±hh
#hhmm±hh
#hh±hh
timestr = _split_tz(isotimestr)[0]
if timestr.count(':') == 2:
#hh:mm:ss
return TimeResolution.Seconds
elif timestr.count(':') == 1:
#hh:mm
return TimeResolution.Minutes
#Format must be hhmmss, hhmm, or hh
if timestr.find('.') == -1:
#No time fractions
timestrlen = len(timestr)
else:
#The lowest order element is a fraction
timestrlen = len(timestr.split('.')[0])
if timestrlen == 6:
#hhmmss
return TimeResolution.Seconds
elif timestrlen == 4: