DateTimeHelperTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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-10-12 08:20#01'],
  126. [1],
  127. [false],
  128. ];
  129. }
  130. /**
  131. * @dataProvider parseInvalidArgumentProvider
  132. * @expectedException \InvalidArgumentException
  133. */
  134. public function testParseInvalidArgument($text)
  135. {
  136. DateTimeHelper::parse($text);
  137. }
  138. public function parseGetStartProvider()
  139. {
  140. return [
  141. [null, 'UTC', null],
  142. [null, 'US/Pacific', null],
  143. ['2016-08-02', 'UTC', new DT('2016-08-02T00:00:00Z')],
  144. ['2016-08-02', 'Europe/Vienna', new DT('2016-08-02T00:00:00+02:00')],
  145. ['2016-08-02', 'Europe/Vienna', new DT('2016-08-01T22:00:00Z')],
  146. ['2016-08-02 15:52:13', 'UTC', new DT('2016-08-02T15:52:13Z')],
  147. ['2016-08-02 15:52:13', 'Europe/Vienna', new DT('2016-08-02T15:52:13+02:00')],
  148. ['2016-08-02 15:52:13', 'Europe/Vienna', new DT('2016-08-02T13:52:13Z')],
  149. ['2016-08-02T15:52:13', 'US/Pacific', new DT('2016-08-02T15:52:13-07:00')],
  150. ];
  151. }
  152. /**
  153. * @dataProvider parseGetStartProvider
  154. */
  155. public function testParseGetStart($text, $timezone, $expected)
  156. {
  157. date_default_timezone_set($timezone);
  158. $this->assertEquals($expected, DateTimeHelper::parseGetStart($text));
  159. }
  160. public function parseGetStartInvalidArgumentProvider()
  161. {
  162. return [
  163. [' '],
  164. [''],
  165. ['2016--12'],
  166. ['2016-10-12 08:20#01'],
  167. [1],
  168. [false],
  169. ];
  170. }
  171. /**
  172. * @dataProvider parseGetStartInvalidArgumentProvider
  173. * @expectedException \InvalidArgumentException
  174. */
  175. public function testParseGetStartInvalidArgument($text)
  176. {
  177. DateTimeHelper::parseGetStart($text);
  178. }
  179. public function deinvertIntervalProvider()
  180. {
  181. return [
  182. [
  183. DI::createFromDateString('-2 years'),
  184. ['y' => -2, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  185. ],
  186. [
  187. DI::createFromDateString('-2 months'),
  188. ['y' => 0, 'm' => -2, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  189. ],
  190. [
  191. DI::createFromDateString('-2 days'),
  192. ['y' => 0, 'm' => 0, 'd' => -2, 'h' => 0, 'i' => 0, 's' => 0],
  193. ],
  194. [
  195. DI::createFromDateString('-2 hours'),
  196. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => -2, 'i' => 0, 's' => 0],
  197. ],
  198. [
  199. DI::createFromDateString('-2 minutes'),
  200. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => -2, 's' => 0],
  201. ],
  202. [
  203. DI::createFromDateString('-2 seconds'),
  204. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => 0, 's' => -2],
  205. ],
  206. [
  207. (new DT('2016-08'))->diff(new DT('2016-07')),
  208. ['y' => 0, 'm' => -1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  209. ],
  210. [
  211. (new DT('2016-08-03'))->diff(new DT('2016-07-03')),
  212. ['y' => 0, 'm' => -1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  213. ],
  214. [
  215. (new DT('2016-07-03'))->diff(new DT('2016-08-03')),
  216. ['y' => 0, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  217. ],
  218. [
  219. (new DT('2016-08-04'))->diff(new DT('2016-07-03')),
  220. ['y' => 0, 'm' => -1, 'd' => -1, 'h' => 0, 'i' => 0, 's' => 0],
  221. ],
  222. [
  223. (new DT('2016-07-03'))->diff(new DT('2016-08-04')),
  224. ['y' => 0, 'm' => 1, 'd' => 1, 'h' => 0, 'i' => 0, 's' => 0],
  225. ],
  226. [
  227. (new DT('2016-08-02'))->diff(new DT('2016-07-03')),
  228. ['y' => 0, 'm' => 0, 'd' => -30, 'h' => 0, 'i' => 0, 's' => 0],
  229. ],
  230. [
  231. (new DT('2016-07-03'))->diff(new DT('2016-08-02')),
  232. ['y' => 0, 'm' => 0, 'd' => 30, 'h' => 0, 'i' => 0, 's' => 0],
  233. ],
  234. [
  235. (new DT('2016-08-04 18:10:02'))->diff(new DT('2016-07-03 14:13:03')),
  236. ['y' => 0, 'm' => -1, 'd' => -1, 'h' => -3, 'i' => -56, 's' => -59],
  237. ],
  238. [
  239. (new DT('2016-07-03 14:13:03'))->diff(new DT('2016-08-04 18:10:02')),
  240. ['y' => 0, 'm' => 1, 'd' => 1, 'h' => 3, 'i' => 56, 's' => 59],
  241. ],
  242. ];
  243. }
  244. /**
  245. * @dataProvider deinvertIntervalProvider
  246. */
  247. public function testDeinvertInterval($source, $expected_attr)
  248. {
  249. // \DateInterval does not implement clone.
  250. // @see https://bugs.php.net/bug.php?id=50559
  251. $source_copy = unserialize(serialize($source));
  252. $deinverted = DateTimeHelper::deinvertInterval($source_copy);
  253. $this->assertEquals($source, $source_copy);
  254. $this->assertEquals(0, $deinverted->invert);
  255. foreach($expected_attr as $k => $v) {
  256. $this->assertSame($v, $deinverted->$k);
  257. }
  258. }
  259. public function intervalToIsoProvider()
  260. {
  261. return [
  262. [null, null],
  263. ];
  264. }
  265. /**
  266. * @dataProvider intervalToIsoProvider
  267. */
  268. public function testIntervalToIso($interval, $iso)
  269. {
  270. $this->assertSame($iso, DateTimeHelper::intervalToIso($interval));
  271. }
  272. public function intervalToIsoReinitProvider()
  273. {
  274. return [
  275. [new DI('P1Y')],
  276. [new DI('P1M')],
  277. [new DI('P1D')],
  278. [new DI('PT1H')],
  279. [new DI('PT1M')],
  280. [new DI('PT1S')],
  281. [new DI('P1Y2M3DT4H5M6S')],
  282. ];
  283. }
  284. /**
  285. * @dataProvider intervalToIsoReinitProvider
  286. */
  287. public function testIntervalToIsoReinit($interval)
  288. {
  289. $iso = DateTimeHelper::intervalToIso($interval);
  290. $this->assertEquals($interval, new DI($iso));
  291. }
  292. public function intervalToIsoReinitUnsupportedProvider()
  293. {
  294. return [
  295. [DI::createFromDateString('-2 years')],
  296. [DI::createFromDateString('-2 months')],
  297. [DI::createFromDateString('-2 days')],
  298. [DI::createFromDateString('-2 hours')],
  299. [DI::createFromDateString('-2 minutes')],
  300. [DI::createFromDateString('-2 seconds')],
  301. [(new DT('2016-08-03'))->diff(new DT('2016-07-03'))],
  302. [(new DT('2016-08-03 10:00:01'))->diff(new DT('2016-08-03 10:00:00'))],
  303. ];
  304. }
  305. /**
  306. * @dataProvider intervalToIsoReinitUnsupportedProvider
  307. * @expectedException \Exception
  308. */
  309. public function testIntervalToIsoReinitUnsupported($interval)
  310. {
  311. DateTimeHelper::intervalToIso($interval);
  312. }
  313. public function periodToIsoProvider()
  314. {
  315. return [
  316. [null, null],
  317. [
  318. new DP(
  319. new DT('2016-08-05T14:50:14+08:00'),
  320. new DI('P1D'),
  321. new DT('2016-08-10T14:50:14+08:00')
  322. ),
  323. 'R4/2016-08-05T14:50:14+08:00/P0Y0M1DT0H0M0S',
  324. ],
  325. [
  326. new DP(
  327. new DT('2016-08-05T14:50:14+08:00'),
  328. new DI('P5D'),
  329. new DT('2016-08-10T14:50:14+08:00')
  330. ),
  331. '2016-08-05T14:50:14+08:00/P0Y0M5DT0H0M0S',
  332. ],
  333. [
  334. new DP(
  335. new DT('2016-08-05T14:50:14+08:00'),
  336. new DI('P1Y2M3DT4H5M6S'),
  337. new DT('2017-10-08T18:55:20+08:00')
  338. ),
  339. '2016-08-05T14:50:14+08:00/P1Y2M3DT4H5M6S',
  340. ],
  341. [
  342. new DP(
  343. new DT('2016-08-05T14:50:14Z'),
  344. new DI('P1D'),
  345. 0
  346. ),
  347. '2016-08-05T14:50:14+00:00/P0Y0M1DT0H0M0S',
  348. ],
  349. [
  350. new DP(
  351. new DT('2016-08-05T14:50:14Z'),
  352. new DI('PT5M'),
  353. 3
  354. ),
  355. 'R3/2016-08-05T14:50:14+00:00/P0Y0M0DT0H5M0S',
  356. ],
  357. [
  358. new DP('R3/2016-08-05T14:50:14Z/PT5M'),
  359. 'R3/2016-08-05T14:50:14+00:00/P0Y0M0DT0H5M0S',
  360. ],
  361. [
  362. new DP('R4/2016-08-05T14:50:14Z/P1Y2M3DT4H5M6S'),
  363. 'R4/2016-08-05T14:50:14+00:00/P1Y2M3DT4H5M6S',
  364. ],
  365. [
  366. DateTimeHelper::parse('2016-08-05T14:50:14Z'),
  367. '2016-08-05T14:50:14+00:00/P0Y0M0DT0H0M1S',
  368. ],
  369. [
  370. DateTimeHelper::parse('2016-08-05Z'),
  371. '2016-08-05T00:00:00+00:00/P0Y0M1DT0H0M0S',
  372. ],
  373. [
  374. DateTimeHelper::parse('2016-08-05-03:00'),
  375. '2016-08-05T00:00:00-03:00/P0Y0M1DT0H0M0S',
  376. ],
  377. ];
  378. }
  379. /**
  380. * @dataProvider periodToIsoProvider
  381. */
  382. public function testPeriodToIso($period, $iso)
  383. {
  384. $this->assertSame($iso, DateTimeHelper::periodToIso($period));
  385. }
  386. }