DateTimeHelperTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. // date
  55. ['2016-08-02', 'UTC', new DP(new DT('2016-08-02T00:00:00Z'), new DI('P1D'), 0)],
  56. ['2016-08-02', 'US/Pacific', new DP(new DT('2016-08-02T00:00:00-07:00'), new DI('P1D'), 0)],
  57. ['2016-08-02', 'US/Pacific', new DP(new DT('2016-08-02T07:00:00Z'), new DI('P1D'), 0)],
  58. ['2016-08-02+02:00', 'UTC', new DP(new DT('2016-08-02T00:00:00+02:00'), new DI('P1D'), 0)],
  59. ['2016-08-02+02:00', 'UTC', new DP(new DT('2016-08-01T22:00:00Z'), new DI('P1D'), 0)],
  60. // second
  61. ['2016-08-02 15:52:13', 'UTC', new DP(new DT('2016-08-02T15:52:13Z'), new DI('PT1S'), 0)],
  62. ['2016-08-02 15:52:13', 'Europe/Vienna', new DP(new DT('2016-08-02T15:52:13+02:00'), new DI('PT1S'), 0)],
  63. ['2016-08-02 15:52:13', 'Europe/Vienna', new DP(new DT('2016-08-02T13:52:13Z'), new DI('PT1S'), 0)],
  64. ['2016-08-02T15:52:13', 'US/Pacific', new DP(new DT('2016-08-02T15:52:13-07:00'), new DI('PT1S'), 0)],
  65. ['2016-08-02T15:52:13Z', 'US/Pacific', new DP(new DT('2016-08-02T15:52:13Z'), new DI('PT1S'), 0)],
  66. ['2016-08-02T15:52:13Z', 'Europe/Vienna', new DP(new DT('2016-08-02T17:52:13+02:00'), new DI('PT1S'), 0)],
  67. ['2016-08-02T15:52:13+00:00', 'Europe/Vienna', new DP(new DT('2016-08-02T15:52:13Z'), new DI('PT1S'), 0)],
  68. ['2016-08-02T15:52:13+02:00', 'US/Pacific', new DP(new DT('2016-08-02T15:52:13+02:00'), new DI('PT1S'), 0)],
  69. ['2016-08-02T15:52:13-08:00', 'UTC', new DP(new DT('2016-08-02T23:52:13Z'), new DI('PT1S'), 0)],
  70. ];
  71. }
  72. /**
  73. * @dataProvider parseProvider
  74. */
  75. public function testParse($text, $timezone, $expected)
  76. {
  77. date_default_timezone_set($timezone);
  78. $this->assertEquals($expected, DateTimeHelper::parse($text));
  79. }
  80. public function parseInvalidArgumentProvider()
  81. {
  82. return [
  83. [' '],
  84. [''],
  85. ['2016--12'],
  86. ['2016-10-12 08:20#01'],
  87. [1],
  88. [false],
  89. ];
  90. }
  91. /**
  92. * @dataProvider parseInvalidArgumentProvider
  93. * @expectedException \InvalidArgumentException
  94. */
  95. public function testParseInvalidArgument($text)
  96. {
  97. DateTimeHelper::parse($text);
  98. }
  99. public function parseGetStartProvider()
  100. {
  101. return [
  102. [null, 'UTC', null],
  103. [null, 'US/Pacific', null],
  104. ['2016-08-02', 'UTC', new DT('2016-08-02T00:00:00Z')],
  105. ['2016-08-02', 'Europe/Vienna', new DT('2016-08-02T00:00:00+02:00')],
  106. ['2016-08-02', 'Europe/Vienna', new DT('2016-08-01T22:00:00Z')],
  107. ['2016-08-02 15:52:13', 'UTC', new DT('2016-08-02T15:52:13Z')],
  108. ['2016-08-02 15:52:13', 'Europe/Vienna', new DT('2016-08-02T15:52:13+02:00')],
  109. ['2016-08-02 15:52:13', 'Europe/Vienna', new DT('2016-08-02T13:52:13Z')],
  110. ['2016-08-02T15:52:13', 'US/Pacific', new DT('2016-08-02T15:52:13-07:00')],
  111. ];
  112. }
  113. /**
  114. * @dataProvider parseGetStartProvider
  115. */
  116. public function testParseGetStart($text, $timezone, $expected)
  117. {
  118. date_default_timezone_set($timezone);
  119. $this->assertEquals($expected, DateTimeHelper::parseGetStart($text));
  120. }
  121. public function parseGetStartInvalidArgumentProvider()
  122. {
  123. return [
  124. [' '],
  125. [''],
  126. ['2016--12'],
  127. ['2016-10-12 08:20#01'],
  128. [1],
  129. [false],
  130. ];
  131. }
  132. /**
  133. * @dataProvider parseGetStartInvalidArgumentProvider
  134. * @expectedException \InvalidArgumentException
  135. */
  136. public function testParseGetStartInvalidArgument($text)
  137. {
  138. DateTimeHelper::parseGetStart($text);
  139. }
  140. public function deinvertIntervalProvider()
  141. {
  142. return [
  143. [
  144. DI::createFromDateString('-2 years'),
  145. ['y' => -2, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  146. ],
  147. [
  148. DI::createFromDateString('-2 months'),
  149. ['y' => 0, 'm' => -2, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  150. ],
  151. [
  152. DI::createFromDateString('-2 days'),
  153. ['y' => 0, 'm' => 0, 'd' => -2, 'h' => 0, 'i' => 0, 's' => 0],
  154. ],
  155. [
  156. DI::createFromDateString('-2 hours'),
  157. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => -2, 'i' => 0, 's' => 0],
  158. ],
  159. [
  160. DI::createFromDateString('-2 minutes'),
  161. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => -2, 's' => 0],
  162. ],
  163. [
  164. DI::createFromDateString('-2 seconds'),
  165. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => 0, 's' => -2],
  166. ],
  167. [
  168. (new DT('2016-08'))->diff(new DT('2016-07')),
  169. ['y' => 0, 'm' => -1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  170. ],
  171. [
  172. (new DT('2016-08-03'))->diff(new DT('2016-07-03')),
  173. ['y' => 0, 'm' => -1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  174. ],
  175. [
  176. (new DT('2016-07-03'))->diff(new DT('2016-08-03')),
  177. ['y' => 0, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  178. ],
  179. [
  180. (new DT('2016-08-04'))->diff(new DT('2016-07-03')),
  181. ['y' => 0, 'm' => -1, 'd' => -1, 'h' => 0, 'i' => 0, 's' => 0],
  182. ],
  183. [
  184. (new DT('2016-07-03'))->diff(new DT('2016-08-04')),
  185. ['y' => 0, 'm' => 1, 'd' => 1, 'h' => 0, 'i' => 0, 's' => 0],
  186. ],
  187. [
  188. (new DT('2016-08-02'))->diff(new DT('2016-07-03')),
  189. ['y' => 0, 'm' => 0, 'd' => -30, 'h' => 0, 'i' => 0, 's' => 0],
  190. ],
  191. [
  192. (new DT('2016-07-03'))->diff(new DT('2016-08-02')),
  193. ['y' => 0, 'm' => 0, 'd' => 30, 'h' => 0, 'i' => 0, 's' => 0],
  194. ],
  195. [
  196. (new DT('2016-08-04 18:10:02'))->diff(new DT('2016-07-03 14:13:03')),
  197. ['y' => 0, 'm' => -1, 'd' => -1, 'h' => -3, 'i' => -56, 's' => -59],
  198. ],
  199. [
  200. (new DT('2016-07-03 14:13:03'))->diff(new DT('2016-08-04 18:10:02')),
  201. ['y' => 0, 'm' => 1, 'd' => 1, 'h' => 3, 'i' => 56, 's' => 59],
  202. ],
  203. ];
  204. }
  205. /**
  206. * @dataProvider deinvertIntervalProvider
  207. */
  208. public function testDeinvertInterval($source, $expected_attr)
  209. {
  210. // \DateInterval does not implement clone.
  211. // @see https://bugs.php.net/bug.php?id=50559
  212. $source_copy = unserialize(serialize($source));
  213. $deinverted = DateTimeHelper::deinvertInterval($source_copy);
  214. $this->assertEquals($source, $source_copy);
  215. $this->assertEquals(0, $deinverted->invert);
  216. foreach($expected_attr as $k => $v) {
  217. $this->assertSame($v, $deinverted->$k);
  218. }
  219. }
  220. public function intervalToIsoProvider()
  221. {
  222. return [
  223. [null, null],
  224. ];
  225. }
  226. /**
  227. * @dataProvider intervalToIsoProvider
  228. */
  229. public function testIntervalToIso($interval, $iso)
  230. {
  231. $this->assertSame($iso, DateTimeHelper::intervalToIso($interval));
  232. }
  233. public function intervalToIsoReinitProvider()
  234. {
  235. return [
  236. [new DI('P1Y')],
  237. [new DI('P1M')],
  238. [new DI('P1D')],
  239. [new DI('PT1H')],
  240. [new DI('PT1M')],
  241. [new DI('PT1S')],
  242. [new DI('P1Y2M3DT4H5M6S')],
  243. ];
  244. }
  245. /**
  246. * @dataProvider intervalToIsoReinitProvider
  247. */
  248. public function testIntervalToIsoReinit($interval)
  249. {
  250. $iso = DateTimeHelper::intervalToIso($interval);
  251. $this->assertEquals($interval, new DI($iso));
  252. }
  253. public function intervalToIsoReinitUnsupportedProvider()
  254. {
  255. return [
  256. [DI::createFromDateString('-2 years')],
  257. [DI::createFromDateString('-2 months')],
  258. [DI::createFromDateString('-2 days')],
  259. [DI::createFromDateString('-2 hours')],
  260. [DI::createFromDateString('-2 minutes')],
  261. [DI::createFromDateString('-2 seconds')],
  262. [(new DT('2016-08-03'))->diff(new DT('2016-07-03'))],
  263. [(new DT('2016-08-03 10:00:01'))->diff(new DT('2016-08-03 10:00:00'))],
  264. ];
  265. }
  266. /**
  267. * @dataProvider intervalToIsoReinitUnsupportedProvider
  268. * @expectedException \Exception
  269. */
  270. public function testIntervalToIsoReinitUnsupported($interval)
  271. {
  272. DateTimeHelper::intervalToIso($interval);
  273. }
  274. public function periodToIsoProvider()
  275. {
  276. return [
  277. [null, null],
  278. [
  279. new DP(
  280. new DT('2016-08-05T14:50:14+08:00'),
  281. new DI('P1D'),
  282. new DT('2016-08-10T14:50:14+08:00')
  283. ),
  284. 'R4/2016-08-05T14:50:14+08:00/P0Y0M1DT0H0M0S',
  285. ],
  286. [
  287. new DP(
  288. new DT('2016-08-05T14:50:14+08:00'),
  289. new DI('P5D'),
  290. new DT('2016-08-10T14:50:14+08:00')
  291. ),
  292. '2016-08-05T14:50:14+08:00/P0Y0M5DT0H0M0S',
  293. ],
  294. [
  295. new DP(
  296. new DT('2016-08-05T14:50:14+08:00'),
  297. new DI('P1Y2M3DT4H5M6S'),
  298. new DT('2017-10-08T18:55:20+08:00')
  299. ),
  300. '2016-08-05T14:50:14+08:00/P1Y2M3DT4H5M6S',
  301. ],
  302. [
  303. new DP(
  304. new DT('2016-08-05T14:50:14Z'),
  305. new DI('P1D'),
  306. 0
  307. ),
  308. '2016-08-05T14:50:14+00:00/P0Y0M1DT0H0M0S',
  309. ],
  310. [
  311. new DP(
  312. new DT('2016-08-05T14:50:14Z'),
  313. new DI('PT5M'),
  314. 3
  315. ),
  316. 'R3/2016-08-05T14:50:14+00:00/P0Y0M0DT0H5M0S',
  317. ],
  318. [
  319. new DP('R3/2016-08-05T14:50:14Z/PT5M'),
  320. 'R3/2016-08-05T14:50:14+00:00/P0Y0M0DT0H5M0S',
  321. ],
  322. [
  323. new DP('R4/2016-08-05T14:50:14Z/P1Y2M3DT4H5M6S'),
  324. 'R4/2016-08-05T14:50:14+00:00/P1Y2M3DT4H5M6S',
  325. ],
  326. [
  327. DateTimeHelper::parse('2016-08-05T14:50:14Z'),
  328. '2016-08-05T14:50:14+00:00/P0Y0M0DT0H0M1S',
  329. ],
  330. [
  331. DateTimeHelper::parse('2016-08-05Z'),
  332. '2016-08-05T00:00:00+00:00/P0Y0M1DT0H0M0S',
  333. ],
  334. [
  335. DateTimeHelper::parse('2016-08-05-03:00'),
  336. '2016-08-05T00:00:00-03:00/P0Y0M1DT0H0M0S',
  337. ],
  338. ];
  339. }
  340. /**
  341. * @dataProvider periodToIsoProvider
  342. */
  343. public function testPeriodToIso($period, $iso)
  344. {
  345. $this->assertSame($iso, DateTimeHelper::periodToIso($period));
  346. }
  347. }