def utc_offset(zone, zoneoffset=zoneoffsetRE, zonetable=zonetable,zerooffset=DateTime.DateTimeDelta(0), oneMinute=DateTime.oneMinute): """ utc_offset(zonestring) Return the UTC time zone offset as DateTimeDelta instance. zone must be string and can either be given as +-HH:MM, +-HHMM, +-HH numeric offset or as time zone abbreviation. Daylight saving time must be encoded into the zone offset. Timezone abbreviations are treated case-insensitive. """ if not zone: return zerooffset uzone = zone.upper() if zonetable.has_key(uzone): return zonetable[uzone]*DateTime.oneHour offset = zoneoffset.match(zone) if not offset: raise ValueError,'wrong format or unknown time zone: "%s"' % zone zonesign,hours,minutes,extra = offset.groups() if extra: raise ValueError,'illegal time zone offset: "%s"' % zone offset = int(hours or 0) * 60 + int(minutes or 0) if zonesign == '-': offset = -offset return offset*oneMinute
def TimeFromString(text, formats=_time_formats, DateTime=DateTime): """ TimeFromString(text, [formats]) Returns a DateTimeDelta instance reflecting the time given in text. A possibly included date part is ignored. formats may be set to a tuple of strings specifying which of the following parsers to use and in which order to try them. Default is to try all of them in the order given below: 'standard' - standard time format with ':' delimiter 'iso' - ISO time format (superset of 'standard') 'unknown' - default to 00:00:00 in case the time format cannot be parsed Defaults to 00:00:00.00 for parts that are not included in the textual representation. """ _text, hour, minute, second, offset, timestyle = _parse_time(text, formats) if timestyle == 'unknown' and \ 'unknown' not in formats: raise ValueError,\ 'Failed to parse "%s": found "%s" time' % \ (text, timestyle) try: dtd = DateTime.DateTimeDelta(0.0, hour, minute, second) except DateTime.RangeError, why: raise DateTime.RangeError,\ 'Failed to parse "%s": %s' % (text, why)
def DateTimeDeltaFromString(text, float=float, DateTime=DateTime): """ DateTimeDeltaFromString(text) Returns a DateTimeDelta instance reflecting the delta given in text. Defaults to 0:00:00:00.00 for parts that are not included in the textual representation or cannot be parsed. """ match = _isodelta1RE.search(text) if match is not None: sign, days, hours, minutes, seconds = match.groups() else: match = _litdelta2RE.search(text) if match is not None: sign, days, hours, minutes, seconds = match.groups() else: match = _isodelta2RE.search(text) if match is not None: sign, hours, minutes, seconds = match.groups() days = None else: match = _isodelta3RE.search(text) if match is not None: sign, hours, minutes = match.groups() days = None seconds = None else: match = _litdeltaRE.search(text) if match is not None: sign, days, hours, minutes, seconds = match.groups() else: # Not matched: return DateTime.DateTimeDelta(0.0) # Conversions if days: days = float(days) else: days = 0.0 if hours: hours = float(hours) else: hours = 0.0 if minutes: minutes = float(minutes) else: minutes = 0.0 if seconds: seconds = float(seconds) else: seconds = 0.0 if sign != '-': sign = 1 else: sign = -1 try: dtd = DateTime.DateTimeDelta(days, hours, minutes, seconds) except DateTime.RangeError, why: raise DateTime.RangeError,\ 'Failed to parse "%s": %s' % (text, why)