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.
 
 
 

231 lines
11 KiB

  1. #!/usr/bin/python2.4
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2010 Google Inc. All Rights Reserved.
  5. #
  6. """
  7. Common time zone acronyms/abbreviations for use with the datetime_tz module.
  8. *WARNING*: There are lots of caveats when using this module which are listed
  9. below.
  10. CAVEAT 1: The acronyms/abbreviations are not globally unique, they are not even
  11. unique within a region. For example, EST can mean any of,
  12. Eastern Standard Time in Australia (which is 10 hour ahead of UTC)
  13. Eastern Standard Time in North America (which is 5 hours behind UTC)
  14. Where there are two abbreviations the more popular one will appear in the all
  15. dictionary, while the less common one will only appear in that countries region
  16. dictionary. IE If using all, EST will be mapped to Eastern Standard Time in
  17. North America.
  18. CAVEAT 2: Many of the acronyms don't map to a neat Oslon timezones. For example,
  19. Eastern European Summer Time (EEDT) is used by many different countries in
  20. Europe *at different times*! If the acronym does not map neatly to one zone it
  21. is mapped to the Etc/GMT+-XX Oslon zone. This means that any date manipulations
  22. can end up with idiot things like summer time in the middle of winter.
  23. CAVEAT 3: The Summer/Standard time difference is really important! For an hour
  24. each year it is needed to determine which time you are actually talking about.
  25. 2002-10-27 01:20:00 EST != 2002-10-27 01:20:00 EDT
  26. """
  27. import datetime
  28. import pytz
  29. import pytz.tzfile
  30. class tzabbr(datetime.tzinfo):
  31. """A timezone abbreviation.
  32. *WARNING*: This is not a tzinfo implementation! Trying to use this as tzinfo
  33. object will result in failure. We inherit from datetime.tzinfo so we can get
  34. through the dateutil checks.
  35. """
  36. pass
  37. # A "marker" tzinfo object which is used to signify an unknown timezone.
  38. unknown = datetime.tzinfo(0)
  39. regions = {'all': {}, 'military': {}}
  40. # Create a special alias for the all and military regions
  41. all = regions['all']
  42. military = regions['military']
  43. def tzabbr_register(abbr, name, region, zone, dst):
  44. """Register a new timezone abbreviation in the global registry.
  45. If another abbreviation with the same name has already been registered it new
  46. abbreviation will only be registered in region specific dictionary.
  47. """
  48. newabbr = tzabbr()
  49. newabbr.abbr = abbr
  50. newabbr.name = name
  51. newabbr.region = region
  52. newabbr.zone = zone
  53. newabbr.dst = dst
  54. if abbr not in all:
  55. all[abbr] = newabbr
  56. if not region in regions:
  57. regions[region] = {}
  58. assert abbr not in regions[region]
  59. regions[region][abbr] = newabbr
  60. def tzinfos_create(use_region):
  61. abbrs = regions[use_region]
  62. def tzinfos(abbr, offset):
  63. if abbr:
  64. if abbr in abbrs:
  65. result = abbrs[abbr]
  66. if offset:
  67. # FIXME: Check the offset matches the abbreviation we just selected.
  68. pass
  69. return result
  70. else:
  71. raise ValueError("Unknown timezone found %s" % abbr)
  72. if offset == 0:
  73. return pytz.utc
  74. if offset:
  75. return pytz.FixedOffset(offset/60)
  76. return unknown
  77. return tzinfos
  78. # Create a special alias for the all tzinfos
  79. tzinfos = tzinfos_create('all')
  80. # Create the abbreviations.
  81. # *WARNING*: Order matters!
  82. tzabbr_register("A", "Alpha Time Zone", "Military", "Etc/GMT-1", False)
  83. tzabbr_register("ACDT", "Australian Central Daylight Time", "Australia",
  84. "Australia/Adelaide", True)
  85. tzabbr_register("ACST", "Australian Central Standard Time", "Australia",
  86. "Australia/Adelaide", False)
  87. tzabbr_register("ADT", "Atlantic Daylight Time", "North America",
  88. "America/Halifax", True)
  89. tzabbr_register("AEDT", "Australian Eastern Daylight Time", "Australia",
  90. "Australia/Sydney", True)
  91. tzabbr_register("AEST", "Australian Eastern Standard Time", "Australia",
  92. "Australia/Sydney", False)
  93. tzabbr_register("AKDT", "Alaska Daylight Time", "North America",
  94. "US/Alaska", True)
  95. tzabbr_register("AKST", "Alaska Standard Time", "North America",
  96. "US/Alaska", False)
  97. tzabbr_register("AST", "Atlantic Standard Time", "North America",
  98. "America/Halifax", False)
  99. tzabbr_register("AWDT", "Australian Western Daylight Time", "Australia",
  100. "Australia/West", True)
  101. tzabbr_register("AWST", "Australian Western Standard Time", "Australia",
  102. "Australia/West", False)
  103. tzabbr_register("B", "Bravo Time Zone", "Military", "Etc/GMT-2", False)
  104. tzabbr_register("BST", "British Summer Time", "Europe", "Europe/London", True)
  105. tzabbr_register("C", "Charlie Time Zone", "Military", "Etc/GMT-2", False)
  106. tzabbr_register("CDT", "Central Daylight Time", "North America",
  107. "US/Central", True)
  108. tzabbr_register("CEDT", "Central European Daylight Time", "Europe",
  109. "Etc/GMT+2", True)
  110. tzabbr_register("CEST", "Central European Summer Time", "Europe",
  111. "Etc/GMT+2", True)
  112. tzabbr_register("CET", "Central European Time", "Europe", "Etc/GMT+1", False)
  113. tzabbr_register("CST", "Central Standard Time", "North America",
  114. "US/Central", False)
  115. tzabbr_register("CXT", "Christmas Island Time", "Australia",
  116. "Indian/Christmas", False)
  117. tzabbr_register("D", "Delta Time Zone", "Military", "Etc/GMT-2", False)
  118. tzabbr_register("E", "Echo Time Zone", "Military", "Etc/GMT-2", False)
  119. tzabbr_register("EDT", "Eastern Daylight Time", "North America",
  120. "US/Eastern", True)
  121. tzabbr_register("EEDT", "Eastern European Daylight Time", "Europe",
  122. "Etc/GMT+3", True)
  123. tzabbr_register("EEST", "Eastern European Summer Time", "Europe",
  124. "Etc/GMT+3", True)
  125. tzabbr_register("EET", "Eastern European Time", "Europe", "Etc/GMT+2", False)
  126. tzabbr_register("EST", "Eastern Standard Time", "North America",
  127. "US/Eastern", False)
  128. tzabbr_register("F", "Foxtrot Time Zone", "Military", "Etc/GMT-6", False)
  129. tzabbr_register("G", "Golf Time Zone", "Military", "Etc/GMT-7", False)
  130. tzabbr_register("GMT", "Greenwich Mean Time", "Europe", pytz.utc, False)
  131. tzabbr_register("H", "Hotel Time Zone", "Military", "Etc/GMT-8", False)
  132. #tzabbr_register("HAA", u"Heure Avancée de l'Atlantique", u"North America", u"UTC - 3 hours")
  133. #tzabbr_register("HAC", u"Heure Avancée du Centre", u"North America", u"UTC - 5 hours")
  134. tzabbr_register("HADT", "Hawaii-Aleutian Daylight Time", "North America",
  135. "Pacific/Honolulu", True)
  136. #tzabbr_register("HAE", u"Heure Avancée de l'Est", u"North America", u"UTC - 4 hours")
  137. #tzabbr_register("HAP", u"Heure Avancée du Pacifique", u"North America", u"UTC - 7 hours")
  138. #tzabbr_register("HAR", u"Heure Avancée des Rocheuses", u"North America", u"UTC - 6 hours")
  139. tzabbr_register("HAST", "Hawaii-Aleutian Standard Time", "North America",
  140. "Pacific/Honolulu", False)
  141. #tzabbr_register("HAT", u"Heure Avancée de Terre-Neuve", u"North America", u"UTC - 2:30 hours")
  142. #tzabbr_register("HAY", u"Heure Avancée du Yukon", u"North America", u"UTC - 8 hours")
  143. tzabbr_register("HDT", "Hawaii Daylight Time", "North America",
  144. "Pacific/Honolulu", True)
  145. #tzabbr_register("HNA", u"Heure Normale de l'Atlantique", u"North America", u"UTC - 4 hours")
  146. #tzabbr_register("HNC", u"Heure Normale du Centre", u"North America", u"UTC - 6 hours")
  147. #tzabbr_register("HNE", u"Heure Normale de l'Est", u"North America", u"UTC - 5 hours")
  148. #tzabbr_register("HNP", u"Heure Normale du Pacifique", u"North America", u"UTC - 8 hours")
  149. #tzabbr_register("HNR", u"Heure Normale des Rocheuses", u"North America", u"UTC - 7 hours")
  150. #tzabbr_register("HNT", u"Heure Normale de Terre-Neuve", u"North America", u"UTC - 3:30 hours")
  151. #tzabbr_register("HNY", u"Heure Normale du Yukon", u"North America", u"UTC - 9 hours")
  152. tzabbr_register("HST", "Hawaii Standard Time", "North America",
  153. "Pacific/Honolulu", False)
  154. tzabbr_register("I", "India Time Zone", "Military", "Etc/GMT-9", False)
  155. tzabbr_register("IST", "Irish Summer Time", "Europe", "Europe/Dublin", True)
  156. tzabbr_register("K", "Kilo Time Zone", "Military", "Etc/GMT-10", False)
  157. tzabbr_register("L", "Lima Time Zone", "Military", "Etc/GMT-11", False)
  158. tzabbr_register("M", "Mike Time Zone", "Military", "Etc/GMT-12", False)
  159. tzabbr_register("MDT", "Mountain Daylight Time", "North America",
  160. "US/Mountain", True)
  161. #tzabbr_register("MESZ", u"Mitteleuroäische Sommerzeit", u"Europe", u"UTC + 2 hours")
  162. #tzabbr_register("MEZ", u"Mitteleuropäische Zeit", u"Europe", u"UTC + 1 hour")
  163. tzabbr_register("MSD", "Moscow Daylight Time", "Europe",
  164. "Europe/Moscow", True)
  165. tzabbr_register("MSK", "Moscow Standard Time", "Europe",
  166. "Europe/Moscow", False)
  167. tzabbr_register("MST", "Mountain Standard Time", "North America",
  168. "US/Mountain", False)
  169. tzabbr_register("N", "November Time Zone", "Military", "Etc/GMT+1", False)
  170. tzabbr_register("NDT", "Newfoundland Daylight Time", "North America",
  171. "America/St_Johns", True)
  172. tzabbr_register("NFT", "Norfolk (Island) Time", "Australia",
  173. "Pacific/Norfolk", False)
  174. tzabbr_register("NST", "Newfoundland Standard Time", "North America",
  175. "America/St_Johns", False)
  176. tzabbr_register("O", "Oscar Time Zone", "Military", "Etc/GMT+2", False)
  177. tzabbr_register("P", "Papa Time Zone", "Military", "Etc/GMT+3", False)
  178. tzabbr_register("PDT", "Pacific Daylight Time", "North America",
  179. "US/Pacific", True)
  180. tzabbr_register("PST", "Pacific Standard Time", "North America",
  181. "US/Pacific", False)
  182. tzabbr_register("Q", "Quebec Time Zone", "Military", "Etc/GMT+4", False)
  183. tzabbr_register("R", "Romeo Time Zone", "Military", "Etc/GMT+5", False)
  184. tzabbr_register("S", "Sierra Time Zone", "Military", "Etc/GMT+6", False)
  185. tzabbr_register("T", "Tango Time Zone", "Military", "Etc/GMT+7", False)
  186. tzabbr_register("U", "Uniform Time Zone", "Military", "Etc/GMT+8", False)
  187. tzabbr_register("UTC", "Coordinated Universal Time", "Europe",
  188. pytz.utc, False)
  189. tzabbr_register("V", "Victor Time Zone", "Military", "Etc/GMT+9", False)
  190. tzabbr_register("W", "Whiskey Time Zone", "Military", "Etc/GMT+10", False)
  191. tzabbr_register("WDT", "Western Daylight Time", "Australia",
  192. "Australia/West", True)
  193. tzabbr_register("WEDT", "Western European Daylight Time", "Europe",
  194. "Etc/GMT+1", True)
  195. tzabbr_register("WEST", "Western European Summer Time", "Europe",
  196. "Etc/GMT+1", True)
  197. tzabbr_register("WET", "Western European Time", "Europe", pytz.utc, False)
  198. tzabbr_register("WST", "Western Standard Time", "Australia",
  199. "Australia/West", False)
  200. tzabbr_register("X", "X-ray Time Zone", "Military", "Etc/GMT+11", False)
  201. tzabbr_register("Y", "Yankee Time Zone", "Military", "Etc/GMT+12", False)
  202. tzabbr_register("Z", "Zulu Time Zone", "Military", pytz.utc, False)