TimeZoneUtilTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace Sabre\VObject;
  3. class TimezoneUtilTest extends \PHPUnit_Framework_TestCase {
  4. function setUp() {
  5. // clearning the tz cache
  6. TimezoneUtil::$map = null;
  7. }
  8. /**
  9. * @dataProvider getMapping
  10. */
  11. function testCorrectTZ($timezoneName) {
  12. try {
  13. $tz = new \DateTimeZone($timezoneName);
  14. $this->assertInstanceOf('DateTimeZone', $tz);
  15. } catch (\Exception $e) {
  16. if (strpos($e->getMessage(), "Unknown or bad timezone")!==false) {
  17. $this->markTestSkipped($timezoneName . ' is not (yet) supported in this PHP version. Update pecl/timezonedb');
  18. } else {
  19. throw $e;
  20. }
  21. }
  22. }
  23. function getMapping() {
  24. TimeZoneUtil::loadTzMaps();
  25. // PHPUNit requires an array of arrays
  26. return array_map(
  27. function($value) {
  28. return array($value);
  29. },
  30. TimeZoneUtil::$map
  31. );
  32. }
  33. function testExchangeMap() {
  34. $vobj = <<<HI
  35. BEGIN:VCALENDAR
  36. METHOD:REQUEST
  37. VERSION:2.0
  38. BEGIN:VTIMEZONE
  39. TZID:foo
  40. X-MICROSOFT-CDO-TZID:2
  41. BEGIN:STANDARD
  42. DTSTART:16010101T030000
  43. TZOFFSETFROM:+0200
  44. TZOFFSETTO:+0100
  45. RRULE:FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=10;BYDAY=-1SU
  46. END:STANDARD
  47. BEGIN:DAYLIGHT
  48. DTSTART:16010101T020000
  49. TZOFFSETFROM:+0100
  50. TZOFFSETTO:+0200
  51. RRULE:FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=3;BYDAY=-1SU
  52. END:DAYLIGHT
  53. END:VTIMEZONE
  54. BEGIN:VEVENT
  55. DTSTAMP:20120416T092149Z
  56. DTSTART;TZID="foo":20120418T1
  57. 00000
  58. SUMMARY:Begin Unterhaltsreinigung
  59. UID:040000008200E00074C5B7101A82E0080000000010DA091DC31BCD01000000000000000
  60. 0100000008FECD2E607780649BE5A4C9EE6418CBC
  61. 000
  62. END:VEVENT
  63. END:VCALENDAR
  64. HI;
  65. $tz = TimeZoneUtil::getTimeZone('foo', Reader::read($vobj));
  66. $ex = new \DateTimeZone('Europe/Lisbon');
  67. $this->assertEquals($ex->getName(), $tz->getName());
  68. }
  69. function testWetherMicrosoftIsStillInsane() {
  70. $vobj = <<<HI
  71. BEGIN:VCALENDAR
  72. METHOD:REQUEST
  73. VERSION:2.0
  74. BEGIN:VTIMEZONE
  75. TZID:(GMT+01.00) Sarajevo/Warsaw/Zagreb
  76. X-MICROSOFT-CDO-TZID:2
  77. BEGIN:STANDARD
  78. DTSTART:16010101T030000
  79. TZOFFSETFROM:+0200
  80. TZOFFSETTO:+0100
  81. RRULE:FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=10;BYDAY=-1SU
  82. END:STANDARD
  83. END:VTIMEZONE
  84. END:VCALENDAR
  85. HI;
  86. $tz = TimeZoneUtil::getTimeZone('(GMT+01.00) Sarajevo/Warsaw/Zagreb', Reader::read($vobj));
  87. $ex = new \DateTimeZone('Europe/Sarajevo');
  88. $this->assertEquals($ex->getName(), $tz->getName());
  89. }
  90. function testUnknownExchangeId() {
  91. $vobj = <<<HI
  92. BEGIN:VCALENDAR
  93. METHOD:REQUEST
  94. VERSION:2.0
  95. BEGIN:VTIMEZONE
  96. TZID:foo
  97. X-MICROSOFT-CDO-TZID:2000
  98. BEGIN:STANDARD
  99. DTSTART:16010101T030000
  100. TZOFFSETFROM:+0200
  101. TZOFFSETTO:+0100
  102. RRULE:FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=10;BYDAY=-1SU
  103. END:STANDARD
  104. BEGIN:DAYLIGHT
  105. DTSTART:16010101T020000
  106. TZOFFSETFROM:+0100
  107. TZOFFSETTO:+0200
  108. RRULE:FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=3;BYDAY=-1SU
  109. END:DAYLIGHT
  110. END:VTIMEZONE
  111. BEGIN:VEVENT
  112. DTSTAMP:20120416T092149Z
  113. DTSTART;TZID="foo":20120418T1
  114. 00000
  115. SUMMARY:Begin Unterhaltsreinigung
  116. UID:040000008200E00074C5B7101A82E0080000000010DA091DC31BCD01000000000000000
  117. 0100000008FECD2E607780649BE5A4C9EE6418CBC
  118. DTEND;TZID="Sarajevo, Skopje, Sofija, Vilnius, Warsaw, Zagreb":20120418T103
  119. 000
  120. END:VEVENT
  121. END:VCALENDAR
  122. HI;
  123. $tz = TimeZoneUtil::getTimeZone('foo', Reader::read($vobj));
  124. $ex = new \DateTimeZone(date_default_timezone_get());
  125. $this->assertEquals($ex->getName(), $tz->getName());
  126. }
  127. function testWindowsTimeZone() {
  128. $tz = TimeZoneUtil::getTimeZone('Eastern Standard Time');
  129. $ex = new \DateTimeZone('America/New_York');
  130. $this->assertEquals($ex->getName(), $tz->getName());
  131. }
  132. /**
  133. * @dataProvider getPHPTimeZoneIdentifiers
  134. */
  135. function testTimeZoneIdentifiers($tzid) {
  136. $tz = TimeZoneUtil::getTimeZone($tzid);
  137. $ex = new \DateTimeZone($tzid);
  138. $this->assertEquals($ex->getName(), $tz->getName());
  139. }
  140. /**
  141. * @dataProvider getPHPTimeZoneBCIdentifiers
  142. */
  143. function testTimeZoneBCIdentifiers($tzid) {
  144. $tz = TimeZoneUtil::getTimeZone($tzid);
  145. $ex = new \DateTimeZone($tzid);
  146. $this->assertEquals($ex->getName(), $tz->getName());
  147. }
  148. function getPHPTimeZoneIdentifiers() {
  149. // PHPUNit requires an array of arrays
  150. return array_map(
  151. function($value) {
  152. return array($value);
  153. },
  154. \DateTimeZone::listIdentifiers()
  155. );
  156. }
  157. function getPHPTimeZoneBCIdentifiers() {
  158. // PHPUNit requires an array of arrays
  159. return array_map(
  160. function($value) {
  161. return array($value);
  162. },
  163. TimeZoneUtil::getIdentifiersBC()
  164. );
  165. }
  166. function testTimezoneOffset() {
  167. $tz = TimeZoneUtil::getTimeZone('GMT-0400', null, true);
  168. if (version_compare(PHP_VERSION, '5.5.10', '>=') && !defined('HHVM_VERSION')) {
  169. $ex = new \DateTimeZone('-04:00');
  170. } else {
  171. $ex = new \DateTimeZone('Etc/GMT-4');
  172. }
  173. $this->assertEquals($ex->getName(), $tz->getName());
  174. }
  175. /**
  176. * @expectedException InvalidArgumentException
  177. */
  178. function testTimezoneFail() {
  179. $tz = TimeZoneUtil::getTimeZone('FooBar', null, true);
  180. }
  181. function testFallBack() {
  182. $vobj = <<<HI
  183. BEGIN:VCALENDAR
  184. METHOD:REQUEST
  185. VERSION:2.0
  186. BEGIN:VTIMEZONE
  187. TZID:foo
  188. BEGIN:STANDARD
  189. DTSTART:16010101T030000
  190. TZOFFSETFROM:+0200
  191. TZOFFSETTO:+0100
  192. RRULE:FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=10;BYDAY=-1SU
  193. END:STANDARD
  194. BEGIN:DAYLIGHT
  195. DTSTART:16010101T020000
  196. TZOFFSETFROM:+0100
  197. TZOFFSETTO:+0200
  198. RRULE:FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=3;BYDAY=-1SU
  199. END:DAYLIGHT
  200. END:VTIMEZONE
  201. BEGIN:VEVENT
  202. DTSTAMP:20120416T092149Z
  203. DTSTART;TZID="foo":20120418T1
  204. 00000
  205. SUMMARY:Begin Unterhaltsreinigung
  206. UID:040000008200E00074C5B7101A82E0080000000010DA091DC31BCD01000000000000000
  207. 0100000008FECD2E607780649BE5A4C9EE6418CBC
  208. 000
  209. END:VEVENT
  210. END:VCALENDAR
  211. HI;
  212. $tz = TimeZoneUtil::getTimeZone('foo', Reader::read($vobj));
  213. $ex = new \DateTimeZone(date_default_timezone_get());
  214. $this->assertEquals($ex->getName(), $tz->getName());
  215. }
  216. function testLjubljanaBug() {
  217. $vobj = <<<HI
  218. BEGIN:VCALENDAR
  219. CALSCALE:GREGORIAN
  220. PRODID:-//Ximian//NONSGML Evolution Calendar//EN
  221. VERSION:2.0
  222. BEGIN:VTIMEZONE
  223. TZID:/freeassociation.sourceforge.net/Tzfile/Europe/Ljubljana
  224. X-LIC-LOCATION:Europe/Ljubljana
  225. BEGIN:STANDARD
  226. TZNAME:CET
  227. DTSTART:19701028T030000
  228. RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
  229. TZOFFSETFROM:+0200
  230. TZOFFSETTO:+0100
  231. END:STANDARD
  232. BEGIN:DAYLIGHT
  233. TZNAME:CEST
  234. DTSTART:19700325T020000
  235. RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3
  236. TZOFFSETFROM:+0100
  237. TZOFFSETTO:+0200
  238. END:DAYLIGHT
  239. END:VTIMEZONE
  240. BEGIN:VEVENT
  241. UID:foo
  242. DTSTART;TZID=/freeassociation.sourceforge.net/Tzfile/Europe/Ljubljana:
  243. 20121003T080000
  244. DTEND;TZID=/freeassociation.sourceforge.net/Tzfile/Europe/Ljubljana:
  245. 20121003T083000
  246. TRANSP:OPAQUE
  247. SEQUENCE:2
  248. SUMMARY:testing
  249. CREATED:20121002T172613Z
  250. LAST-MODIFIED:20121002T172613Z
  251. END:VEVENT
  252. END:VCALENDAR
  253. HI;
  254. $tz = TimeZoneUtil::getTimeZone('/freeassociation.sourceforge.net/Tzfile/Europe/Ljubljana', Reader::read($vobj));
  255. $ex = new \DateTimeZone('Europe/Ljubljana');
  256. $this->assertEquals($ex->getName(), $tz->getName());
  257. }
  258. function testWeirdSystemVLICs() {
  259. $vobj = <<<HI
  260. BEGIN:VCALENDAR
  261. CALSCALE:GREGORIAN
  262. PRODID:-//Ximian//NONSGML Evolution Calendar//EN
  263. VERSION:2.0
  264. BEGIN:VTIMEZONE
  265. TZID:/freeassociation.sourceforge.net/Tzfile/SystemV/EST5EDT
  266. X-LIC-LOCATION:SystemV/EST5EDT
  267. BEGIN:STANDARD
  268. TZNAME:EST
  269. DTSTART:19701104T020000
  270. RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11
  271. TZOFFSETFROM:-0400
  272. TZOFFSETTO:-0500
  273. END:STANDARD
  274. BEGIN:DAYLIGHT
  275. TZNAME:EDT
  276. DTSTART:19700311T020000
  277. RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3
  278. TZOFFSETFROM:-0500
  279. TZOFFSETTO:-0400
  280. END:DAYLIGHT
  281. END:VTIMEZONE
  282. BEGIN:VEVENT
  283. UID:20121026T021107Z-6301-1000-1-0@chAir
  284. DTSTAMP:20120905T172126Z
  285. DTSTART;TZID=/freeassociation.sourceforge.net/Tzfile/SystemV/EST5EDT:
  286. 20121026T153000
  287. DTEND;TZID=/freeassociation.sourceforge.net/Tzfile/SystemV/EST5EDT:
  288. 20121026T160000
  289. TRANSP:OPAQUE
  290. SEQUENCE:5
  291. SUMMARY:pick up Ibby
  292. CLASS:PUBLIC
  293. CREATED:20121026T021108Z
  294. LAST-MODIFIED:20121026T021118Z
  295. X-EVOLUTION-MOVE-CALENDAR:1
  296. END:VEVENT
  297. END:VCALENDAR
  298. HI;
  299. $tz = TimeZoneUtil::getTimeZone('/freeassociation.sourceforge.net/Tzfile/SystemV/EST5EDT', Reader::read($vobj), true);
  300. $ex = new \DateTimeZone('America/New_York');
  301. $this->assertEquals($ex->getName(), $tz->getName());
  302. }
  303. }