# -*- coding: utf-8 -*- # Copyright (c) 2018, Brandon Nielsen # All rights reserved. # # This software may be modified and distributed under the terms # of the BSD license. See the LICENSE file for details. import datetime from aniso8601.date import parse_date from aniso8601.exceptions import HoursOutOfBoundsError, ISOFormatError, \ LeapSecondError, MidnightBoundsError, MinutesOutOfBoundsError, \ SecondsOutOfBoundsError from aniso8601.resolution import TimeResolution from aniso8601.timezone import parse_timezone def get_time_resolution(isotimestr): #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 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: #hhmm return TimeResolution.Minutes elif timestrlen == 2: #hh return TimeResolution.Hours raise ISOFormatError('"{0}" is not a valid ISO 8601 time.'.format(isotimestr)) def parse_time(isotimestr): #Given a string in any ISO 8601 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 (timestr, tzstr) = _split_tz(isotimestr) if tzstr is None: return _parse_time_naive(timestr) else: tzinfo = parse_timezone(tzstr) return _parse_time_naive(timestr).replace(tzinfo=tzinfo) 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