You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tzfile.py 4.6KB

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