TimeZoneFactory.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV;
  8. use DateTimeZone;
  9. /**
  10. * Class to generate DateTimeZone object with automated Microsoft and IANA handling
  11. *
  12. * @since 31.0.0
  13. */
  14. class TimeZoneFactory {
  15. /**
  16. * conversion table of Microsoft time zones to IANA time zones
  17. *
  18. * @var array<string,string> MS2IANA
  19. */
  20. private const MS2IANA = [
  21. 'AUS Central Standard Time' => 'Australia/Darwin',
  22. 'Aus Central W. Standard Time' => 'Australia/Eucla',
  23. 'AUS Eastern Standard Time' => 'Australia/Sydney',
  24. 'Afghanistan Standard Time' => 'Asia/Kabul',
  25. 'Alaskan Standard Time' => 'America/Anchorage',
  26. 'Aleutian Standard Time' => 'America/Adak',
  27. 'Altai Standard Time' => 'Asia/Barnaul',
  28. 'Arab Standard Time' => 'Asia/Riyadh',
  29. 'Arabian Standard Time' => 'Asia/Dubai',
  30. 'Arabic Standard Time' => 'Asia/Baghdad',
  31. 'Argentina Standard Time' => 'America/Buenos_Aires',
  32. 'Astrakhan Standard Time' => 'Europe/Astrakhan',
  33. 'Atlantic Standard Time' => 'America/Halifax',
  34. 'Azerbaijan Standard Time' => 'Asia/Baku',
  35. 'Azores Standard Time' => 'Atlantic/Azores',
  36. 'Bahia Standard Time' => 'America/Bahia',
  37. 'Bangladesh Standard Time' => 'Asia/Dhaka',
  38. 'Belarus Standard Time' => 'Europe/Minsk',
  39. 'Bougainville Standard Time' => 'Pacific/Bougainville',
  40. 'Cape Verde Standard Time' => 'Atlantic/Cape_Verde',
  41. 'Canada Central Standard Time' => 'America/Regina',
  42. 'Caucasus Standard Time' => 'Asia/Yerevan',
  43. 'Cen. Australia Standard Time' => 'Australia/Adelaide',
  44. 'Central America Standard Time' => 'America/Guatemala',
  45. 'Central Asia Standard Time' => 'Asia/Almaty',
  46. 'Central Brazilian Standard Time' => 'America/Cuiaba',
  47. 'Central Europe Standard Time' => 'Europe/Budapest',
  48. 'Central European Standard Time' => 'Europe/Warsaw',
  49. 'Central Pacific Standard Time' => 'Pacific/Guadalcanal',
  50. 'Central Standard Time' => 'America/Chicago',
  51. 'Central Standard Time (Mexico)' => 'America/Mexico_City',
  52. 'Chatham Islands Standard Time' => 'Pacific/Chatham',
  53. 'China Standard Time' => 'Asia/Shanghai',
  54. 'Coordinated Universal Time' => 'UTC',
  55. 'Cuba Standard Time' => 'America/Havana',
  56. 'Dateline Standard Time' => 'Etc/GMT+12',
  57. 'E. Africa Standard Time' => 'Africa/Nairobi',
  58. 'E. Australia Standard Time' => 'Australia/Brisbane',
  59. 'E. Europe Standard Time' => 'Europe/Chisinau',
  60. 'E. South America Standard Time' => 'America/Sao_Paulo',
  61. 'Easter Island Standard Time' => 'Pacific/Easter',
  62. 'Eastern Standard Time' => 'America/Toronto',
  63. 'Eastern Standard Time (Mexico)' => 'America/Cancun',
  64. 'Egypt Standard Time' => 'Africa/Cairo',
  65. 'Ekaterinburg Standard Time' => 'Asia/Yekaterinburg',
  66. 'FLE Standard Time' => 'Europe/Kiev',
  67. 'Fiji Standard Time' => 'Pacific/Fiji',
  68. 'GMT Standard Time' => 'Europe/London',
  69. 'GTB Standard Time' => 'Europe/Bucharest',
  70. 'Georgian Standard Time' => 'Asia/Tbilisi',
  71. 'Greenland Standard Time' => 'America/Godthab',
  72. 'Greenland (Danmarkshavn)' => 'America/Godthab',
  73. 'Greenwich Standard Time' => 'Atlantic/Reykjavik',
  74. 'Haiti Standard Time' => 'America/Port-au-Prince',
  75. 'Hawaiian Standard Time' => 'Pacific/Honolulu',
  76. 'India Standard Time' => 'Asia/Kolkata',
  77. 'Iran Standard Time' => 'Asia/Tehran',
  78. 'Israel Standard Time' => 'Asia/Jerusalem',
  79. 'Jordan Standard Time' => 'Asia/Amman',
  80. 'Kaliningrad Standard Time' => 'Europe/Kaliningrad',
  81. 'Kamchatka Standard Time' => 'Asia/Kamchatka',
  82. 'Korea Standard Time' => 'Asia/Seoul',
  83. 'Libya Standard Time' => 'Africa/Tripoli',
  84. 'Line Islands Standard Time' => 'Pacific/Kiritimati',
  85. 'Lord Howe Standard Time' => 'Australia/Lord_Howe',
  86. 'Magadan Standard Time' => 'Asia/Magadan',
  87. 'Magallanes Standard Time' => 'America/Punta_Arenas',
  88. 'Malaysia Standard Time' => 'Asia/Kuala_Lumpur',
  89. 'Marquesas Standard Time' => 'Pacific/Marquesas',
  90. 'Mauritius Standard Time' => 'Indian/Mauritius',
  91. 'Mid-Atlantic Standard Time' => 'Atlantic/South_Georgia',
  92. 'Middle East Standard Time' => 'Asia/Beirut',
  93. 'Montevideo Standard Time' => 'America/Montevideo',
  94. 'Morocco Standard Time' => 'Africa/Casablanca',
  95. 'Mountain Standard Time' => 'America/Denver',
  96. 'Mountain Standard Time (Mexico)' => 'America/Chihuahua',
  97. 'Myanmar Standard Time' => 'Asia/Rangoon',
  98. 'N. Central Asia Standard Time' => 'Asia/Novosibirsk',
  99. 'Namibia Standard Time' => 'Africa/Windhoek',
  100. 'Nepal Standard Time' => 'Asia/Kathmandu',
  101. 'New Zealand Standard Time' => 'Pacific/Auckland',
  102. 'Newfoundland Standard Time' => 'America/St_Johns',
  103. 'Norfolk Standard Time' => 'Pacific/Norfolk',
  104. 'North Asia East Standard Time' => 'Asia/Irkutsk',
  105. 'North Asia Standard Time' => 'Asia/Krasnoyarsk',
  106. 'North Korea Standard Time' => 'Asia/Pyongyang',
  107. 'Omsk Standard Time' => 'Asia/Omsk',
  108. 'Pacific SA Standard Time' => 'America/Santiago',
  109. 'Pacific Standard Time' => 'America/Los_Angeles',
  110. 'Pacific Standard Time (Mexico)' => 'America/Tijuana',
  111. 'Pakistan Standard Time' => 'Asia/Karachi',
  112. 'Paraguay Standard Time' => 'America/Asuncion',
  113. 'Qyzylorda Standard Time' => 'Asia/Qyzylorda',
  114. 'Romance Standard Time' => 'Europe/Paris',
  115. 'Russian Standard Time' => 'Europe/Moscow',
  116. 'Russia Time Zone 10' => 'Asia/Srednekolymsk',
  117. 'Russia Time Zone 3' => 'Europe/Samara',
  118. 'SA Eastern Standard Time' => 'America/Cayenne',
  119. 'SA Pacific Standard Time' => 'America/Bogota',
  120. 'SA Western Standard Time' => 'America/La_Paz',
  121. 'SE Asia Standard Time' => 'Asia/Bangkok',
  122. 'Saint Pierre Standard Time' => 'America/Miquelon',
  123. 'Sakhalin Standard Time' => 'Asia/Sakhalin',
  124. 'Samoa Standard Time' => 'Pacific/Apia',
  125. 'Sao Tome Standard Time' => 'Africa/Sao_Tome',
  126. 'Saratov Standard Time' => 'Europe/Saratov',
  127. 'Singapore Standard Time' => 'Asia/Singapore',
  128. 'South Africa Standard Time' => 'Africa/Johannesburg',
  129. 'South Sudan Standard Time' => 'Africa/Juba',
  130. 'Sri Lanka Standard Time' => 'Asia/Colombo',
  131. 'Sudan Standard Time' => 'Africa/Khartoum',
  132. 'Syria Standard Time' => 'Asia/Damascus',
  133. 'Taipei Standard Time' => 'Asia/Taipei',
  134. 'Tasmania Standard Time' => 'Australia/Hobart',
  135. 'Tocantins Standard Time' => 'America/Araguaina',
  136. 'Tokyo Standard Time' => 'Asia/Tokyo',
  137. 'Tomsk Standard Time' => 'Asia/Tomsk',
  138. 'Tonga Standard Time' => 'Pacific/Tongatapu',
  139. 'Transbaikal Standard Time' => 'Asia/Chita',
  140. 'Turkey Standard Time' => 'Europe/Istanbul',
  141. 'Turks And Caicos Standard Time' => 'America/Grand_Turk',
  142. 'US Eastern Standard Time' => 'America/Indianapolis',
  143. 'US Mountain Standard Time' => 'America/Phoenix',
  144. 'UTC' => 'Etc/GMT',
  145. 'UTC+13' => 'Etc/GMT-13',
  146. 'UTC+12' => 'Etc/GMT-12',
  147. 'UTC-02' => 'Etc/GMT+2',
  148. 'UTC-09' => 'Etc/GMT+9',
  149. 'UTC-11' => 'Etc/GMT+11',
  150. 'Ulaanbaatar Standard Time' => 'Asia/Ulaanbaatar',
  151. 'Venezuela Standard Time' => 'America/Caracas',
  152. 'Vladivostok Standard Time' => 'Asia/Vladivostok',
  153. 'Volgograd Standard Time' => 'Europe/Volgograd',
  154. 'W. Australia Standard Time' => 'Australia/Perth',
  155. 'W. Central Africa Standard Time' => 'Africa/Lagos',
  156. 'W. Europe Standard Time' => 'Europe/Berlin',
  157. 'W. Mongolia Standard Time' => 'Asia/Hovd',
  158. 'West Asia Standard Time' => 'Asia/Tashkent',
  159. 'West Bank Standard Time' => 'Asia/Hebron',
  160. 'West Pacific Standard Time' => 'Pacific/Port_Moresby',
  161. 'West Samoa Standard Time' => 'Pacific/Apia',
  162. 'Yakutsk Standard Time' => 'Asia/Yakutsk',
  163. 'Yukon Standard Time' => 'America/Whitehorse',
  164. 'Yekaterinburg Standard Time' => 'Asia/Yekaterinburg',
  165. ];
  166. /**
  167. * Determines if given time zone name is a Microsoft time zone
  168. *
  169. * @since 31.0.0
  170. *
  171. * @param string $name time zone name
  172. *
  173. * @return bool
  174. */
  175. public static function isMS(string $name): bool {
  176. return isset(self::MS2IANA[$name]);
  177. }
  178. /**
  179. * Converts Microsoft time zone name to IANA time zone name
  180. *
  181. * @since 31.0.0
  182. *
  183. * @param string $name microsoft time zone
  184. *
  185. * @return string|null valid IANA time zone name on success, or null on failure
  186. */
  187. public static function toIANA(string $name): ?string {
  188. return isset(self::MS2IANA[$name]) ? self::MS2IANA[$name] : null;
  189. }
  190. /**
  191. * Generates DateTimeZone object for given time zone name
  192. *
  193. * @since 31.0.0
  194. *
  195. * @param string $name time zone name
  196. *
  197. * @return DateTimeZone|null
  198. */
  199. public function fromName(string $name): ?DateTimeZone {
  200. // if zone name is MS convert to IANA, otherwise just assume the zone is IANA
  201. $zone = @timezone_open(self::toIANA($name) ?? $name);
  202. return ($zone instanceof DateTimeZone) ? $zone : null;
  203. }
  204. }