jpayne@69: ''' jpayne@69: $Id: tzfile.py,v 1.8 2004/06/03 00:15:24 zenzen Exp $ jpayne@69: ''' jpayne@69: jpayne@69: from datetime import datetime jpayne@69: from struct import unpack, calcsize jpayne@69: jpayne@69: from pytz.tzinfo import StaticTzInfo, DstTzInfo, memorized_ttinfo jpayne@69: from pytz.tzinfo import memorized_datetime, memorized_timedelta jpayne@69: jpayne@69: jpayne@69: def _byte_string(s): jpayne@69: """Cast a string or byte string to an ASCII byte string.""" jpayne@69: return s.encode('ASCII') jpayne@69: jpayne@69: _NULL = _byte_string('\0') jpayne@69: jpayne@69: jpayne@69: def _std_string(s): jpayne@69: """Cast a string or byte string to an ASCII string.""" jpayne@69: return str(s.decode('ASCII')) jpayne@69: jpayne@69: jpayne@69: def build_tzinfo(zone, fp): jpayne@69: head_fmt = '>4s c 15x 6l' jpayne@69: head_size = calcsize(head_fmt) jpayne@69: (magic, format, ttisgmtcnt, ttisstdcnt, leapcnt, timecnt, jpayne@69: typecnt, charcnt) = unpack(head_fmt, fp.read(head_size)) jpayne@69: jpayne@69: # Make sure it is a tzfile(5) file jpayne@69: assert magic == _byte_string('TZif'), 'Got magic %s' % repr(magic) jpayne@69: jpayne@69: # Read out the transition times, localtime indices and ttinfo structures. jpayne@69: data_fmt = '>%(timecnt)dl %(timecnt)dB %(ttinfo)s %(charcnt)ds' % dict( jpayne@69: timecnt=timecnt, ttinfo='lBB' * typecnt, charcnt=charcnt) jpayne@69: data_size = calcsize(data_fmt) jpayne@69: data = unpack(data_fmt, fp.read(data_size)) jpayne@69: jpayne@69: # make sure we unpacked the right number of values jpayne@69: assert len(data) == 2 * timecnt + 3 * typecnt + 1 jpayne@69: transitions = [memorized_datetime(trans) jpayne@69: for trans in data[:timecnt]] jpayne@69: lindexes = list(data[timecnt:2 * timecnt]) jpayne@69: ttinfo_raw = data[2 * timecnt:-1] jpayne@69: tznames_raw = data[-1] jpayne@69: del data jpayne@69: jpayne@69: # Process ttinfo into separate structs jpayne@69: ttinfo = [] jpayne@69: tznames = {} jpayne@69: i = 0 jpayne@69: while i < len(ttinfo_raw): jpayne@69: # have we looked up this timezone name yet? jpayne@69: tzname_offset = ttinfo_raw[i + 2] jpayne@69: if tzname_offset not in tznames: jpayne@69: nul = tznames_raw.find(_NULL, tzname_offset) jpayne@69: if nul < 0: jpayne@69: nul = len(tznames_raw) jpayne@69: tznames[tzname_offset] = _std_string( jpayne@69: tznames_raw[tzname_offset:nul]) jpayne@69: ttinfo.append((ttinfo_raw[i], jpayne@69: bool(ttinfo_raw[i + 1]), jpayne@69: tznames[tzname_offset])) jpayne@69: i += 3 jpayne@69: jpayne@69: # Now build the timezone object jpayne@69: if len(ttinfo) == 1 or len(transitions) == 0: jpayne@69: ttinfo[0][0], ttinfo[0][2] jpayne@69: cls = type(zone, (StaticTzInfo,), dict( jpayne@69: zone=zone, jpayne@69: _utcoffset=memorized_timedelta(ttinfo[0][0]), jpayne@69: _tzname=ttinfo[0][2])) jpayne@69: else: jpayne@69: # Early dates use the first standard time ttinfo jpayne@69: i = 0 jpayne@69: while ttinfo[i][1]: jpayne@69: i += 1 jpayne@69: if ttinfo[i] == ttinfo[lindexes[0]]: jpayne@69: transitions[0] = datetime.min jpayne@69: else: jpayne@69: transitions.insert(0, datetime.min) jpayne@69: lindexes.insert(0, i) jpayne@69: jpayne@69: # calculate transition info jpayne@69: transition_info = [] jpayne@69: for i in range(len(transitions)): jpayne@69: inf = ttinfo[lindexes[i]] jpayne@69: utcoffset = inf[0] jpayne@69: if not inf[1]: jpayne@69: dst = 0 jpayne@69: else: jpayne@69: for j in range(i - 1, -1, -1): jpayne@69: prev_inf = ttinfo[lindexes[j]] jpayne@69: if not prev_inf[1]: jpayne@69: break jpayne@69: dst = inf[0] - prev_inf[0] # dst offset jpayne@69: jpayne@69: # Bad dst? Look further. DST > 24 hours happens when jpayne@69: # a timzone has moved across the international dateline. jpayne@69: if dst <= 0 or dst > 3600 * 3: jpayne@69: for j in range(i + 1, len(transitions)): jpayne@69: stdinf = ttinfo[lindexes[j]] jpayne@69: if not stdinf[1]: jpayne@69: dst = inf[0] - stdinf[0] jpayne@69: if dst > 0: jpayne@69: break # Found a useful std time. jpayne@69: jpayne@69: tzname = inf[2] jpayne@69: jpayne@69: # Round utcoffset and dst to the nearest minute or the jpayne@69: # datetime library will complain. Conversions to these timezones jpayne@69: # might be up to plus or minus 30 seconds out, but it is jpayne@69: # the best we can do. jpayne@69: utcoffset = int((utcoffset + 30) // 60) * 60 jpayne@69: dst = int((dst + 30) // 60) * 60 jpayne@69: transition_info.append(memorized_ttinfo(utcoffset, dst, tzname)) jpayne@69: jpayne@69: cls = type(zone, (DstTzInfo,), dict( jpayne@69: zone=zone, jpayne@69: _utc_transition_times=transitions, jpayne@69: _transition_info=transition_info)) jpayne@69: jpayne@69: return cls() jpayne@69: jpayne@69: if __name__ == '__main__': jpayne@69: import os.path jpayne@69: from pprint import pprint jpayne@69: base = os.path.join(os.path.dirname(__file__), 'zoneinfo') jpayne@69: tz = build_tzinfo('Australia/Melbourne', jpayne@69: open(os.path.join(base, 'Australia', 'Melbourne'), 'rb')) jpayne@69: tz = build_tzinfo('US/Eastern', jpayne@69: open(os.path.join(base, 'US', 'Eastern'), 'rb')) jpayne@69: pprint(tz._utc_transition_times)