# -*- coding: utf-8 -*-
#Copyright 2013 Brandon Nielsen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
import datetime
from timezone import parse_timezone, build_utcoffset
from date import parse_date
def parse_time(isotimestr):
#Given a string in any ISO8601 time format, return a datetime.time object
#that corresponds to the given time. Fixed offset tzdata will be included
#if UTC offset is given in the input string. Valid time formats are:
#
#hh:mm:ss
#hhmmss
#hh:mm
#hhmm
#hh
#hh:mm:ssZ
#hhmmssZ
#hh:mmZ
#hhmmZ
#hhZ
#hh:mm:ss±hh:mm
#hhmmss±hh:mm
#hh:mm±hh:mm
#hhmm±hh:mm
#hh±hh:mm
#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
#Split the string at the TZ, if necessary
if isotimestr.find('+') != -1:
timestr = isotimestr[0:isotimestr.find('+')]
tzstr = isotimestr[isotimestr.find('+'):]
elif isotimestr.find('-') != -1:
timestr = isotimestr[0:isotimestr.find('-')]
tzstr = isotimestr[isotimestr.find('-'):]
elif isotimestr.endswith('Z'):
timestr = isotimestr[:-1]
tzstr = 'Z'
else:
timestr = isotimestr
tzstr = None
if tzstr == None:
return parse_time_naive(timestr)
elif tzstr == 'Z':
return parse_time_naive(timestr).replace(tzinfo=build_utcoffset('UTC', datetime.timedelta(hours=0)))
else:
return parse_time_naive(timestr).replace(tzinfo=parse_timezone(tzstr))
def parse_datetime(isodatetimestr, delimiter='T'):
#Given a string in ISO8601 date time format, return a datetime.datetime
#object that corresponds to the given date time.
#By default, the ISO8601 specified T delimiter is used to split the
#date and time (T