DateTimeParserTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. namespace Sabre\VObject;
  3. use DateTime;
  4. use DateTimeZone;
  5. use DateInterval;
  6. class DateTimeParserTest extends \PHPUnit_Framework_TestCase {
  7. function testParseICalendarDuration() {
  8. $this->assertEquals('+1 weeks', DateTimeParser::parseDuration('P1W',true));
  9. $this->assertEquals('+5 days', DateTimeParser::parseDuration('P5D',true));
  10. $this->assertEquals('+5 days 3 hours 50 minutes 12 seconds', DateTimeParser::parseDuration('P5DT3H50M12S',true));
  11. $this->assertEquals('-1 weeks 50 minutes', DateTimeParser::parseDuration('-P1WT50M',true));
  12. $this->assertEquals('+50 days 3 hours 2 seconds', DateTimeParser::parseDuration('+P50DT3H2S',true));
  13. $this->assertEquals('+0 seconds', DateTimeParser::parseDuration('+PT0S',true));
  14. $this->assertEquals(new DateInterval('PT0S'), DateTimeParser::parseDuration('PT0S'));
  15. }
  16. function testParseICalendarDurationDateInterval() {
  17. $expected = new DateInterval('P7D');
  18. $this->assertEquals($expected, DateTimeParser::parseDuration('P1W'));
  19. $this->assertEquals($expected, DateTimeParser::parse('P1W'));
  20. $expected = new DateInterval('PT3M');
  21. $expected->invert = true;
  22. $this->assertEquals($expected, DateTimeParser::parseDuration('-PT3M'));
  23. }
  24. /**
  25. * @expectedException LogicException
  26. */
  27. function testParseICalendarDurationFail() {
  28. DateTimeParser::parseDuration('P1X',true);
  29. }
  30. function testParseICalendarDateTime() {
  31. $dateTime = DateTimeParser::parseDateTime('20100316T141405');
  32. $compare = new DateTime('2010-03-16 14:14:05',new DateTimeZone('UTC'));
  33. $this->assertEquals($compare, $dateTime);
  34. }
  35. /**
  36. * @depends testParseICalendarDateTime
  37. * @expectedException LogicException
  38. */
  39. function testParseICalendarDateTimeBadFormat() {
  40. $dateTime = DateTimeParser::parseDateTime('20100316T141405 ');
  41. }
  42. /**
  43. * @depends testParseICalendarDateTime
  44. */
  45. function testParseICalendarDateTimeUTC() {
  46. $dateTime = DateTimeParser::parseDateTime('20100316T141405Z');
  47. $compare = new DateTime('2010-03-16 14:14:05',new DateTimeZone('UTC'));
  48. $this->assertEquals($compare, $dateTime);
  49. }
  50. /**
  51. * @depends testParseICalendarDateTime
  52. */
  53. function testParseICalendarDateTimeUTC2() {
  54. $dateTime = DateTimeParser::parseDateTime('20101211T160000Z');
  55. $compare = new DateTime('2010-12-11 16:00:00',new DateTimeZone('UTC'));
  56. $this->assertEquals($compare, $dateTime);
  57. }
  58. /**
  59. * @depends testParseICalendarDateTime
  60. */
  61. function testParseICalendarDateTimeCustomTimeZone() {
  62. $dateTime = DateTimeParser::parseDateTime('20100316T141405', new DateTimeZone('Europe/Amsterdam'));
  63. $compare = new DateTime('2010-03-16 14:14:05',new DateTimeZone('Europe/Amsterdam'));
  64. $this->assertEquals($compare, $dateTime);
  65. }
  66. function testParseICalendarDate() {
  67. $dateTime = DateTimeParser::parseDate('20100316');
  68. $expected = new DateTime('2010-03-16 00:00:00',new DateTimeZone('UTC'));
  69. $this->assertEquals($expected, $dateTime);
  70. $dateTime = DateTimeParser::parse('20100316');
  71. $this->assertEquals($expected, $dateTime);
  72. }
  73. /**
  74. * TCheck if a date with year > 4000 will not throw an exception. iOS seems to use 45001231 in yearly recurring events
  75. */
  76. function testParseICalendarDateGreaterThan4000() {
  77. $dateTime = DateTimeParser::parseDate('45001231');
  78. $expected = new DateTime('4500-12-31 00:00:00',new DateTimeZone('UTC'));
  79. $this->assertEquals($expected, $dateTime);
  80. $dateTime = DateTimeParser::parse('45001231');
  81. $this->assertEquals($expected, $dateTime);
  82. }
  83. /**
  84. * Check if a datetime with year > 4000 will not throw an exception. iOS seems to use 45001231T235959 in yearly recurring events
  85. */
  86. function testParseICalendarDateTimeGreaterThan4000() {
  87. $dateTime = DateTimeParser::parseDateTime('45001231T235959');
  88. $expected = new DateTime('4500-12-31 23:59:59',new DateTimeZone('UTC'));
  89. $this->assertEquals($expected, $dateTime);
  90. $dateTime = DateTimeParser::parse('45001231T235959');
  91. $this->assertEquals($expected, $dateTime);
  92. }
  93. /**
  94. * @depends testParseICalendarDate
  95. * @expectedException LogicException
  96. */
  97. function testParseICalendarDateBadFormat() {
  98. $dateTime = DateTimeParser::parseDate('20100316T141405');
  99. }
  100. /**
  101. * @dataProvider vcardDates
  102. */
  103. function testVCardDate($input, $output) {
  104. $this->assertEquals(
  105. $output,
  106. DateTimeParser::parseVCardDateTime($input)
  107. );
  108. }
  109. /**
  110. * @dataProvider vcardDates
  111. * @expectedException \InvalidArgumentException
  112. */
  113. function testBadVCardDate() {
  114. DateTimeParser::parseVCardDateTime('1985---01');
  115. }
  116. /**
  117. * @dataProvider vcardDates
  118. * @expectedException \InvalidArgumentException
  119. */
  120. function testBadVCardTime() {
  121. DateTimeParser::parseVCardTime('23:12:166');
  122. }
  123. function vcardDates() {
  124. return array(
  125. array(
  126. "19961022T140000",
  127. array(
  128. "year" => 1996,
  129. "month" => 10,
  130. "date" => 22,
  131. "hour" => 14,
  132. "minute" => 00,
  133. "second" => 00,
  134. "timezone" => null
  135. ),
  136. ),
  137. array(
  138. "--1022T1400",
  139. array(
  140. "year" => null,
  141. "month" => 10,
  142. "date" => 22,
  143. "hour" => 14,
  144. "minute" => 00,
  145. "second" => null,
  146. "timezone" => null
  147. ),
  148. ),
  149. array(
  150. "---22T14",
  151. array(
  152. "year" => null,
  153. "month" => null,
  154. "date" => 22,
  155. "hour" => 14,
  156. "minute" => null,
  157. "second" => null,
  158. "timezone" => null
  159. ),
  160. ),
  161. array(
  162. "19850412",
  163. array(
  164. "year" => 1985,
  165. "month" => 4,
  166. "date" => 12,
  167. "hour" => null,
  168. "minute" => null,
  169. "second" => null,
  170. "timezone" => null
  171. ),
  172. ),
  173. array(
  174. "1985-04",
  175. array(
  176. "year" => 1985,
  177. "month" => 04,
  178. "date" => null,
  179. "hour" => null,
  180. "minute" => null,
  181. "second" => null,
  182. "timezone" => null
  183. ),
  184. ),
  185. array(
  186. "1985",
  187. array(
  188. "year" => 1985,
  189. "month" => null,
  190. "date" => null,
  191. "hour" => null,
  192. "minute" => null,
  193. "second" => null,
  194. "timezone" => null
  195. ),
  196. ),
  197. array(
  198. "--0412",
  199. array(
  200. "year" => null,
  201. "month" => 4,
  202. "date" => 12,
  203. "hour" => null,
  204. "minute" => null,
  205. "second" => null,
  206. "timezone" => null
  207. ),
  208. ),
  209. array(
  210. "---12",
  211. array(
  212. "year" => null,
  213. "month" => null,
  214. "date" => 12,
  215. "hour" => null,
  216. "minute" => null,
  217. "second" => null,
  218. "timezone" => null
  219. ),
  220. ),
  221. array(
  222. "T102200",
  223. array(
  224. "year" => null,
  225. "month" => null,
  226. "date" => null,
  227. "hour" => 10,
  228. "minute" => 22,
  229. "second" => 0,
  230. "timezone" => null
  231. ),
  232. ),
  233. array(
  234. "T1022",
  235. array(
  236. "year" => null,
  237. "month" => null,
  238. "date" => null,
  239. "hour" => 10,
  240. "minute" => 22,
  241. "second" => null,
  242. "timezone" => null
  243. ),
  244. ),
  245. array(
  246. "T10",
  247. array(
  248. "year" => null,
  249. "month" => null,
  250. "date" => null,
  251. "hour" => 10,
  252. "minute" => null,
  253. "second" => null,
  254. "timezone" => null
  255. ),
  256. ),
  257. array(
  258. "T-2200",
  259. array(
  260. "year" => null,
  261. "month" => null,
  262. "date" => null,
  263. "hour" => null,
  264. "minute" => 22,
  265. "second" => 00,
  266. "timezone" => null
  267. ),
  268. ),
  269. array(
  270. "T--00",
  271. array(
  272. "year" => null,
  273. "month" => null,
  274. "date" => null,
  275. "hour" => null,
  276. "minute" => null,
  277. "second" => 00,
  278. "timezone" => null
  279. ),
  280. ),
  281. array(
  282. "T102200Z",
  283. array(
  284. "year" => null,
  285. "month" => null,
  286. "date" => null,
  287. "hour" => 10,
  288. "minute" => 22,
  289. "second" => 00,
  290. "timezone" => 'Z'
  291. ),
  292. ),
  293. array(
  294. "T102200-0800",
  295. array(
  296. "year" => null,
  297. "month" => null,
  298. "date" => null,
  299. "hour" => 10,
  300. "minute" => 22,
  301. "second" => 00,
  302. "timezone" => '-0800'
  303. ),
  304. ),
  305. // extended format
  306. array(
  307. "2012-11-29T15:10:53Z",
  308. array(
  309. "year" => 2012,
  310. "month" => 11,
  311. "date" => 29,
  312. "hour" => 15,
  313. "minute" => 10,
  314. "second" => 53,
  315. "timezone" => 'Z'
  316. ),
  317. ),
  318. // with milliseconds
  319. array(
  320. "20121129T151053.123Z",
  321. array(
  322. "year" => 2012,
  323. "month" => 11,
  324. "date" => 29,
  325. "hour" => 15,
  326. "minute" => 10,
  327. "second" => 53,
  328. "timezone" => 'Z'
  329. ),
  330. ),
  331. // extended format with milliseconds
  332. array(
  333. "2012-11-29T15:10:53.123Z",
  334. array(
  335. "year" => 2012,
  336. "month" => 11,
  337. "date" => 29,
  338. "hour" => 15,
  339. "minute" => 10,
  340. "second" => 53,
  341. "timezone" => 'Z'
  342. ),
  343. ),
  344. );
  345. }
  346. }