DateTimeHelperTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 iso6801DateFormatProvider()
  10. {
  11. return [
  12. [0, '1970-01-01'],
  13. [1456704000, '2016-02-29'],
  14. [1479202824, '2016-11-15'],
  15. ];
  16. }
  17. /**
  18. * @dataProvider iso6801DateFormatProvider
  19. */
  20. public function testIso6801DateFormat($timestamp, $expected_date)
  21. {
  22. $date = date(DateTimeHelper::ISO8601_DATE_FORMAT, $timestamp);
  23. $this->assertEquals($expected_date, $date);
  24. }
  25. public function iso6801TimeFormatProvider()
  26. {
  27. return [
  28. [0, '00:00:00'],
  29. [1456707723, '01:02:03'],
  30. [1479202824, '09:40:24'],
  31. ];
  32. }
  33. /**
  34. * @dataProvider iso6801TimeFormatProvider
  35. */
  36. public function testIso6801TimeFormat($timestamp, $expected_time)
  37. {
  38. $time = date(DateTimeHelper::ISO8601_TIME_FORMAT, $timestamp);
  39. $this->assertEquals($expected_time, $time);
  40. }
  41. public function timestampToDateTimeProvider()
  42. {
  43. return [
  44. [null, null],
  45. [0, new DT('1970-01-01 00:00:00', new \DateTimeZone('UTC'))],
  46. [0, new DT('1970-01-01 01:00:00', new \DateTimeZone('Europe/Vienna'))],
  47. [1234567890, new DT('2009-02-13 23:31:30', new \DateTimeZone('UTC'))],
  48. [1234567890, new DT('2009-02-14 00:31:30', new \DateTimeZone('Europe/Vienna'))],
  49. [-3600, new DT('1970-01-01 00:00:00', new \DateTimeZone('Europe/Vienna'))],
  50. ];
  51. }
  52. /**
  53. * @dataProvider timestampToDateTimeProvider
  54. */
  55. public function testTimestampToDateTime($timestamp, $expected_datetime)
  56. {
  57. $generated_datetime = DateTimeHelper::timestampToDateTime($timestamp);
  58. $this->assertEquals($expected_datetime, $generated_datetime);
  59. }
  60. public function timestampToDateTimeDefaultTimezoneProvider()
  61. {
  62. return [
  63. ['UTC', 100],
  64. ['Europe/Vienna', 0],
  65. ['Europe/Vienna', -100],
  66. ['Europe/Vienna', 100],
  67. ['Europe/London', 3600],
  68. ['US/Pacific', 3600],
  69. ];
  70. }
  71. /**
  72. * @dataProvider timestampToDateTimeDefaultTimezoneProvider
  73. */
  74. public function testTimestampToDateTimeDefaultTimezone($timezone, $timestamp)
  75. {
  76. date_default_timezone_set($timezone);
  77. $generated_datetime = DateTimeHelper::timestampToDateTime($timestamp);
  78. $this->assertSame($timestamp, $generated_datetime->getTimestamp());
  79. }
  80. public function parseProvider()
  81. {
  82. return [
  83. // null
  84. [null, 'UTC', null],
  85. [null, 'US/Pacific', null],
  86. // year without timezone
  87. ['1900', 'UTC', new DP(new DT('1900-01-01T00:00:00Z'), new DI('P1Y'), 0)],
  88. ['0014', 'Europe/Vienna', new DP(new DT('0014-01-01T00:00:00+01:00'), new DI('P1Y'), 0)],
  89. ['2016', 'US/Pacific', new DP(new DT('2016-01-01T00:00:00-08:00'), new DI('P1Y'), 0)],
  90. // year with timezone
  91. ['1900Z', 'US/Pacific', new DP(new DT('1900-01-01T00:00:00Z'), new DI('P1Y'), 0)],
  92. ['2016Z', 'Europe/Vienna', new DP(new DT('2016-01-01T00:00:00Z'), new DI('P1Y'), 0)],
  93. ['2016+00:00', 'Europe/Vienna', new DP(new DT('2016-01-01T00:00:00Z'), new DI('P1Y'), 0)],
  94. ['2016+02:00', 'US/Pacific', new DP(new DT('2016-01-01T00:00:00+02:00'), new DI('P1Y'), 0)],
  95. ['0000 +02:05', 'US/Pacific', new DP(new DT('0000-01-01T00:00:00+02:05'), new DI('P1Y'), 0)],
  96. ['2016-08:00', 'UTC', new DP(new DT('2016-01-01T00:00:00-08:00'), new DI('P1Y'), 0)],
  97. ['2016 -08:00', 'UTC', new DP(new DT('2016-01-01T00:00:00-08:00'), new DI('P1Y'), 0)],
  98. // month without timezone
  99. ['2016-08', 'UTC', new DP(new DT('2016-08-01T00:00:00Z'), new DI('P1M'), 0)],
  100. ['2016-08', 'Europe/Vienna', new DP(new DT('2016-08-01T00:00:00+02:00'), new DI('P1M'), 0)],
  101. ['2016-01', 'US/Pacific', new DP(new DT('2016-01-01T00:00:00-08:00'), new DI('P1M'), 0)],
  102. // month with timezone
  103. ['2016-08Z', 'US/Pacific', new DP(new DT('2016-08-01T00:00:00Z'), new DI('P1M'), 0)],
  104. ['2016-08Z', 'Europe/Vienna', new DP(new DT('2016-08-01T00:00:00Z'), new DI('P1M'), 0)],
  105. ['2016-01+00:00', 'Europe/Vienna', new DP(new DT('2016-01-01T00:00:00Z'), new DI('P1M'), 0)],
  106. ['2016-01+02:00', 'US/Pacific', new DP(new DT('2016-01-01T00:00:00+02:00'), new DI('P1M'), 0)],
  107. ['2016-01 +02:00', 'US/Pacific', new DP(new DT('2016-01-01T00:00:00+02:00'), new DI('P1M'), 0)],
  108. ['2016-01 -08:00', 'UTC', new DP(new DT('2016-01-01T00:00:00-08:00'), new DI('P1M'), 0)],
  109. // date without timezone
  110. ['2016-08-02', 'UTC', new DP(new DT('2016-08-02T00:00:00Z'), new DI('P1D'), 0)],
  111. ['2016-08-02', 'Europe/Vienna', new DP(new DT('2016-08-02T00:00:00+02:00'), new DI('P1D'), 0)],
  112. ['2016-01-02', 'US/Pacific', new DP(new DT('2016-01-02T00:00:00-08:00'), new DI('P1D'), 0)],
  113. // date with timezone
  114. ['2016-08-02Z', 'US/Pacific', new DP(new DT('2016-08-02T00:00:00Z'), new DI('P1D'), 0)],
  115. ['2016-08-02Z', 'Europe/Vienna', new DP(new DT('2016-08-02T00:00:00Z'), new DI('P1D'), 0)],
  116. ['2016-01-02+00:00', 'Europe/Vienna', new DP(new DT('2016-01-02T00:00:00Z'), new DI('P1D'), 0)],
  117. ['2016-01-02+02:00', 'US/Pacific', new DP(new DT('2016-01-02T00:00:00+02:00'), new DI('P1D'), 0)],
  118. ['2016-01-02-08:13', 'UTC', new DP(new DT('2016-01-02T00:00:00-08:13'), new DI('P1D'), 0)],
  119. // minute without timezone
  120. ['2016-08-02 15:52', 'UTC', new DP(new DT('2016-08-02T15:52:00Z'), new DI('PT1M'), 0)],
  121. ['2016-08-02T15:52', 'UTC', new DP(new DT('2016-08-02T15:52:00Z'), new DI('PT1M'), 0)],
  122. ['2016-08-02T15:52', 'Europe/Vienna', new DP(new DT('2016-08-02T15:52:00+02:00'), new DI('PT1M'), 0)],
  123. ['2016-01-02T15:52', 'US/Pacific', new DP(new DT('2016-01-02T15:52:00-08:00'), new DI('PT1M'), 0)],
  124. // minute with timezone
  125. ['2016-08-02 15:52Z', 'US/Pacific', new DP(new DT('2016-08-02T15:52:00Z'), new DI('PT1M'), 0)],
  126. ['2016-08-02T15:52Z', 'Europe/Vienna', new DP(new DT('2016-08-02T15:52:00Z'), new DI('PT1M'), 0)],
  127. ['2016-01-02T15:52+00:00', 'Europe/Vienna', new DP(new DT('2016-01-02T15:52:00Z'), new DI('PT1M'), 0)],
  128. ['2016-01-02T15:52+02:00', 'US/Pacific', new DP(new DT('2016-01-02T15:52:00+02:00'), new DI('PT1M'), 0)],
  129. ['2016-01-02T15:52-08:00', 'UTC', new DP(new DT('2016-01-02T15:52:00-08:00'), new DI('PT1M'), 0)],
  130. // second without timezone
  131. ['2016-08-02 15:52:13', 'UTC', new DP(new DT('2016-08-02T15:52:13Z'), new DI('PT1S'), 0)],
  132. ['2016-08-02T15:52:13', 'UTC', new DP(new DT('2016-08-02T15:52:13Z'), new DI('PT1S'), 0)],
  133. ['2016-08-02T15:52:13', 'Europe/Vienna', new DP(new DT('2016-08-02T15:52:13+02:00'), new DI('PT1S'), 0)],
  134. ['2016-01-02T15:52:00', 'US/Pacific', new DP(new DT('2016-01-02T15:52:00-08:00'), new DI('PT1S'), 0)],
  135. // second with timezone
  136. ['2016-08-02 15:52:13Z', 'US/Pacific', new DP(new DT('2016-08-02T15:52:13Z'), new DI('PT1S'), 0)],
  137. ['2016-08-02T15:52:13Z', 'Europe/Vienna', new DP(new DT('2016-08-02T15:52:13Z'), new DI('PT1S'), 0)],
  138. ['2016-01-02T15:52:13+00:00', 'Europe/Vienna', new DP(new DT('2016-01-02T15:52:13Z'), new DI('PT1S'), 0)],
  139. ['2016-01-02T15:52:13+02:00', 'US/Pacific', new DP(new DT('2016-01-02T15:52:13+02:00'), new DI('PT1S'), 0)],
  140. ['2016-01-02T15:52:13-08:00', 'UTC', new DP(new DT('2016-01-02T15:52:13-08:00'), new DI('PT1S'), 0)],
  141. ];
  142. }
  143. /**
  144. * @dataProvider parseProvider
  145. */
  146. public function testParse($text, $timezone, $expected)
  147. {
  148. date_default_timezone_set($timezone);
  149. $this->assertEquals($expected, DateTimeHelper::parse($text));
  150. }
  151. public function parseInvalidArgumentProvider()
  152. {
  153. return [
  154. [' '],
  155. [''],
  156. ['2016--12'],
  157. ['2016-01-08:00'],
  158. ['2016-10-12 08:20#01'],
  159. [1],
  160. [false],
  161. ];
  162. }
  163. /**
  164. * @dataProvider parseInvalidArgumentProvider
  165. * @expectedException \InvalidArgumentException
  166. */
  167. public function testParseInvalidArgument($text)
  168. {
  169. DateTimeHelper::parse($text);
  170. }
  171. public function parseGetStartProvider()
  172. {
  173. return [
  174. [null, 'UTC', null],
  175. [null, 'US/Pacific', null],
  176. ['1900', 'UTC', new DT('1900-01-01T00:00:00Z') ],
  177. ['0014', 'Europe/Vienna', new DT('0014-01-01T00:00:00+01:00')],
  178. ['2016', 'US/Pacific', new DT('2016-01-01T00:00:00-08:00')],
  179. ['1900Z', 'US/Pacific', new DT('1900-01-01T00:00:00Z') ],
  180. ['2016Z', 'Europe/Vienna', new DT('2016-01-01T00:00:00Z') ],
  181. ['2016+00:00', 'Europe/Vienna', new DT('2016-01-01T00:00:00Z') ],
  182. ['2016+02:00', 'US/Pacific', new DT('2016-01-01T00:00:00+02:00')],
  183. ['0000 +02:05', 'US/Pacific', new DT('0000-01-01T00:00:00+02:05')],
  184. ['2016-08:00', 'UTC', new DT('2016-01-01T00:00:00-08:00')],
  185. ['2016 -08:00', 'UTC', new DT('2016-01-01T00:00:00-08:00')],
  186. ['2016-08', 'UTC', new DT('2016-08-01T00:00:00Z') ],
  187. ['2016-08', 'Europe/Vienna', new DT('2016-08-01T00:00:00+02:00')],
  188. ['2016-01', 'US/Pacific', new DT('2016-01-01T00:00:00-08:00')],
  189. ['2016-08Z', 'US/Pacific', new DT('2016-08-01T00:00:00Z') ],
  190. ['2016-08Z', 'Europe/Vienna', new DT('2016-08-01T00:00:00Z') ],
  191. ['2016-01+00:00', 'Europe/Vienna', new DT('2016-01-01T00:00:00Z') ],
  192. ['2016-01+02:00', 'US/Pacific', new DT('2016-01-01T00:00:00+02:00')],
  193. ['2016-01 +02:00', 'US/Pacific', new DT('2016-01-01T00:00:00+02:00')],
  194. ['2016-01 -08:00', 'UTC', new DT('2016-01-01T00:00:00-08:00')],
  195. ['2016-08-02', 'UTC', new DT('2016-08-02T00:00:00Z') ],
  196. ['2016-08-02', 'Europe/Vienna', new DT('2016-08-02T00:00:00+02:00')],
  197. ['2016-01-02', 'US/Pacific', new DT('2016-01-02T00:00:00-08:00')],
  198. ['2016-08-02Z', 'US/Pacific', new DT('2016-08-02T00:00:00Z') ],
  199. ['2016-08-02Z', 'Europe/Vienna', new DT('2016-08-02T00:00:00Z') ],
  200. ['2016-01-02+00:00', 'Europe/Vienna', new DT('2016-01-02T00:00:00Z') ],
  201. ['2016-01-02+02:00', 'US/Pacific', new DT('2016-01-02T00:00:00+02:00')],
  202. ['2016-01-02-08:13', 'UTC', new DT('2016-01-02T00:00:00-08:13')],
  203. ['2016-08-02 15:52', 'UTC', new DT('2016-08-02T15:52:00Z') ],
  204. ['2016-08-02T15:52', 'UTC', new DT('2016-08-02T15:52:00Z') ],
  205. ['2016-08-02T15:52', 'Europe/Vienna', new DT('2016-08-02T15:52:00+02:00')],
  206. ['2016-01-02T15:52', 'US/Pacific', new DT('2016-01-02T15:52:00-08:00')],
  207. ['2016-08-02 15:52Z', 'US/Pacific', new DT('2016-08-02T15:52:00Z') ],
  208. ['2016-08-02T15:52Z', 'Europe/Vienna', new DT('2016-08-02T15:52:00Z') ],
  209. ['2016-01-02T15:52+00:00', 'Europe/Vienna', new DT('2016-01-02T15:52:00Z') ],
  210. ['2016-01-02T15:52+02:00', 'US/Pacific', new DT('2016-01-02T15:52:00+02:00')],
  211. ['2016-01-02T15:52-08:00', 'UTC', new DT('2016-01-02T15:52:00-08:00')],
  212. ['2016-08-02 15:52:13', 'UTC', new DT('2016-08-02T15:52:13Z') ],
  213. ['2016-08-02T15:52:13', 'UTC', new DT('2016-08-02T15:52:13Z') ],
  214. ['2016-08-02T15:52:13', 'Europe/Vienna', new DT('2016-08-02T15:52:13+02:00')],
  215. ['2016-01-02T15:52:00', 'US/Pacific', new DT('2016-01-02T15:52:00-08:00')],
  216. ['2016-08-02 15:52:13Z', 'US/Pacific', new DT('2016-08-02T15:52:13Z') ],
  217. ['2016-08-02T15:52:13Z', 'Europe/Vienna', new DT('2016-08-02T15:52:13Z') ],
  218. ['2016-01-02T15:52:13+00:00', 'Europe/Vienna', new DT('2016-01-02T15:52:13Z') ],
  219. ['2016-01-02T15:52:13+02:00', 'US/Pacific', new DT('2016-01-02T15:52:13+02:00')],
  220. ['2016-01-02T15:52:13-08:00', 'UTC', new DT('2016-01-02T15:52:13-08:00')],
  221. ];
  222. }
  223. /**
  224. * @dataProvider parseGetStartProvider
  225. */
  226. public function testParseGetStart($text, $timezone, $expected)
  227. {
  228. date_default_timezone_set($timezone);
  229. $this->assertEquals($expected, DateTimeHelper::parseGetStart($text));
  230. }
  231. public function parseGetStartInvalidArgumentProvider()
  232. {
  233. return [
  234. [' '],
  235. [''],
  236. ['2016--12'],
  237. ['2016-01-08:00'],
  238. ['2016-10-12 08:20#01'],
  239. [1],
  240. [false],
  241. ];
  242. }
  243. /**
  244. * @dataProvider parseGetStartInvalidArgumentProvider
  245. * @expectedException \InvalidArgumentException
  246. */
  247. public function testParseGetStartInvalidArgument($text)
  248. {
  249. DateTimeHelper::parseGetStart($text);
  250. }
  251. public function deinvertIntervalProvider()
  252. {
  253. return [
  254. [
  255. DI::createFromDateString('-2 years'),
  256. ['y' => -2, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  257. ],
  258. [
  259. DI::createFromDateString('-2 months'),
  260. ['y' => 0, 'm' => -2, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  261. ],
  262. [
  263. DI::createFromDateString('-2 days'),
  264. ['y' => 0, 'm' => 0, 'd' => -2, 'h' => 0, 'i' => 0, 's' => 0],
  265. ],
  266. [
  267. DI::createFromDateString('-2 hours'),
  268. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => -2, 'i' => 0, 's' => 0],
  269. ],
  270. [
  271. DI::createFromDateString('-2 minutes'),
  272. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => -2, 's' => 0],
  273. ],
  274. [
  275. DI::createFromDateString('-2 seconds'),
  276. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => 0, 's' => -2],
  277. ],
  278. [
  279. (new DT('2016-08'))->diff(new DT('2016-07')),
  280. ['y' => 0, 'm' => -1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  281. ],
  282. [
  283. (new DT('2016-08-03'))->diff(new DT('2016-07-03')),
  284. ['y' => 0, 'm' => -1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  285. ],
  286. [
  287. (new DT('2016-07-03'))->diff(new DT('2016-08-03')),
  288. ['y' => 0, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  289. ],
  290. [
  291. (new DT('2016-08-04'))->diff(new DT('2016-07-03')),
  292. ['y' => 0, 'm' => -1, 'd' => -1, 'h' => 0, 'i' => 0, 's' => 0],
  293. ],
  294. [
  295. (new DT('2016-07-03'))->diff(new DT('2016-08-04')),
  296. ['y' => 0, 'm' => 1, 'd' => 1, 'h' => 0, 'i' => 0, 's' => 0],
  297. ],
  298. [
  299. (new DT('2016-08-02'))->diff(new DT('2016-07-03')),
  300. ['y' => 0, 'm' => 0, 'd' => -30, 'h' => 0, 'i' => 0, 's' => 0],
  301. ],
  302. [
  303. (new DT('2016-07-03'))->diff(new DT('2016-08-02')),
  304. ['y' => 0, 'm' => 0, 'd' => 30, 'h' => 0, 'i' => 0, 's' => 0],
  305. ],
  306. [
  307. (new DT('2016-08-04 18:10:02'))->diff(new DT('2016-07-03 14:13:03')),
  308. ['y' => 0, 'm' => -1, 'd' => -1, 'h' => -3, 'i' => -56, 's' => -59],
  309. ],
  310. [
  311. (new DT('2016-07-03 14:13:03'))->diff(new DT('2016-08-04 18:10:02')),
  312. ['y' => 0, 'm' => 1, 'd' => 1, 'h' => 3, 'i' => 56, 's' => 59],
  313. ],
  314. ];
  315. }
  316. /**
  317. * @dataProvider deinvertIntervalProvider
  318. */
  319. public function testDeinvertInterval($source, $expected_attr)
  320. {
  321. // \DateInterval does not implement clone.
  322. // @see https://bugs.php.net/bug.php?id=50559
  323. $source_copy = unserialize(serialize($source));
  324. $deinverted = DateTimeHelper::deinvertInterval($source_copy);
  325. $this->assertEquals($source, $source_copy);
  326. $this->assertEquals(0, $deinverted->invert);
  327. foreach($expected_attr as $k => $v) {
  328. $this->assertSame($v, $deinverted->$k);
  329. }
  330. }
  331. public function isoProvider()
  332. {
  333. return [
  334. [null, null],
  335. [new DT('2016-09-16 21:13+02:00'), '2016-09-16T21:13:00+02:00'],
  336. // \DateInterval::__construct() does not support negative specifiers.
  337. // e.g. 'P-1Y'
  338. // 0 years
  339. [new DI('P0Y0M0DT0H0M0S'), 'P0S' ],
  340. [new DI('P0Y0M0DT0H0M6S'), 'PT6S' ],
  341. [new DI('P0Y0M0DT0H5M0S'), 'PT5M' ],
  342. [new DI('P0Y0M0DT0H5M6S'), 'PT5M6S' ],
  343. [new DI('P0Y0M0DT4H0M0S'), 'PT4H' ],
  344. [new DI('P0Y0M3DT0H0M0S'), 'P3D' ],
  345. [new DI('P0Y0M3DT0H0M6S'), 'P3DT6S' ],
  346. [new DI('P0Y0M3DT4H5M6S'), 'P3DT4H5M6S' ],
  347. [new DI('P0Y2M0DT0H0M0S'), 'P2M' ],
  348. [new DI('P0Y2M0DT0H0M6S'), 'P2MT6S' ],
  349. [new DI('P0Y2M3DT0H0M6S'), 'P2M3DT6S' ],
  350. // >0 years, 0 months
  351. [new DI('P1Y0M0DT0H0M0S'), 'P1Y' ],
  352. [new DI('P1Y0M0DT0H0M6S'), 'P1YT6S' ],
  353. [new DI('P1Y0M0DT0H5M0S'), 'P1YT5M' ],
  354. [new DI('P1Y0M0DT0H5M6S'), 'P1YT5M6S' ],
  355. [new DI('P1Y0M0DT4H0M0S'), 'P1YT4H' ],
  356. [new DI('P1Y0M3DT0H0M0S'), 'P1Y3D' ],
  357. [new DI('P1Y0M3DT0H0M6S'), 'P1Y3DT6S' ],
  358. [new DI('P1Y0M3DT4H5M6S'), 'P1Y3DT4H5M6S' ],
  359. // >0 years, >0 months, 0 days (complete)
  360. [new DI('P1Y2M0DT0H0M0S'), 'P1Y2M' ],
  361. [new DI('P1Y2M0DT0H0M6S'), 'P1Y2MT6S' ],
  362. [new DI('P1Y2M0DT0H5M0S'), 'P1Y2MT5M' ],
  363. [new DI('P1Y2M0DT0H5M6S'), 'P1Y2MT5M6S' ],
  364. [new DI('P1Y2M0DT4H0M0S'), 'P1Y2MT4H' ],
  365. [new DI('P1Y2M0DT4H0M6S'), 'P1Y2MT4H6S' ],
  366. [new DI('P1Y2M0DT4H5M0S'), 'P1Y2MT4H5M' ],
  367. [new DI('P1Y2M0DT4H5M6S'), 'P1Y2MT4H5M6S' ],
  368. // >0 years, >0 months, >0 days (complete)
  369. [new DI('P1Y2M3DT0H0M0S'), 'P1Y2M3D' ],
  370. [new DI('P1Y2M3DT0H0M6S'), 'P1Y2M3DT6S' ],
  371. [new DI('P1Y2M3DT0H5M0S'), 'P1Y2M3DT5M' ],
  372. [new DI('P1Y2M3DT0H5M6S'), 'P1Y2M3DT5M6S' ],
  373. [new DI('P1Y2M3DT4H0M0S'), 'P1Y2M3DT4H' ],
  374. [new DI('P1Y2M3DT4H0M6S'), 'P1Y2M3DT4H6S' ],
  375. [new DI('P1Y2M3DT4H5M0S'), 'P1Y2M3DT4H5M' ],
  376. [new DI('P1Y2M3DT4H5M6S'), 'P1Y2M3DT4H5M6S'],
  377. [
  378. new DP(
  379. new DT('2016-08-05T14:50:14+08:00'),
  380. new DI('P1D'),
  381. new DT('2016-08-10T14:50:14+08:00')
  382. ),
  383. 'R4/2016-08-05T14:50:14+08:00/P1D',
  384. ],
  385. [
  386. new DP(
  387. new DT('2016-08-05T14:50:14+08:00'),
  388. new DI('P5D'),
  389. new DT('2016-08-10T14:50:14+08:00')
  390. ),
  391. '2016-08-05T14:50:14+08:00/P5D',
  392. ],
  393. [
  394. new DP(
  395. new DT('2016-08-05T14:50:14+08:00'),
  396. new DI('P1Y2M3DT4H5M6S'),
  397. new DT('2017-10-08T18:55:20+08:00')
  398. ),
  399. '2016-08-05T14:50:14+08:00/P1Y2M3DT4H5M6S',
  400. ],
  401. [
  402. new DP(
  403. new DT('2016-08-05T14:50:14Z'),
  404. new DI('P1D'),
  405. 0
  406. ),
  407. '2016-08-05T14:50:14+00:00/P1D',
  408. ],
  409. [
  410. new DP(
  411. new DT('2016-08-05T14:50:14Z'),
  412. new DI('PT5M'),
  413. 3
  414. ),
  415. 'R3/2016-08-05T14:50:14+00:00/PT5M',
  416. ],
  417. [
  418. new DP('R3/2016-08-05T14:50:14Z/PT5M'),
  419. 'R3/2016-08-05T14:50:14+00:00/PT5M',
  420. ],
  421. [
  422. new DP('R4/2016-08-05T14:50:14Z/P1Y2M3DT4H5M6S'),
  423. 'R4/2016-08-05T14:50:14+00:00/P1Y2M3DT4H5M6S',
  424. ],
  425. [
  426. DateTimeHelper::parse('2016-08-05T14:50:14Z'),
  427. '2016-08-05T14:50:14+00:00/PT1S',
  428. ],
  429. [
  430. DateTimeHelper::parse('2016-08-05Z'),
  431. '2016-08-05T00:00:00+00:00/P1D',
  432. ],
  433. [
  434. DateTimeHelper::parse('2016-08-05-03:00'),
  435. '2016-08-05T00:00:00-03:00/P1D',
  436. ],
  437. ];
  438. }
  439. /**
  440. * @dataProvider isoProvider
  441. */
  442. public function testIso($interval, $iso)
  443. {
  444. $this->assertSame($iso, DateTimeHelper::iso($interval));
  445. }
  446. public function isoReinitProvider()
  447. {
  448. return [
  449. [new DT('2016-09-16')],
  450. [new DT('2016-09-16 +02:00')],
  451. [new DT('2016-09-16 -07:13')],
  452. [new DT('2016-09-16 21:13')],
  453. [new DT('2016-09-16 21:13+02:00')],
  454. [new DT('2016-09-16 14:13-07:00')],
  455. [new DI('P1Y')],
  456. [new DI('P1M')],
  457. [new DI('P1D')],
  458. [new DI('PT1H')],
  459. [new DI('PT1M')],
  460. [new DI('PT1S')],
  461. [new DI('P3DT1S')],
  462. [new DI('P1Y2M3DT4H5M6S')],
  463. ];
  464. }
  465. /**
  466. * @dataProvider isoReinitProvider
  467. */
  468. public function testIsoReinit($obj)
  469. {
  470. $iso = DateTimeHelper::iso($obj);
  471. $class = get_class($obj);
  472. $this->assertEquals($obj, new $class($iso));
  473. }
  474. public function isoUnsupportedProvider()
  475. {
  476. return [
  477. [DI::createFromDateString('-2 years')],
  478. [DI::createFromDateString('-2 months')],
  479. [DI::createFromDateString('-2 days')],
  480. [DI::createFromDateString('-2 hours')],
  481. [DI::createFromDateString('-2 minutes')],
  482. [DI::createFromDateString('-2 seconds')],
  483. [(new DT('2016-08-03'))->diff(new DT('2016-07-03'))],
  484. [(new DT('2016-08-03 10:00:01'))->diff(new DT('2016-08-03 10:00:00'))],
  485. [new DP(new DT('2016-08-05'), new DI('P1D'), new DT('2016-08-05'))],
  486. [new DP(new DT('2016-08-05'), new DI('PT1S'), new DT('2016-08-04'))],
  487. [new \Exception('unsupported class')],
  488. ];
  489. }
  490. /**
  491. * @dataProvider isoUnsupportedProvider
  492. * @expectedException \InvalidArgumentException
  493. */
  494. public function testIsoUnsupported($interval)
  495. {
  496. DateTimeHelper::iso($interval);
  497. }
  498. }