DateTimeHelperTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. namespace fphammerle\helpers\tests;
  3. use \DateInterval as DI;
  4. use \DatePeriod as DP;
  5. use \DateTime as DT;
  6. use \fphammerle\helpers\DateTimeHelper;
  7. class DateTimeHelperTest extends \PHPUnit_Framework_TestCase
  8. {
  9. public function timestampToDateTimeProvider()
  10. {
  11. return [
  12. [null, null],
  13. [0, new DT('1970-01-01 00:00:00', new \DateTimeZone('UTC'))],
  14. [0, new DT('1970-01-01 01:00:00', new \DateTimeZone('Europe/Vienna'))],
  15. [1234567890, new DT('2009-02-13 23:31:30', new \DateTimeZone('UTC'))],
  16. [1234567890, new DT('2009-02-14 00:31:30', new \DateTimeZone('Europe/Vienna'))],
  17. [-3600, new DT('1970-01-01 00:00:00', new \DateTimeZone('Europe/Vienna'))],
  18. ];
  19. }
  20. /**
  21. * @dataProvider timestampToDateTimeProvider
  22. */
  23. public function testTimestampToDateTime($timestamp, $expected_datetime)
  24. {
  25. $generated_datetime = DateTimeHelper::timestampToDateTime($timestamp);
  26. $this->assertEquals($expected_datetime, $generated_datetime);
  27. }
  28. public function timestampToDateTimeDefaultTimezoneProvider()
  29. {
  30. return [
  31. ['UTC', 100],
  32. ['Europe/Vienna', 0],
  33. ['Europe/Vienna', -100],
  34. ['Europe/Vienna', 100],
  35. ['Europe/London', 3600],
  36. ['US/Pacific', 3600],
  37. ];
  38. }
  39. /**
  40. * @dataProvider timestampToDateTimeDefaultTimezoneProvider
  41. */
  42. public function testTimestampToDateTimeDefaultTimezone($timezone, $timestamp)
  43. {
  44. date_default_timezone_set($timezone);
  45. $generated_datetime = DateTimeHelper::timestampToDateTime($timestamp);
  46. $this->assertSame($timestamp, $generated_datetime->getTimestamp());
  47. }
  48. public function parseProvider()
  49. {
  50. return [
  51. // null
  52. [null, 'UTC', null],
  53. [null, 'US/Pacific', null],
  54. // year without timezone
  55. ['1900', 'UTC', new DP(new DT('1900-01-01T00:00:00Z'), new DI('P1Y'), 0)],
  56. ['0014', 'Europe/Vienna', new DP(new DT('0014-01-01T00:00:00+01:00'), new DI('P1Y'), 0)],
  57. ['2016', 'US/Pacific', new DP(new DT('2016-01-01T00:00:00-08:00'), new DI('P1Y'), 0)],
  58. // year with timezone
  59. ['1900Z', 'US/Pacific', new DP(new DT('1900-01-01T00:00:00Z'), new DI('P1Y'), 0)],
  60. ['2016Z', 'Europe/Vienna', new DP(new DT('2016-01-01T00:00:00Z'), new DI('P1Y'), 0)],
  61. ['2016+00:00', 'Europe/Vienna', new DP(new DT('2016-01-01T00:00:00Z'), new DI('P1Y'), 0)],
  62. ['2016+02:00', 'US/Pacific', new DP(new DT('2016-01-01T00:00:00+02:00'), new DI('P1Y'), 0)],
  63. ['0000 +02:05', 'US/Pacific', new DP(new DT('0000-01-01T00:00:00+02:05'), new DI('P1Y'), 0)],
  64. ['2016-08:00', 'UTC', new DP(new DT('2016-01-01T00:00:00-08:00'), new DI('P1Y'), 0)],
  65. ['2016 -08:00', 'UTC', new DP(new DT('2016-01-01T00:00:00-08:00'), new DI('P1Y'), 0)],
  66. // month without timezone
  67. ['2016-08', 'UTC', new DP(new DT('2016-08-01T00:00:00Z'), new DI('P1M'), 0)],
  68. ['2016-08', 'Europe/Vienna', new DP(new DT('2016-08-01T00:00:00+02:00'), new DI('P1M'), 0)],
  69. ['2016-01', 'US/Pacific', new DP(new DT('2016-01-01T00:00:00-08:00'), new DI('P1M'), 0)],
  70. // month with timezone
  71. ['2016-08Z', 'US/Pacific', new DP(new DT('2016-08-01T00:00:00Z'), new DI('P1M'), 0)],
  72. ['2016-08Z', 'Europe/Vienna', new DP(new DT('2016-08-01T00:00:00Z'), new DI('P1M'), 0)],
  73. ['2016-01+00:00', 'Europe/Vienna', new DP(new DT('2016-01-01T00:00:00Z'), new DI('P1M'), 0)],
  74. ['2016-01+02:00', 'US/Pacific', new DP(new DT('2016-01-01T00:00:00+02:00'), new DI('P1M'), 0)],
  75. ['2016-01 +02:00', 'US/Pacific', new DP(new DT('2016-01-01T00:00:00+02:00'), new DI('P1M'), 0)],
  76. ['2016-01 -08:00', 'UTC', new DP(new DT('2016-01-01T00:00:00-08:00'), new DI('P1M'), 0)],
  77. // date without timezone
  78. ['2016-08-02', 'UTC', new DP(new DT('2016-08-02T00:00:00Z'), new DI('P1D'), 0)],
  79. ['2016-08-02', 'Europe/Vienna', new DP(new DT('2016-08-02T00:00:00+02:00'), new DI('P1D'), 0)],
  80. ['2016-01-02', 'US/Pacific', new DP(new DT('2016-01-02T00:00:00-08:00'), new DI('P1D'), 0)],
  81. // date with timezone
  82. ['2016-08-02Z', 'US/Pacific', new DP(new DT('2016-08-02T00:00:00Z'), new DI('P1D'), 0)],
  83. ['2016-08-02Z', 'Europe/Vienna', new DP(new DT('2016-08-02T00:00:00Z'), new DI('P1D'), 0)],
  84. ['2016-01-02+00:00', 'Europe/Vienna', new DP(new DT('2016-01-02T00:00:00Z'), new DI('P1D'), 0)],
  85. ['2016-01-02+02:00', 'US/Pacific', new DP(new DT('2016-01-02T00:00:00+02:00'), new DI('P1D'), 0)],
  86. ['2016-01-02-08:13', 'UTC', new DP(new DT('2016-01-02T00:00:00-08:13'), new DI('P1D'), 0)],
  87. // minute without timezone
  88. ['2016-08-02 15:52', 'UTC', new DP(new DT('2016-08-02T15:52:00Z'), new DI('PT1M'), 0)],
  89. ['2016-08-02T15:52', 'UTC', new DP(new DT('2016-08-02T15:52:00Z'), new DI('PT1M'), 0)],
  90. ['2016-08-02T15:52', 'Europe/Vienna', new DP(new DT('2016-08-02T15:52:00+02:00'), new DI('PT1M'), 0)],
  91. ['2016-01-02T15:52', 'US/Pacific', new DP(new DT('2016-01-02T15:52:00-08:00'), new DI('PT1M'), 0)],
  92. // minute with timezone
  93. ['2016-08-02 15:52Z', 'US/Pacific', new DP(new DT('2016-08-02T15:52:00Z'), new DI('PT1M'), 0)],
  94. ['2016-08-02T15:52Z', 'Europe/Vienna', new DP(new DT('2016-08-02T15:52:00Z'), new DI('PT1M'), 0)],
  95. ['2016-01-02T15:52+00:00', 'Europe/Vienna', new DP(new DT('2016-01-02T15:52:00Z'), new DI('PT1M'), 0)],
  96. ['2016-01-02T15:52+02:00', 'US/Pacific', new DP(new DT('2016-01-02T15:52:00+02:00'), new DI('PT1M'), 0)],
  97. ['2016-01-02T15:52-08:00', 'UTC', new DP(new DT('2016-01-02T15:52:00-08:00'), new DI('PT1M'), 0)],
  98. // second without timezone
  99. ['2016-08-02 15:52:13', 'UTC', new DP(new DT('2016-08-02T15:52:13Z'), new DI('PT1S'), 0)],
  100. ['2016-08-02T15:52:13', 'UTC', new DP(new DT('2016-08-02T15:52:13Z'), new DI('PT1S'), 0)],
  101. ['2016-08-02T15:52:13', 'Europe/Vienna', new DP(new DT('2016-08-02T15:52:13+02:00'), new DI('PT1S'), 0)],
  102. ['2016-01-02T15:52:00', 'US/Pacific', new DP(new DT('2016-01-02T15:52:00-08:00'), new DI('PT1S'), 0)],
  103. // second with timezone
  104. ['2016-08-02 15:52:13Z', 'US/Pacific', new DP(new DT('2016-08-02T15:52:13Z'), new DI('PT1S'), 0)],
  105. ['2016-08-02T15:52:13Z', 'Europe/Vienna', new DP(new DT('2016-08-02T15:52:13Z'), new DI('PT1S'), 0)],
  106. ['2016-01-02T15:52:13+00:00', 'Europe/Vienna', new DP(new DT('2016-01-02T15:52:13Z'), new DI('PT1S'), 0)],
  107. ['2016-01-02T15:52:13+02:00', 'US/Pacific', new DP(new DT('2016-01-02T15:52:13+02:00'), new DI('PT1S'), 0)],
  108. ['2016-01-02T15:52:13-08:00', 'UTC', new DP(new DT('2016-01-02T15:52:13-08:00'), new DI('PT1S'), 0)],
  109. ];
  110. }
  111. /**
  112. * @dataProvider parseProvider
  113. */
  114. public function testParse($text, $timezone, $expected)
  115. {
  116. date_default_timezone_set($timezone);
  117. $this->assertEquals($expected, DateTimeHelper::parse($text));
  118. }
  119. public function parseInvalidArgumentProvider()
  120. {
  121. return [
  122. [' '],
  123. [''],
  124. ['2016--12'],
  125. ['2016-01-08:00'],
  126. ['2016-10-12 08:20#01'],
  127. [1],
  128. [false],
  129. ];
  130. }
  131. /**
  132. * @dataProvider parseInvalidArgumentProvider
  133. * @expectedException \InvalidArgumentException
  134. */
  135. public function testParseInvalidArgument($text)
  136. {
  137. DateTimeHelper::parse($text);
  138. }
  139. public function parseGetStartProvider()
  140. {
  141. return [
  142. [null, 'UTC', null],
  143. [null, 'US/Pacific', null],
  144. ['2016-08-02', 'UTC', new DT('2016-08-02T00:00:00Z')],
  145. ['2016-08-02', 'Europe/Vienna', new DT('2016-08-02T00:00:00+02:00')],
  146. ['2016-08-02', 'Europe/Vienna', new DT('2016-08-01T22:00:00Z')],
  147. ['2016-08-02 15:52:13', 'UTC', new DT('2016-08-02T15:52:13Z')],
  148. ['2016-08-02 15:52:13', 'Europe/Vienna', new DT('2016-08-02T15:52:13+02:00')],
  149. ['2016-08-02 15:52:13', 'Europe/Vienna', new DT('2016-08-02T13:52:13Z')],
  150. ['2016-08-02T15:52:13', 'US/Pacific', new DT('2016-08-02T15:52:13-07:00')],
  151. ];
  152. }
  153. /**
  154. * @dataProvider parseGetStartProvider
  155. */
  156. public function testParseGetStart($text, $timezone, $expected)
  157. {
  158. date_default_timezone_set($timezone);
  159. $this->assertEquals($expected, DateTimeHelper::parseGetStart($text));
  160. }
  161. public function parseGetStartInvalidArgumentProvider()
  162. {
  163. return [
  164. [' '],
  165. [''],
  166. ['2016--12'],
  167. ['2016-10-12 08:20#01'],
  168. [1],
  169. [false],
  170. ];
  171. }
  172. /**
  173. * @dataProvider parseGetStartInvalidArgumentProvider
  174. * @expectedException \InvalidArgumentException
  175. */
  176. public function testParseGetStartInvalidArgument($text)
  177. {
  178. DateTimeHelper::parseGetStart($text);
  179. }
  180. public function deinvertIntervalProvider()
  181. {
  182. return [
  183. [
  184. DI::createFromDateString('-2 years'),
  185. ['y' => -2, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  186. ],
  187. [
  188. DI::createFromDateString('-2 months'),
  189. ['y' => 0, 'm' => -2, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  190. ],
  191. [
  192. DI::createFromDateString('-2 days'),
  193. ['y' => 0, 'm' => 0, 'd' => -2, 'h' => 0, 'i' => 0, 's' => 0],
  194. ],
  195. [
  196. DI::createFromDateString('-2 hours'),
  197. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => -2, 'i' => 0, 's' => 0],
  198. ],
  199. [
  200. DI::createFromDateString('-2 minutes'),
  201. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => -2, 's' => 0],
  202. ],
  203. [
  204. DI::createFromDateString('-2 seconds'),
  205. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => 0, 's' => -2],
  206. ],
  207. [
  208. (new DT('2016-08'))->diff(new DT('2016-07')),
  209. ['y' => 0, 'm' => -1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  210. ],
  211. [
  212. (new DT('2016-08-03'))->diff(new DT('2016-07-03')),
  213. ['y' => 0, 'm' => -1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  214. ],
  215. [
  216. (new DT('2016-07-03'))->diff(new DT('2016-08-03')),
  217. ['y' => 0, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  218. ],
  219. [
  220. (new DT('2016-08-04'))->diff(new DT('2016-07-03')),
  221. ['y' => 0, 'm' => -1, 'd' => -1, 'h' => 0, 'i' => 0, 's' => 0],
  222. ],
  223. [
  224. (new DT('2016-07-03'))->diff(new DT('2016-08-04')),
  225. ['y' => 0, 'm' => 1, 'd' => 1, 'h' => 0, 'i' => 0, 's' => 0],
  226. ],
  227. [
  228. (new DT('2016-08-02'))->diff(new DT('2016-07-03')),
  229. ['y' => 0, 'm' => 0, 'd' => -30, 'h' => 0, 'i' => 0, 's' => 0],
  230. ],
  231. [
  232. (new DT('2016-07-03'))->diff(new DT('2016-08-02')),
  233. ['y' => 0, 'm' => 0, 'd' => 30, 'h' => 0, 'i' => 0, 's' => 0],
  234. ],
  235. [
  236. (new DT('2016-08-04 18:10:02'))->diff(new DT('2016-07-03 14:13:03')),
  237. ['y' => 0, 'm' => -1, 'd' => -1, 'h' => -3, 'i' => -56, 's' => -59],
  238. ],
  239. [
  240. (new DT('2016-07-03 14:13:03'))->diff(new DT('2016-08-04 18:10:02')),
  241. ['y' => 0, 'm' => 1, 'd' => 1, 'h' => 3, 'i' => 56, 's' => 59],
  242. ],
  243. ];
  244. }
  245. /**
  246. * @dataProvider deinvertIntervalProvider
  247. */
  248. public function testDeinvertInterval($source, $expected_attr)
  249. {
  250. // \DateInterval does not implement clone.
  251. // @see https://bugs.php.net/bug.php?id=50559
  252. $source_copy = unserialize(serialize($source));
  253. $deinverted = DateTimeHelper::deinvertInterval($source_copy);
  254. $this->assertEquals($source, $source_copy);
  255. $this->assertEquals(0, $deinverted->invert);
  256. foreach($expected_attr as $k => $v) {
  257. $this->assertSame($v, $deinverted->$k);
  258. }
  259. }
  260. public function intervalToIsoProvider()
  261. {
  262. return [
  263. [null, null],
  264. ];
  265. }
  266. /**
  267. * @dataProvider intervalToIsoProvider
  268. */
  269. public function testIntervalToIso($interval, $iso)
  270. {
  271. $this->assertSame($iso, DateTimeHelper::intervalToIso($interval));
  272. }
  273. public function intervalToIsoReinitProvider()
  274. {
  275. return [
  276. [new DI('P1Y')],
  277. [new DI('P1M')],
  278. [new DI('P1D')],
  279. [new DI('PT1H')],
  280. [new DI('PT1M')],
  281. [new DI('PT1S')],
  282. [new DI('P1Y2M3DT4H5M6S')],
  283. ];
  284. }
  285. /**
  286. * @dataProvider intervalToIsoReinitProvider
  287. */
  288. public function testIntervalToIsoReinit($interval)
  289. {
  290. $iso = DateTimeHelper::intervalToIso($interval);
  291. $this->assertEquals($interval, new DI($iso));
  292. }
  293. public function intervalToIsoReinitUnsupportedProvider()
  294. {
  295. return [
  296. [DI::createFromDateString('-2 years')],
  297. [DI::createFromDateString('-2 months')],
  298. [DI::createFromDateString('-2 days')],
  299. [DI::createFromDateString('-2 hours')],
  300. [DI::createFromDateString('-2 minutes')],
  301. [DI::createFromDateString('-2 seconds')],
  302. [(new DT('2016-08-03'))->diff(new DT('2016-07-03'))],
  303. [(new DT('2016-08-03 10:00:01'))->diff(new DT('2016-08-03 10:00:00'))],
  304. ];
  305. }
  306. /**
  307. * @dataProvider intervalToIsoReinitUnsupportedProvider
  308. * @expectedException \Exception
  309. */
  310. public function testIntervalToIsoReinitUnsupported($interval)
  311. {
  312. DateTimeHelper::intervalToIso($interval);
  313. }
  314. public function periodToIsoProvider()
  315. {
  316. return [
  317. [null, null],
  318. [
  319. new DP(
  320. new DT('2016-08-05T14:50:14+08:00'),
  321. new DI('P1D'),
  322. new DT('2016-08-10T14:50:14+08:00')
  323. ),
  324. 'R4/2016-08-05T14:50:14+08:00/P0Y0M1DT0H0M0S',
  325. ],
  326. [
  327. new DP(
  328. new DT('2016-08-05T14:50:14+08:00'),
  329. new DI('P5D'),
  330. new DT('2016-08-10T14:50:14+08:00')
  331. ),
  332. '2016-08-05T14:50:14+08:00/P0Y0M5DT0H0M0S',
  333. ],
  334. [
  335. new DP(
  336. new DT('2016-08-05T14:50:14+08:00'),
  337. new DI('P1Y2M3DT4H5M6S'),
  338. new DT('2017-10-08T18:55:20+08:00')
  339. ),
  340. '2016-08-05T14:50:14+08:00/P1Y2M3DT4H5M6S',
  341. ],
  342. [
  343. new DP(
  344. new DT('2016-08-05T14:50:14Z'),
  345. new DI('P1D'),
  346. 0
  347. ),
  348. '2016-08-05T14:50:14+00:00/P0Y0M1DT0H0M0S',
  349. ],
  350. [
  351. new DP(
  352. new DT('2016-08-05T14:50:14Z'),
  353. new DI('PT5M'),
  354. 3
  355. ),
  356. 'R3/2016-08-05T14:50:14+00:00/P0Y0M0DT0H5M0S',
  357. ],
  358. [
  359. new DP('R3/2016-08-05T14:50:14Z/PT5M'),
  360. 'R3/2016-08-05T14:50:14+00:00/P0Y0M0DT0H5M0S',
  361. ],
  362. [
  363. new DP('R4/2016-08-05T14:50:14Z/P1Y2M3DT4H5M6S'),
  364. 'R4/2016-08-05T14:50:14+00:00/P1Y2M3DT4H5M6S',
  365. ],
  366. [
  367. DateTimeHelper::parse('2016-08-05T14:50:14Z'),
  368. '2016-08-05T14:50:14+00:00/P0Y0M0DT0H0M1S',
  369. ],
  370. [
  371. DateTimeHelper::parse('2016-08-05Z'),
  372. '2016-08-05T00:00:00+00:00/P0Y0M1DT0H0M0S',
  373. ],
  374. [
  375. DateTimeHelper::parse('2016-08-05-03:00'),
  376. '2016-08-05T00:00:00-03:00/P0Y0M1DT0H0M0S',
  377. ],
  378. ];
  379. }
  380. /**
  381. * @dataProvider periodToIsoProvider
  382. */
  383. public function testPeriodToIso($period, $iso)
  384. {
  385. $this->assertSame($iso, DateTimeHelper::periodToIso($period));
  386. }
  387. }