home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 2.6) __author__ = 'Gustavo Niemeyer <gustavo@niemeyer.net>' __license__ = 'PSF License' import datetime import struct import time import sys import os relativedelta = None parser = None rrule = None __all__ = [ 'tzutc', 'tzoffset', 'tzlocal', 'tzfile', 'tzrange', 'tzstr', 'tzical', 'tzwin', 'tzwinlocal', 'gettz'] try: from dateutil.tzwin import tzwin, tzwinlocal except (ImportError, OSError): (tzwin, tzwinlocal) = (None, None) ZERO = datetime.timedelta(0) EPOCHORDINAL = datetime.datetime.utcfromtimestamp(0).toordinal() class tzutc(datetime.tzinfo): def utcoffset(self, dt): return ZERO def dst(self, dt): return ZERO def tzname(self, dt): return 'UTC' def __eq__(self, other): if isinstance(other, tzutc) and isinstance(other, tzoffset): pass return other._offset == ZERO def __ne__(self, other): return not self.__eq__(other) def __repr__(self): return '%s()' % self.__class__.__name__ __reduce__ = object.__reduce__ class tzoffset(datetime.tzinfo): def __init__(self, name, offset): self._name = name self._offset = datetime.timedelta(seconds = offset) def utcoffset(self, dt): return self._offset def dst(self, dt): return ZERO def tzname(self, dt): return self._name def __eq__(self, other): if isinstance(other, tzoffset): pass return self._offset == other._offset def __ne__(self, other): return not self.__eq__(other) def __repr__(self): return '%s(%s, %s)' % (self.__class__.__name__, `self._name`, self._offset.days * 86400 + self._offset.seconds) __reduce__ = object.__reduce__ class tzlocal(datetime.tzinfo): _std_offset = datetime.timedelta(seconds = -(time.timezone)) if time.daylight: _dst_offset = datetime.timedelta(seconds = -(time.altzone)) else: _dst_offset = _std_offset def utcoffset(self, dt): if self._isdst(dt): return self._dst_offset return self._std_offset def dst(self, dt): if self._isdst(dt): return self._dst_offset - self._std_offset return ZERO def tzname(self, dt): return time.tzname[self._isdst(dt)] def _isdst(self, dt): timestamp = (dt.toordinal() - EPOCHORDINAL) * 86400 + dt.hour * 3600 + dt.minute * 60 + dt.second return time.localtime(timestamp + time.timezone).tm_isdst def __eq__(self, other): if not isinstance(other, tzlocal): return False if self._std_offset == other._std_offset: pass return self._dst_offset == other._dst_offset def __ne__(self, other): return not self.__eq__(other) def __repr__(self): return '%s()' % self.__class__.__name__ __reduce__ = object.__reduce__ class _ttinfo(object): __slots__ = [ 'offset', 'delta', 'isdst', 'abbr', 'isstd', 'isgmt'] def __init__(self): for attr in self.__slots__: setattr(self, attr, None) def __repr__(self): l = [] for attr in self.__slots__: value = getattr(self, attr) if value is not None: l.append('%s=%s' % (attr, `value`)) continue return '%s(%s)' % (self.__class__.__name__, ', '.join(l)) def __eq__(self, other): if not isinstance(other, _ttinfo): return False if self.offset == other.offset and self.delta == other.delta and self.isdst == other.isdst and self.abbr == other.abbr and self.isstd == other.isstd: pass return self.isgmt == other.isgmt def __ne__(self, other): return not self.__eq__(other) def __getstate__(self): state = { } for name in self.__slots__: state[name] = getattr(self, name, None) return state def __setstate__(self, state): for name in self.__slots__: if name in state: setattr(self, name, state[name]) continue class tzfile(datetime.tzinfo): def __init__(self, fileobj): if isinstance(fileobj, basestring): self._filename = fileobj fileobj = open(fileobj) elif hasattr(fileobj, 'name'): self._filename = fileobj.name else: self._filename = `fileobj` if fileobj.read(4) != 'TZif': raise ValueError, 'magic not found' fileobj.read(4) != 'TZif' fileobj.read(16) (ttisgmtcnt, ttisstdcnt, leapcnt, timecnt, typecnt, charcnt) = struct.unpack('>6l', fileobj.read(24)) if timecnt: self._trans_list = struct.unpack('>%dl' % timecnt, fileobj.read(timecnt * 4)) else: self._trans_list = [] if timecnt: self._trans_idx = struct.unpack('>%dB' % timecnt, fileobj.read(timecnt)) else: self._trans_idx = [] ttinfo = [] for i in range(typecnt): ttinfo.append(struct.unpack('>lbb', fileobj.read(6))) abbr = fileobj.read(charcnt) if leapcnt: leap = struct.unpack('>%dl' % leapcnt * 2, fileobj.read(leapcnt * 8)) if ttisstdcnt: isstd = struct.unpack('>%db' % ttisstdcnt, fileobj.read(ttisstdcnt)) if ttisgmtcnt: isgmt = struct.unpack('>%db' % ttisgmtcnt, fileobj.read(ttisgmtcnt)) self._ttinfo_list = [] for i in range(typecnt): (gmtoff, isdst, abbrind) = ttinfo[i] gmtoff = ((gmtoff + 30) // 60) * 60 tti = _ttinfo() tti.offset = gmtoff tti.delta = datetime.timedelta(seconds = gmtoff) tti.isdst = isdst tti.abbr = abbr[abbrind:abbr.find('\x00', abbrind)] if ttisstdcnt > i: pass tti.isstd = isstd[i] != 0 if ttisgmtcnt > i: pass tti.isgmt = isgmt[i] != 0 self._ttinfo_list.append(tti) trans_idx = [] for idx in self._trans_idx: trans_idx.append(self._ttinfo_list[idx]) self._trans_idx = tuple(trans_idx) self._ttinfo_std = None self._ttinfo_dst = None self._ttinfo_before = None if self._ttinfo_list: if not self._trans_list: self._ttinfo_std = self._ttinfo_first = self._ttinfo_list[0] else: for i in range(timecnt - 1, -1, -1): tti = self._trans_idx[i] if not (self._ttinfo_std) and not (tti.isdst): self._ttinfo_std = tti elif not (self._ttinfo_dst) and tti.isdst: self._ttinfo_dst = tti if self._ttinfo_std and self._ttinfo_dst: break continue elif self._ttinfo_dst and not (self._ttinfo_std): self._ttinfo_std = self._ttinfo_dst for tti in self._ttinfo_list: if not tti.isdst: self._ttinfo_before = tti break continue else: self._ttinfo_before = self._ttinfo_list[0] laststdoffset = 0 self._trans_list = list(self._trans_list) for i in range(len(self._trans_list)): tti = self._trans_idx[i] if not tti.isdst: self._trans_list[i] += tti.offset laststdoffset = tti.offset continue self._trans_list[i] += laststdoffset self._trans_list = tuple(self._trans_list) def _find_ttinfo(self, dt, laststd = 0): timestamp = (dt.toordinal() - EPOCHORDINAL) * 86400 + dt.hour * 3600 + dt.minute * 60 + dt.second idx = 0 for trans in self._trans_list: if timestamp < trans: break idx += 1 else: return self._ttinfo_std if None == 0: return self._ttinfo_before if laststd: while idx > 0: tti = self._trans_idx[idx - 1] if not tti.isdst: return tti idx -= 1 continue tti.isdst return self._ttinfo_std laststd return self._trans_idx[idx - 1] def utcoffset(self, dt): if not self._ttinfo_std: return ZERO return self._find_ttinfo(dt).delta def dst(self, dt): if not self._ttinfo_dst: return ZERO tti = self._find_ttinfo(dt) if not tti.isdst: return ZERO return tti.delta - self._find_ttinfo(dt, laststd = 1).delta def tzname(self, dt): if not self._ttinfo_std: return None return self._find_ttinfo(dt).abbr def __eq__(self, other): if not isinstance(other, tzfile): return False if self._trans_list == other._trans_list and self._trans_idx == other._trans_idx: pass return self._ttinfo_list == other._ttinfo_list def __ne__(self, other): return not self.__eq__(other) def __repr__(self): return '%s(%s)' % (self.__class__.__name__, `self._filename`) def __reduce__(self): if not os.path.isfile(self._filename): raise ValueError, 'Unpickable %s class' % self.__class__.__name__ os.path.isfile(self._filename) return (self.__class__, (self._filename,)) class tzrange(datetime.tzinfo): def __init__(self, stdabbr, stdoffset = None, dstabbr = None, dstoffset = None, start = None, end = None): global relativedelta if not relativedelta: relativedelta = relativedelta import dateutil self._std_abbr = stdabbr self._dst_abbr = dstabbr if stdoffset is not None: self._std_offset = datetime.timedelta(seconds = stdoffset) else: self._std_offset = ZERO if dstoffset is not None: self._dst_offset = datetime.timedelta(seconds = dstoffset) elif dstabbr and stdoffset is not None: self._dst_offset = self._std_offset + datetime.timedelta(hours = +1) else: self._dst_offset = ZERO if dstabbr and start is None: self._start_delta = relativedelta.relativedelta(hours = +2, month = 4, day = 1, weekday = relativedelta.SU(+1)) else: self._start_delta = start if dstabbr and end is None: self._end_delta = relativedelta.relativedelta(hours = +1, month = 10, day = 31, weekday = relativedelta.SU(-1)) else: self._end_delta = end def utcoffset(self, dt): if self._isdst(dt): return self._dst_offset return self._std_offset def dst(self, dt): if self._isdst(dt): return self._dst_offset - self._std_offset return ZERO def tzname(self, dt): if self._isdst(dt): return self._dst_abbr return self._std_abbr def _isdst(self, dt): if not self._start_delta: return False year = datetime.datetime(dt.year, 1, 1) start = year + self._start_delta end = year + self._end_delta dt = dt.replace(tzinfo = None) if start < end: if dt >= start: pass return dt < end if not dt >= start: pass return dt < end def __eq__(self, other): if not isinstance(other, tzrange): return False if self._std_abbr == other._std_abbr and self._dst_abbr == other._dst_abbr and self._std_offset == other._std_offset and self._dst_offset == other._dst_offset and self._start_delta == other._start_delta: pass return self._end_delta == other._end_delta def __ne__(self, other): return not self.__eq__(other) def __repr__(self): return '%s(...)' % self.__class__.__name__ __reduce__ = object.__reduce__ class tzstr(tzrange): def __init__(self, s): global parser if not parser: parser = parser import dateutil self._s = s res = parser._parsetz(s) if res is None: raise ValueError, 'unknown string format' res is None if res.stdabbr in ('GMT', 'UTC'): res.stdoffset *= -1 tzrange.__init__(self, res.stdabbr, res.stdoffset, res.dstabbr, res.dstoffset, start = False, end = False) if not res.dstabbr: self._start_delta = None self._end_delta = None else: self._start_delta = self._delta(res.start) if self._start_delta: self._end_delta = self._delta(res.end, isend = 1) def _delta(self, x, isend = 0): kwargs = { } if x.month is not None: kwargs['month'] = x.month if x.weekday is not None: kwargs['weekday'] = relativedelta.weekday(x.weekday, x.week) if x.week > 0: kwargs['day'] = 1 else: kwargs['day'] = 31 elif x.day: kwargs['day'] = x.day elif x.yday is not None: kwargs['yearday'] = x.yday elif x.jyday is not None: kwargs['nlyearday'] = x.jyday if not kwargs: if not isend: kwargs['month'] = 4 kwargs['day'] = 1 kwargs['weekday'] = relativedelta.SU(+1) else: kwargs['month'] = 10 kwargs['day'] = 31 kwargs['weekday'] = relativedelta.SU(-1) if x.time is not None: kwargs['seconds'] = x.time else: kwargs['seconds'] = 7200 if isend: delta = self._dst_offset - self._std_offset kwargs['seconds'] -= delta.seconds + delta.days * 86400 return relativedelta.relativedelta(**kwargs) def __repr__(self): return '%s(%s)' % (self.__class__.__name__, `self._s`) class _tzicalvtzcomp: def __init__(self, tzoffsetfrom, tzoffsetto, isdst, tzname = None, rrule = None): self.tzoffsetfrom = datetime.timedelta(seconds = tzoffsetfrom) self.tzoffsetto = datetime.timedelta(seconds = tzoffsetto) self.tzoffsetdiff = self.tzoffsetto - self.tzoffsetfrom self.isdst = isdst self.tzname = tzname self.rrule = rrule class _tzicalvtz(datetime.tzinfo): def __init__(self, tzid, comps = []): self._tzid = tzid self._comps = comps self._cachedate = [] self._cachecomp = [] def _find_comp(self, dt): if len(self._comps) == 1: return self._comps[0] dt = dt.replace(tzinfo = None) try: return self._cachecomp[self._cachedate.index(dt)] except ValueError: len(self._comps) == 1 len(self._comps) == 1 except: len(self._comps) == 1 lastcomp = None lastcompdt = None for comp in self._comps: if not comp.isdst: compdt = comp.rrule.before(dt - comp.tzoffsetdiff, inc = True) else: compdt = comp.rrule.before(dt, inc = True) if compdt: if not lastcompdt or lastcompdt < compdt: lastcompdt = compdt lastcomp = comp continue if not lastcomp: for comp in self._comps: if not comp.isdst: lastcomp = comp break continue else: lastcomp = comp[0] self._cachedate.insert(0, dt) self._cachecomp.insert(0, lastcomp) if len(self._cachedate) > 10: self._cachedate.pop() self._cachecomp.pop() return lastcomp def utcoffset(self, dt): return self._find_comp(dt).tzoffsetto def dst(self, dt): comp = self._find_comp(dt) if comp.isdst: return comp.tzoffsetdiff return ZERO def tzname(self, dt): return self._find_comp(dt).tzname def __repr__(self): return '<tzicalvtz %s>' % `self._tzid` __reduce__ = object.__reduce__ class tzical: def __init__(self, fileobj): global rrule if not rrule: rrule = rrule import dateutil if isinstance(fileobj, basestring): self._s = fileobj fileobj = open(fileobj) elif hasattr(fileobj, 'name'): self._s = fileobj.name else: self._s = `fileobj` self._vtz = { } self._parse_rfc(fileobj.read()) def keys(self): return self._vtz.keys() def get(self, tzid = None): if tzid is None: keys = self._vtz.keys() if len(keys) == 0: raise ValueError, 'no timezones defined' len(keys) == 0 if len(keys) > 1: raise ValueError, 'more than one timezone available' len(keys) > 1 tzid = keys[0] return self._vtz.get(tzid) def _parse_offset(self, s): s = s.strip() if not s: raise ValueError, 'empty offset' s if s[0] in ('+', '-'): signal = (-1, +1)[s[0] == '+'] s = s[1:] else: signal = +1 if len(s) == 4: return (int(s[:2]) * 3600 + int(s[2:]) * 60) * signal if len(s) == 6: return (int(s[:2]) * 3600 + int(s[2:4]) * 60 + int(s[4:])) * signal raise ValueError, 'invalid offset: ' + s def _parse_rfc(self, s): lines = s.splitlines() if not lines: raise ValueError, 'empty string' lines i = 0 while i < len(lines): line = lines[i].rstrip() if not line: del lines[i] continue if i > 0 and line[0] == ' ': lines[i - 1] += line[1:] del lines[i] continue i += 1 tzid = None comps = [] invtz = False comptype = None for line in lines: if not line: continue (name, value) = line.split(':', 1) parms = name.split(';') if not parms: raise ValueError, 'empty property name' parms name = parms[0].upper() parms = parms[1:] if invtz: if name == 'BEGIN': if value in ('STANDARD', 'DAYLIGHT'): pass else: raise ValueError, 'unknown component: ' + value comptype = value in ('STANDARD', 'DAYLIGHT') founddtstart = False tzoffsetfrom = None tzoffsetto = None rrulelines = [] tzname = None elif name == 'END': if value == 'VTIMEZONE': if comptype: raise ValueError, 'component not closed: ' + comptype comptype if not tzid: raise ValueError, 'mandatory TZID not found' tzid if not comps: raise ValueError, 'at least one component is needed' comps self._vtz[tzid] = _tzicalvtz(tzid, comps) invtz = False elif value == comptype: if not founddtstart: raise ValueError, 'mandatory DTSTART not found' founddtstart if tzoffsetfrom is None: raise ValueError, 'mandatory TZOFFSETFROM not found' tzoffsetfrom is None if tzoffsetto is None: raise ValueError, 'mandatory TZOFFSETFROM not found' tzoffsetto is None rr = None if rrulelines: rr = rrule.rrulestr('\n'.join(rrulelines), compatible = True, ignoretz = True, cache = True) comp = _tzicalvtzcomp(tzoffsetfrom, tzoffsetto, comptype == 'DAYLIGHT', tzname, rr) comps.append(comp) comptype = None else: raise ValueError, 'invalid component end: ' + value value == 'VTIMEZONE' if comptype: if name == 'DTSTART': rrulelines.append(line) founddtstart = True elif name in ('RRULE', 'RDATE', 'EXRULE', 'EXDATE'): rrulelines.append(line) elif name == 'TZOFFSETFROM': if parms: raise ValueError, 'unsupported %s parm: %s ' % (name, parms[0]) parms tzoffsetfrom = self._parse_offset(value) elif name == 'TZOFFSETTO': if parms: raise ValueError, 'unsupported TZOFFSETTO parm: ' + parms[0] parms tzoffsetto = self._parse_offset(value) elif name == 'TZNAME': if parms: raise ValueError, 'unsupported TZNAME parm: ' + parms[0] parms tzname = value elif name == 'COMMENT': pass else: raise ValueError, 'unsupported property: ' + name name == 'DTSTART' if name == 'TZID': if parms: raise ValueError, 'unsupported TZID parm: ' + parms[0] parms tzid = value elif name in ('TZURL', 'LAST-MODIFIED', 'COMMENT'): pass else: raise ValueError, 'unsupported property: ' + name name == 'TZID' if name == 'BEGIN' and value == 'VTIMEZONE': tzid = None comps = [] invtz = True continue def __repr__(self): return '%s(%s)' % (self.__class__.__name__, `self._s`) if sys.platform != 'win32': TZFILES = [ '/etc/localtime', 'localtime'] TZPATHS = [ '/usr/share/zoneinfo', '/usr/lib/zoneinfo', '/etc/zoneinfo'] else: TZFILES = [] TZPATHS = [] def gettz(name = None): tz = None if not name: try: name = os.environ['TZ'] except KeyError: pass except: None<EXCEPTION MATCH>KeyError None<EXCEPTION MATCH>KeyError if name is None or name == ':': for filepath in TZFILES: if not os.path.isabs(filepath): filename = filepath for path in TZPATHS: filepath = os.path.join(path, filename) if os.path.isfile(filepath): break continue if os.path.isfile(filepath): try: tz = tzfile(filepath) except (IOError, OSError, ValueError): pass except: None<EXCEPTION MATCH>(IOError, OSError, ValueError) None<EXCEPTION MATCH>(IOError, OSError, ValueError) else: tz = tzlocal() elif name.startswith(':'): name = name[:-1] if os.path.isabs(name): if os.path.isfile(name): tz = tzfile(name) else: tz = None else: for path in TZPATHS: filepath = os.path.join(path, name) if not os.path.isfile(filepath): filepath = filepath.replace(' ', '_') if not os.path.isfile(filepath): continue try: tz = tzfile(filepath) continue except (IOError, OSError, ValueError): continue else: tz = None if tzwin: try: tz = tzwin(name) except OSError: None<EXCEPTION MATCH>(IOError, OSError, ValueError) None<EXCEPTION MATCH>(IOError, OSError, ValueError) except: None<EXCEPTION MATCH>(IOError, OSError, ValueError)<EXCEPTION MATCH>OSError if not tz: gettz = gettz import dateutil.zoneinfo tz = gettz(name) if not tz: for c in name: if c in '0123456789': try: tz = tzstr(name) except ValueError: pass break continue elif name in ('GMT', 'UTC'): tz = tzutc() elif name in time.tzname: tz = tzlocal() return tz