tzfile.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/env python
  2. '''
  3. $Id: tzfile.py,v 1.8 2004/06/03 00:15:24 zenzen Exp $
  4. '''
  5. try:
  6. from cStringIO import StringIO
  7. except ImportError:
  8. from io import StringIO
  9. from datetime import datetime, timedelta
  10. from struct import unpack, calcsize
  11. from pytz.tzinfo import StaticTzInfo, DstTzInfo, memorized_ttinfo
  12. from pytz.tzinfo import memorized_datetime, memorized_timedelta
  13. def _byte_string(s):
  14. """Cast a string or byte string to an ASCII byte string."""
  15. return s.encode('ASCII')
  16. _NULL = _byte_string('\0')
  17. def _std_string(s):
  18. """Cast a string or byte string to an ASCII string."""
  19. return str(s.decode('ASCII'))
  20. def build_tzinfo(zone, fp):
  21. head_fmt = '>4s c 15x 6l'
  22. head_size = calcsize(head_fmt)
  23. (magic, format, ttisgmtcnt, ttisstdcnt,leapcnt, timecnt,
  24. typecnt, charcnt) = unpack(head_fmt, fp.read(head_size))
  25. # Make sure it is a tzfile(5) file
  26. assert magic == _byte_string('TZif'), 'Got magic %s' % repr(magic)
  27. # Read out the transition times, localtime indices and ttinfo structures.
  28. data_fmt = '>%(timecnt)dl %(timecnt)dB %(ttinfo)s %(charcnt)ds' % dict(
  29. timecnt=timecnt, ttinfo='lBB'*typecnt, charcnt=charcnt)
  30. data_size = calcsize(data_fmt)
  31. data = unpack(data_fmt, fp.read(data_size))
  32. # make sure we unpacked the right number of values
  33. assert len(data) == 2 * timecnt + 3 * typecnt + 1
  34. transitions = [memorized_datetime(trans)
  35. for trans in data[:timecnt]]
  36. lindexes = list(data[timecnt:2 * timecnt])
  37. ttinfo_raw = data[2 * timecnt:-1]
  38. tznames_raw = data[-1]
  39. del data
  40. # Process ttinfo into separate structs
  41. ttinfo = []
  42. tznames = {}
  43. i = 0
  44. while i < len(ttinfo_raw):
  45. # have we looked up this timezone name yet?
  46. tzname_offset = ttinfo_raw[i+2]
  47. if tzname_offset not in tznames:
  48. nul = tznames_raw.find(_NULL, tzname_offset)
  49. if nul < 0:
  50. nul = len(tznames_raw)
  51. tznames[tzname_offset] = _std_string(
  52. tznames_raw[tzname_offset:nul])
  53. ttinfo.append((ttinfo_raw[i],
  54. bool(ttinfo_raw[i+1]),
  55. tznames[tzname_offset]))
  56. i += 3
  57. # Now build the timezone object
  58. if len(ttinfo) ==1 or len(transitions) == 0:
  59. ttinfo[0][0], ttinfo[0][2]
  60. cls = type(zone, (StaticTzInfo,), dict(
  61. zone=zone,
  62. _utcoffset=memorized_timedelta(ttinfo[0][0]),
  63. _tzname=ttinfo[0][2]))
  64. else:
  65. # Early dates use the first standard time ttinfo
  66. i = 0
  67. while ttinfo[i][1]:
  68. i += 1
  69. if ttinfo[i] == ttinfo[lindexes[0]]:
  70. transitions[0] = datetime.min
  71. else:
  72. transitions.insert(0, datetime.min)
  73. lindexes.insert(0, i)
  74. # calculate transition info
  75. transition_info = []
  76. for i in range(len(transitions)):
  77. inf = ttinfo[lindexes[i]]
  78. utcoffset = inf[0]
  79. if not inf[1]:
  80. dst = 0
  81. else:
  82. for j in range(i-1, -1, -1):
  83. prev_inf = ttinfo[lindexes[j]]
  84. if not prev_inf[1]:
  85. break
  86. dst = inf[0] - prev_inf[0] # dst offset
  87. # Bad dst? Look further. DST > 24 hours happens when
  88. # a timzone has moved across the international dateline.
  89. if dst <= 0 or dst > 3600*3:
  90. for j in range(i+1, len(transitions)):
  91. stdinf = ttinfo[lindexes[j]]
  92. if not stdinf[1]:
  93. dst = inf[0] - stdinf[0]
  94. if dst > 0:
  95. break # Found a useful std time.
  96. tzname = inf[2]
  97. # Round utcoffset and dst to the nearest minute or the
  98. # datetime library will complain. Conversions to these timezones
  99. # might be up to plus or minus 30 seconds out, but it is
  100. # the best we can do.
  101. utcoffset = int((utcoffset + 30) // 60) * 60
  102. dst = int((dst + 30) // 60) * 60
  103. transition_info.append(memorized_ttinfo(utcoffset, dst, tzname))
  104. cls = type(zone, (DstTzInfo,), dict(
  105. zone=zone,
  106. _utc_transition_times=transitions,
  107. _transition_info=transition_info))
  108. return cls()
  109. if __name__ == '__main__':
  110. import os.path
  111. from pprint import pprint
  112. base = os.path.join(os.path.dirname(__file__), 'zoneinfo')
  113. tz = build_tzinfo('Australia/Melbourne',
  114. open(os.path.join(base,'Australia','Melbourne'), 'rb'))
  115. tz = build_tzinfo('US/Eastern',
  116. open(os.path.join(base,'US','Eastern'), 'rb'))
  117. pprint(tz._utc_transition_times)
  118. #print tz.asPython(4)
  119. #print tz.transitions_mapping