DateTimeHelperTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. ['1900', 'UTC', new DT('1900-01-01T00:00:00Z') ],
  145. ['0014', 'Europe/Vienna', new DT('0014-01-01T00:00:00+01:00')],
  146. ['2016', 'US/Pacific', new DT('2016-01-01T00:00:00-08:00')],
  147. ['1900Z', 'US/Pacific', new DT('1900-01-01T00:00:00Z') ],
  148. ['2016Z', 'Europe/Vienna', new DT('2016-01-01T00:00:00Z') ],
  149. ['2016+00:00', 'Europe/Vienna', new DT('2016-01-01T00:00:00Z') ],
  150. ['2016+02:00', 'US/Pacific', new DT('2016-01-01T00:00:00+02:00')],
  151. ['0000 +02:05', 'US/Pacific', new DT('0000-01-01T00:00:00+02:05')],
  152. ['2016-08:00', 'UTC', new DT('2016-01-01T00:00:00-08:00')],
  153. ['2016 -08:00', 'UTC', new DT('2016-01-01T00:00:00-08:00')],
  154. ['2016-08', 'UTC', new DT('2016-08-01T00:00:00Z') ],
  155. ['2016-08', 'Europe/Vienna', new DT('2016-08-01T00:00:00+02:00')],
  156. ['2016-01', 'US/Pacific', new DT('2016-01-01T00:00:00-08:00')],
  157. ['2016-08Z', 'US/Pacific', new DT('2016-08-01T00:00:00Z') ],
  158. ['2016-08Z', 'Europe/Vienna', new DT('2016-08-01T00:00:00Z') ],
  159. ['2016-01+00:00', 'Europe/Vienna', new DT('2016-01-01T00:00:00Z') ],
  160. ['2016-01+02:00', 'US/Pacific', new DT('2016-01-01T00:00:00+02:00')],
  161. ['2016-01 +02:00', 'US/Pacific', new DT('2016-01-01T00:00:00+02:00')],
  162. ['2016-01 -08:00', 'UTC', new DT('2016-01-01T00:00:00-08:00')],
  163. ['2016-08-02', 'UTC', new DT('2016-08-02T00:00:00Z') ],
  164. ['2016-08-02', 'Europe/Vienna', new DT('2016-08-02T00:00:00+02:00')],
  165. ['2016-01-02', 'US/Pacific', new DT('2016-01-02T00:00:00-08:00')],
  166. ['2016-08-02Z', 'US/Pacific', new DT('2016-08-02T00:00:00Z') ],
  167. ['2016-08-02Z', 'Europe/Vienna', new DT('2016-08-02T00:00:00Z') ],
  168. ['2016-01-02+00:00', 'Europe/Vienna', new DT('2016-01-02T00:00:00Z') ],
  169. ['2016-01-02+02:00', 'US/Pacific', new DT('2016-01-02T00:00:00+02:00')],
  170. ['2016-01-02-08:13', 'UTC', new DT('2016-01-02T00:00:00-08:13')],
  171. ['2016-08-02 15:52', 'UTC', new DT('2016-08-02T15:52:00Z') ],
  172. ['2016-08-02T15:52', 'UTC', new DT('2016-08-02T15:52:00Z') ],
  173. ['2016-08-02T15:52', 'Europe/Vienna', new DT('2016-08-02T15:52:00+02:00')],
  174. ['2016-01-02T15:52', 'US/Pacific', new DT('2016-01-02T15:52:00-08:00')],
  175. ['2016-08-02 15:52Z', 'US/Pacific', new DT('2016-08-02T15:52:00Z') ],
  176. ['2016-08-02T15:52Z', 'Europe/Vienna', new DT('2016-08-02T15:52:00Z') ],
  177. ['2016-01-02T15:52+00:00', 'Europe/Vienna', new DT('2016-01-02T15:52:00Z') ],
  178. ['2016-01-02T15:52+02:00', 'US/Pacific', new DT('2016-01-02T15:52:00+02:00')],
  179. ['2016-01-02T15:52-08:00', 'UTC', new DT('2016-01-02T15:52:00-08:00')],
  180. ['2016-08-02 15:52:13', 'UTC', new DT('2016-08-02T15:52:13Z') ],
  181. ['2016-08-02T15:52:13', 'UTC', new DT('2016-08-02T15:52:13Z') ],
  182. ['2016-08-02T15:52:13', 'Europe/Vienna', new DT('2016-08-02T15:52:13+02:00')],
  183. ['2016-01-02T15:52:00', 'US/Pacific', new DT('2016-01-02T15:52:00-08:00')],
  184. ['2016-08-02 15:52:13Z', 'US/Pacific', new DT('2016-08-02T15:52:13Z') ],
  185. ['2016-08-02T15:52:13Z', 'Europe/Vienna', new DT('2016-08-02T15:52:13Z') ],
  186. ['2016-01-02T15:52:13+00:00', 'Europe/Vienna', new DT('2016-01-02T15:52:13Z') ],
  187. ['2016-01-02T15:52:13+02:00', 'US/Pacific', new DT('2016-01-02T15:52:13+02:00')],
  188. ['2016-01-02T15:52:13-08:00', 'UTC', new DT('2016-01-02T15:52:13-08:00')],
  189. ];
  190. }
  191. /**
  192. * @dataProvider parseGetStartProvider
  193. */
  194. public function testParseGetStart($text, $timezone, $expected)
  195. {
  196. date_default_timezone_set($timezone);
  197. $this->assertEquals($expected, DateTimeHelper::parseGetStart($text));
  198. }
  199. public function parseGetStartInvalidArgumentProvider()
  200. {
  201. return [
  202. [' '],
  203. [''],
  204. ['2016--12'],
  205. ['2016-01-08:00'],
  206. ['2016-10-12 08:20#01'],
  207. [1],
  208. [false],
  209. ];
  210. }
  211. /**
  212. * @dataProvider parseGetStartInvalidArgumentProvider
  213. * @expectedException \InvalidArgumentException
  214. */
  215. public function testParseGetStartInvalidArgument($text)
  216. {
  217. DateTimeHelper::parseGetStart($text);
  218. }
  219. public function deinvertIntervalProvider()
  220. {
  221. return [
  222. [
  223. DI::createFromDateString('-2 years'),
  224. ['y' => -2, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  225. ],
  226. [
  227. DI::createFromDateString('-2 months'),
  228. ['y' => 0, 'm' => -2, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  229. ],
  230. [
  231. DI::createFromDateString('-2 days'),
  232. ['y' => 0, 'm' => 0, 'd' => -2, 'h' => 0, 'i' => 0, 's' => 0],
  233. ],
  234. [
  235. DI::createFromDateString('-2 hours'),
  236. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => -2, 'i' => 0, 's' => 0],
  237. ],
  238. [
  239. DI::createFromDateString('-2 minutes'),
  240. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => -2, 's' => 0],
  241. ],
  242. [
  243. DI::createFromDateString('-2 seconds'),
  244. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => 0, 's' => -2],
  245. ],
  246. [
  247. (new DT('2016-08'))->diff(new DT('2016-07')),
  248. ['y' => 0, 'm' => -1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  249. ],
  250. [
  251. (new DT('2016-08-03'))->diff(new DT('2016-07-03')),
  252. ['y' => 0, 'm' => -1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  253. ],
  254. [
  255. (new DT('2016-07-03'))->diff(new DT('2016-08-03')),
  256. ['y' => 0, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  257. ],
  258. [
  259. (new DT('2016-08-04'))->diff(new DT('2016-07-03')),
  260. ['y' => 0, 'm' => -1, 'd' => -1, 'h' => 0, 'i' => 0, 's' => 0],
  261. ],
  262. [
  263. (new DT('2016-07-03'))->diff(new DT('2016-08-04')),
  264. ['y' => 0, 'm' => 1, 'd' => 1, 'h' => 0, 'i' => 0, 's' => 0],
  265. ],
  266. [
  267. (new DT('2016-08-02'))->diff(new DT('2016-07-03')),
  268. ['y' => 0, 'm' => 0, 'd' => -30, 'h' => 0, 'i' => 0, 's' => 0],
  269. ],
  270. [
  271. (new DT('2016-07-03'))->diff(new DT('2016-08-02')),
  272. ['y' => 0, 'm' => 0, 'd' => 30, 'h' => 0, 'i' => 0, 's' => 0],
  273. ],
  274. [
  275. (new DT('2016-08-04 18:10:02'))->diff(new DT('2016-07-03 14:13:03')),
  276. ['y' => 0, 'm' => -1, 'd' => -1, 'h' => -3, 'i' => -56, 's' => -59],
  277. ],
  278. [
  279. (new DT('2016-07-03 14:13:03'))->diff(new DT('2016-08-04 18:10:02')),
  280. ['y' => 0, 'm' => 1, 'd' => 1, 'h' => 3, 'i' => 56, 's' => 59],
  281. ],
  282. ];
  283. }
  284. /**
  285. * @dataProvider deinvertIntervalProvider
  286. */
  287. public function testDeinvertInterval($source, $expected_attr)
  288. {
  289. // \DateInterval does not implement clone.
  290. // @see https://bugs.php.net/bug.php?id=50559
  291. $source_copy = unserialize(serialize($source));
  292. $deinverted = DateTimeHelper::deinvertInterval($source_copy);
  293. $this->assertEquals($source, $source_copy);
  294. $this->assertEquals(0, $deinverted->invert);
  295. foreach($expected_attr as $k => $v) {
  296. $this->assertSame($v, $deinverted->$k);
  297. }
  298. }
  299. public function isoProvider()
  300. {
  301. return [
  302. [null, null],
  303. [new DT('2016-09-16 21:13+02:00'), '2016-09-16T21:13:00+02:00'],
  304. [
  305. new DP(
  306. new DT('2016-08-05T14:50:14+08:00'),
  307. new DI('P1D'),
  308. new DT('2016-08-10T14:50:14+08:00')
  309. ),
  310. 'R4/2016-08-05T14:50:14+08:00/P0Y0M1DT0H0M0S',
  311. ],
  312. [
  313. new DP(
  314. new DT('2016-08-05T14:50:14+08:00'),
  315. new DI('P5D'),
  316. new DT('2016-08-10T14:50:14+08:00')
  317. ),
  318. '2016-08-05T14:50:14+08:00/P0Y0M5DT0H0M0S',
  319. ],
  320. [
  321. new DP(
  322. new DT('2016-08-05T14:50:14+08:00'),
  323. new DI('P1Y2M3DT4H5M6S'),
  324. new DT('2017-10-08T18:55:20+08:00')
  325. ),
  326. '2016-08-05T14:50:14+08:00/P1Y2M3DT4H5M6S',
  327. ],
  328. [
  329. new DP(
  330. new DT('2016-08-05T14:50:14Z'),
  331. new DI('P1D'),
  332. 0
  333. ),
  334. '2016-08-05T14:50:14+00:00/P0Y0M1DT0H0M0S',
  335. ],
  336. [
  337. new DP(
  338. new DT('2016-08-05T14:50:14Z'),
  339. new DI('PT5M'),
  340. 3
  341. ),
  342. 'R3/2016-08-05T14:50:14+00:00/P0Y0M0DT0H5M0S',
  343. ],
  344. [
  345. new DP('R3/2016-08-05T14:50:14Z/PT5M'),
  346. 'R3/2016-08-05T14:50:14+00:00/P0Y0M0DT0H5M0S',
  347. ],
  348. [
  349. new DP('R4/2016-08-05T14:50:14Z/P1Y2M3DT4H5M6S'),
  350. 'R4/2016-08-05T14:50:14+00:00/P1Y2M3DT4H5M6S',
  351. ],
  352. [
  353. DateTimeHelper::parse('2016-08-05T14:50:14Z'),
  354. '2016-08-05T14:50:14+00:00/P0Y0M0DT0H0M1S',
  355. ],
  356. [
  357. DateTimeHelper::parse('2016-08-05Z'),
  358. '2016-08-05T00:00:00+00:00/P0Y0M1DT0H0M0S',
  359. ],
  360. [
  361. DateTimeHelper::parse('2016-08-05-03:00'),
  362. '2016-08-05T00:00:00-03:00/P0Y0M1DT0H0M0S',
  363. ],
  364. ];
  365. }
  366. /**
  367. * @dataProvider isoProvider
  368. */
  369. public function testIso($interval, $iso)
  370. {
  371. $this->assertSame($iso, DateTimeHelper::iso($interval));
  372. }
  373. public function isoReinitDateTimeProvider()
  374. {
  375. return [
  376. [new DT('2016-09-16')],
  377. [new DT('2016-09-16 +02:00')],
  378. [new DT('2016-09-16 -07:13')],
  379. [new DT('2016-09-16 21:13')],
  380. [new DT('2016-09-16 21:13+02:00')],
  381. [new DT('2016-09-16 14:13-07:00')],
  382. ];
  383. }
  384. /**
  385. * @dataProvider isoReinitDateTimeProvider
  386. */
  387. public function testIsoReinitDateTime($dt)
  388. {
  389. $iso = DateTimeHelper::iso($dt);
  390. $this->assertEquals($dt, new DT($iso));
  391. }
  392. public function isoReinitIntervalProvider()
  393. {
  394. return [
  395. [new DI('P1Y')],
  396. [new DI('P1M')],
  397. [new DI('P1D')],
  398. [new DI('PT1H')],
  399. [new DI('PT1M')],
  400. [new DI('PT1S')],
  401. [new DI('P1Y2M3DT4H5M6S')],
  402. ];
  403. }
  404. /**
  405. * @dataProvider isoReinitIntervalProvider
  406. */
  407. public function testIsoReinitInterval($interval)
  408. {
  409. $iso = DateTimeHelper::iso($interval);
  410. $this->assertEquals($interval, new DI($iso));
  411. }
  412. public function isoUnsupportedProvider()
  413. {
  414. return [
  415. [DI::createFromDateString('-2 years')],
  416. [DI::createFromDateString('-2 months')],
  417. [DI::createFromDateString('-2 days')],
  418. [DI::createFromDateString('-2 hours')],
  419. [DI::createFromDateString('-2 minutes')],
  420. [DI::createFromDateString('-2 seconds')],
  421. [(new DT('2016-08-03'))->diff(new DT('2016-07-03'))],
  422. [(new DT('2016-08-03 10:00:01'))->diff(new DT('2016-08-03 10:00:00'))],
  423. [new DP(new DT('2016-08-05'), new DI('P1D'), new DT('2016-08-05'))],
  424. [new DP(new DT('2016-08-05'), new DI('PT1S'), new DT('2016-08-04'))],
  425. [new \Exception('unsupported class')],
  426. ];
  427. }
  428. /**
  429. * @dataProvider isoUnsupportedProvider
  430. * @expectedException \InvalidArgumentException
  431. */
  432. public function testIsoUnsupported($interval)
  433. {
  434. DateTimeHelper::iso($interval);
  435. }
  436. }