pytz.txt 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. Pytz Support
  2. ============
  3. Allows the pytz package to be used for time zone information. The
  4. advantage of using pytz is that it has a more complete and up to date
  5. time zone and daylight savings time database.
  6. Usage
  7. -----
  8. You don't have to do anything special to make it work.
  9. >>> from DateTime import DateTime, Timezones
  10. >>> d = DateTime('March 11, 2007 US/Eastern')
  11. Daylight Savings
  12. ----------------
  13. In 2007 daylight savings time in the US was changed. The Energy Policy
  14. Act of 2005 mandates that DST will start on the second Sunday in March
  15. and end on the first Sunday in November.
  16. In 2007, the start and stop dates are March 11 and November 4,
  17. respectively. These dates are different from previous DST start and
  18. stop dates. In 2006, the dates were the first Sunday in April (April
  19. 2, 2006) and the last Sunday in October (October 29, 2006).
  20. Let's make sure that DateTime can deal with this, since the primary
  21. motivation to use pytz for time zone information is the fact that it
  22. is kept up to date with daylight savings changes.
  23. >>> DateTime('March 11, 2007 US/Eastern').tzoffset()
  24. -18000
  25. >>> DateTime('March 12, 2007 US/Eastern').tzoffset()
  26. -14400
  27. >>> DateTime('November 4, 2007 US/Eastern').tzoffset()
  28. -14400
  29. >>> DateTime('November 5, 2007 US/Eastern').tzoffset()
  30. -18000
  31. Let's compare this to 2006.
  32. >>> DateTime('April 2, 2006 US/Eastern').tzoffset()
  33. -18000
  34. >>> DateTime('April 3, 2006 US/Eastern').tzoffset()
  35. -14400
  36. >>> DateTime('October 29, 2006 US/Eastern').tzoffset()
  37. -14400
  38. >>> DateTime('October 30, 2006 US/Eastern').tzoffset()
  39. -18000
  40. Time Zones
  41. ---------
  42. DateTime can use pytz's large database of time zones. Here are some
  43. examples:
  44. >>> d = DateTime('Pacific/Kwajalein')
  45. >>> d = DateTime('America/Shiprock')
  46. >>> d = DateTime('Africa/Ouagadougou')
  47. Of course pytz doesn't know about everything.
  48. >>> from DateTime.interfaces import SyntaxError
  49. >>> try:
  50. ... d = DateTime('July 21, 1969 Moon/Eastern')
  51. ... print('fail')
  52. ... except SyntaxError:
  53. ... print('ok')
  54. ok
  55. You can still use zone names that DateTime defines that aren't part of
  56. the pytz database.
  57. >>> d = DateTime('eet')
  58. >>> d = DateTime('iceland')
  59. These time zones use DateTimes database. So it's preferable to use the
  60. official time zone name.
  61. One trickiness is that DateTime supports some zone name
  62. abbreviations. Some of these map to pytz names, so these abbreviations
  63. will give you time zone date from pytz. Notable among abbreviations
  64. that work this way are 'est', 'cst', 'mst', and 'pst'.
  65. Let's verify that 'est' picks up the 2007 daylight savings time changes.
  66. >>> DateTime('March 11, 2007 est').tzoffset()
  67. -18000
  68. >>> DateTime('March 12, 2007 est').tzoffset()
  69. -14400
  70. >>> DateTime('November 4, 2007 est').tzoffset()
  71. -14400
  72. >>> DateTime('November 5, 2007 est').tzoffset()
  73. -18000
  74. You can get a list of time zones supported by calling the Timezones() function.
  75. >>> Timezones() #doctest: +ELLIPSIS
  76. ['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', ...]
  77. Note that you can mess with this list without hurting things.
  78. >>> t = Timezones()
  79. >>> t.remove('US/Eastern')
  80. >>> d = DateTime('US/Eastern')
  81. Internal Components
  82. -------------------
  83. The following are tests of internal components.
  84. Cache
  85. ~~~~~
  86. The DateTime class uses a new time zone cache.
  87. >>> from DateTime.DateTime import _TZINFO
  88. >>> _TZINFO #doctest: +ELLIPSIS
  89. <DateTime.pytz_support.PytzCache ...>
  90. The cache maps time zone names to time zone instances.
  91. >>> cache = _TZINFO
  92. >>> tz = cache['GMT+730']
  93. >>> tz = cache['US/Mountain']
  94. The cache also must provide a few attributes for use by the DateTime
  95. class.
  96. The _zlst attribute is a list of supported time zone names.
  97. >>> cache._zlst #doctest: +ELLIPSIS
  98. ['Africa/Abidjan'... 'Africa/Accra'... 'IDLE'... 'NZST'... 'NZT'...]
  99. The _zidx attribute is a list of lower-case and possibly abbreviated
  100. time zone names that can be mapped to offical zone names.
  101. >>> 'australia/yancowinna' in cache._zidx
  102. True
  103. >>> 'europe/isle_of_man' in cache._zidx
  104. True
  105. >>> 'gmt+0500' in cache._zidx
  106. True
  107. Note that there are more items in _zidx than in _zlst since there are
  108. multiple names for some time zones.
  109. >>> len(cache._zidx) > len(cache._zlst)
  110. True
  111. Each entry in _zlst should also be present in _zidx in lower case form.
  112. >>> for name in cache._zlst:
  113. ... if not name.lower() in cache._zidx:
  114. ... print("Error %s not in _zidx" % name.lower())
  115. The _zmap attribute maps the names in _zidx to official names in _zlst.
  116. >>> cache._zmap['africa/abidjan']
  117. 'Africa/Abidjan'
  118. >>> cache._zmap['gmt+1']
  119. 'GMT+1'
  120. >>> cache._zmap['gmt+0100']
  121. 'GMT+1'
  122. >>> cache._zmap['utc']
  123. 'UTC'
  124. Let's make sure that _zmap and _zidx agree.
  125. >>> idx = set(cache._zidx)
  126. >>> keys = set(cache._zmap.keys())
  127. >>> idx == keys
  128. True
  129. Timezone objects
  130. ~~~~~~~~~~~~~~~~
  131. The timezone instances have only one public method info(). It returns
  132. a tuple of (offset, is_dst, name). The method takes a timestamp, which
  133. is used to determine dst information.
  134. >>> t1 = DateTime('November 4, 00:00 2007 US/Mountain').timeTime()
  135. >>> t2 = DateTime('November 4, 02:00 2007 US/Mountain').timeTime()
  136. >>> tz.info(t1)
  137. (-21600, 1, 'MDT')
  138. >>> tz.info(t2)
  139. (-25200, 0, 'MST')
  140. If you don't pass any arguments to info it provides daylight savings
  141. time information as of today.
  142. >>> tz.info() in ((-21600, 1, 'MDT'), (-25200, 0, 'MST'))
  143. True