123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- <?php
- namespace fphammerle\helpers\tests;
- use \DateInterval as DI;
- use \DatePeriod as DP;
- use \DateTime as DT;
- use \fphammerle\helpers\DateTimeHelper;
- class DateTimeHelperTest extends \PHPUnit_Framework_TestCase
- {
- public function iso6801DateFormatProvider()
- {
- return [
- [0, '1970-01-01'],
- [1456704000, '2016-02-29'],
- [1479202824, '2016-11-15'],
- ];
- }
- /**
- * @dataProvider iso6801DateFormatProvider
- */
- public function testIso6801DateFormat($timestamp, $expected_date)
- {
- $date = date(DateTimeHelper::ISO8601_DATE_FORMAT, $timestamp);
- $this->assertEquals($expected_date, $date);
- }
- public function iso6801TimeFormatProvider()
- {
- return [
- [0, '00:00:00'],
- [1456707723, '01:02:03'],
- [1479202824, '09:40:24'],
- ];
- }
- /**
- * @dataProvider iso6801TimeFormatProvider
- */
- public function testIso6801TimeFormat($timestamp, $expected_time)
- {
- $time = date(DateTimeHelper::ISO8601_TIME_FORMAT, $timestamp);
- $this->assertEquals($expected_time, $time);
- }
- public function timestampToDateTimeProvider()
- {
- return [
- [null, null],
- [0, new DT('1970-01-01 00:00:00', new \DateTimeZone('UTC'))],
- [0, new DT('1970-01-01 01:00:00', new \DateTimeZone('Europe/Vienna'))],
- [1234567890, new DT('2009-02-13 23:31:30', new \DateTimeZone('UTC'))],
- [1234567890, new DT('2009-02-14 00:31:30', new \DateTimeZone('Europe/Vienna'))],
- [-3600, new DT('1970-01-01 00:00:00', new \DateTimeZone('Europe/Vienna'))],
- ];
- }
- /**
- * @dataProvider timestampToDateTimeProvider
- */
- public function testTimestampToDateTime($timestamp, $expected_datetime)
- {
- $generated_datetime = DateTimeHelper::timestampToDateTime($timestamp);
- $this->assertEquals($expected_datetime, $generated_datetime);
- }
- public function timestampToDateTimeDefaultTimezoneProvider()
- {
- return [
- ['UTC', 100],
- ['Europe/Vienna', 0],
- ['Europe/Vienna', -100],
- ['Europe/Vienna', 100],
- ['Europe/London', 3600],
- ['US/Pacific', 3600],
- ];
- }
- /**
- * @dataProvider timestampToDateTimeDefaultTimezoneProvider
- */
- public function testTimestampToDateTimeDefaultTimezone($timezone, $timestamp)
- {
- date_default_timezone_set($timezone);
- $generated_datetime = DateTimeHelper::timestampToDateTime($timestamp);
- $this->assertSame($timestamp, $generated_datetime->getTimestamp());
- }
- public function parseProvider()
- {
- return [
- // null
- [null, 'UTC', null],
- [null, 'US/Pacific', null],
- // year without timezone
- ['1900', 'UTC', new DP(new DT('1900-01-01T00:00:00Z'), new DI('P1Y'), 0)],
- ['0014', 'Europe/Vienna', new DP(new DT('0014-01-01T00:00:00+01:00'), new DI('P1Y'), 0)],
- ['2016', 'US/Pacific', new DP(new DT('2016-01-01T00:00:00-08:00'), new DI('P1Y'), 0)],
- // year with timezone
- ['1900Z', 'US/Pacific', new DP(new DT('1900-01-01T00:00:00Z'), new DI('P1Y'), 0)],
- ['2016Z', 'Europe/Vienna', new DP(new DT('2016-01-01T00:00:00Z'), new DI('P1Y'), 0)],
- ['2016+00:00', 'Europe/Vienna', new DP(new DT('2016-01-01T00:00:00Z'), new DI('P1Y'), 0)],
- ['2016+02:00', 'US/Pacific', new DP(new DT('2016-01-01T00:00:00+02:00'), new DI('P1Y'), 0)],
- ['0000 +02:05', 'US/Pacific', new DP(new DT('0000-01-01T00:00:00+02:05'), new DI('P1Y'), 0)],
- ['2016-08:00', 'UTC', new DP(new DT('2016-01-01T00:00:00-08:00'), new DI('P1Y'), 0)],
- ['2016 -08:00', 'UTC', new DP(new DT('2016-01-01T00:00:00-08:00'), new DI('P1Y'), 0)],
- // month without timezone
- ['2016-08', 'UTC', new DP(new DT('2016-08-01T00:00:00Z'), new DI('P1M'), 0)],
- ['2016-08', 'Europe/Vienna', new DP(new DT('2016-08-01T00:00:00+02:00'), new DI('P1M'), 0)],
- ['2016-01', 'US/Pacific', new DP(new DT('2016-01-01T00:00:00-08:00'), new DI('P1M'), 0)],
- // month with timezone
- ['2016-08Z', 'US/Pacific', new DP(new DT('2016-08-01T00:00:00Z'), new DI('P1M'), 0)],
- ['2016-08Z', 'Europe/Vienna', new DP(new DT('2016-08-01T00:00:00Z'), new DI('P1M'), 0)],
- ['2016-01+00:00', 'Europe/Vienna', new DP(new DT('2016-01-01T00:00:00Z'), new DI('P1M'), 0)],
- ['2016-01+02:00', 'US/Pacific', new DP(new DT('2016-01-01T00:00:00+02:00'), new DI('P1M'), 0)],
- ['2016-01 +02:00', 'US/Pacific', new DP(new DT('2016-01-01T00:00:00+02:00'), new DI('P1M'), 0)],
- ['2016-01 -08:00', 'UTC', new DP(new DT('2016-01-01T00:00:00-08:00'), new DI('P1M'), 0)],
- // date without timezone
- ['2016-08-02', 'UTC', new DP(new DT('2016-08-02T00:00:00Z'), new DI('P1D'), 0)],
- ['2016-08-02', 'Europe/Vienna', new DP(new DT('2016-08-02T00:00:00+02:00'), new DI('P1D'), 0)],
- ['2016-01-02', 'US/Pacific', new DP(new DT('2016-01-02T00:00:00-08:00'), new DI('P1D'), 0)],
- // date with timezone
- ['2016-08-02Z', 'US/Pacific', new DP(new DT('2016-08-02T00:00:00Z'), new DI('P1D'), 0)],
- ['2016-08-02Z', 'Europe/Vienna', new DP(new DT('2016-08-02T00:00:00Z'), new DI('P1D'), 0)],
- ['2016-01-02+00:00', 'Europe/Vienna', new DP(new DT('2016-01-02T00:00:00Z'), new DI('P1D'), 0)],
- ['2016-01-02+02:00', 'US/Pacific', new DP(new DT('2016-01-02T00:00:00+02:00'), new DI('P1D'), 0)],
- ['2016-01-02-08:13', 'UTC', new DP(new DT('2016-01-02T00:00:00-08:13'), new DI('P1D'), 0)],
- // minute without timezone
- ['2016-08-02 15:52', 'UTC', new DP(new DT('2016-08-02T15:52:00Z'), new DI('PT1M'), 0)],
- ['2016-08-02T15:52', 'UTC', new DP(new DT('2016-08-02T15:52:00Z'), new DI('PT1M'), 0)],
- ['2016-08-02T15:52', 'Europe/Vienna', new DP(new DT('2016-08-02T15:52:00+02:00'), new DI('PT1M'), 0)],
- ['2016-01-02T15:52', 'US/Pacific', new DP(new DT('2016-01-02T15:52:00-08:00'), new DI('PT1M'), 0)],
- // minute with timezone
- ['2016-08-02 15:52Z', 'US/Pacific', new DP(new DT('2016-08-02T15:52:00Z'), new DI('PT1M'), 0)],
- ['2016-08-02T15:52Z', 'Europe/Vienna', new DP(new DT('2016-08-02T15:52:00Z'), new DI('PT1M'), 0)],
- ['2016-01-02T15:52+00:00', 'Europe/Vienna', new DP(new DT('2016-01-02T15:52:00Z'), new DI('PT1M'), 0)],
- ['2016-01-02T15:52+02:00', 'US/Pacific', new DP(new DT('2016-01-02T15:52:00+02:00'), new DI('PT1M'), 0)],
- ['2016-01-02T15:52-08:00', 'UTC', new DP(new DT('2016-01-02T15:52:00-08:00'), new DI('PT1M'), 0)],
- // second without timezone
- ['2016-08-02 15:52:13', 'UTC', new DP(new DT('2016-08-02T15:52:13Z'), new DI('PT1S'), 0)],
- ['2016-08-02T15:52:13', 'UTC', new DP(new DT('2016-08-02T15:52:13Z'), new DI('PT1S'), 0)],
- ['2016-08-02T15:52:13', 'Europe/Vienna', new DP(new DT('2016-08-02T15:52:13+02:00'), new DI('PT1S'), 0)],
- ['2016-01-02T15:52:00', 'US/Pacific', new DP(new DT('2016-01-02T15:52:00-08:00'), new DI('PT1S'), 0)],
- // second with timezone
- ['2016-08-02 15:52:13Z', 'US/Pacific', new DP(new DT('2016-08-02T15:52:13Z'), new DI('PT1S'), 0)],
- ['2016-08-02T15:52:13Z', 'Europe/Vienna', new DP(new DT('2016-08-02T15:52:13Z'), new DI('PT1S'), 0)],
- ['2016-01-02T15:52:13+00:00', 'Europe/Vienna', new DP(new DT('2016-01-02T15:52:13Z'), new DI('PT1S'), 0)],
- ['2016-01-02T15:52:13+02:00', 'US/Pacific', new DP(new DT('2016-01-02T15:52:13+02:00'), new DI('PT1S'), 0)],
- ['2016-01-02T15:52:13-08:00', 'UTC', new DP(new DT('2016-01-02T15:52:13-08:00'), new DI('PT1S'), 0)],
- // durations PnYnMnDTnHnMnS
- ['P1Y', 'UTC', new DI('P1Y')],
- ['P3M', 'UTC', new DI('P3M')],
- ['P5D', 'UTC', new DI('P5D')],
- ['PT9H', 'UTC', new DI('PT9H')],
- ['PT7M', 'US/Pacific', new DI('PT7M')],
- ['PT6S', 'UTC', new DI('PT6S')],
- ['P1Y3M5D', 'UTC', new DI('P1Y3M5D')],
- ['PT9H7M6S', 'Europe/Vienna', new DI('PT9H7M6S')],
- ['P1Y3M5DT9H7M6S', 'UTC', new DI('P1Y3M5DT9H7M6S')],
- // durations PnW
- ['P4W', 'UTC', new DI('P4W')],
- ];
- }
- /**
- * @dataProvider parseProvider
- */
- public function testParse($text, $timezone, $expected)
- {
- date_default_timezone_set($timezone);
- $this->assertEquals($expected, DateTimeHelper::parse($text));
- }
- public function parseInvalidArgumentProvider()
- {
- return [
- [' '],
- [''],
- ['2016--12'],
- ['2016-01-08:00'],
- ['2016-10-12 08:20#01'],
- ['P'],
- ['PT1'],
- [1],
- [false],
- ];
- }
- /**
- * @dataProvider parseInvalidArgumentProvider
- * @expectedException \InvalidArgumentException
- */
- public function testParseInvalidArgument($text)
- {
- DateTimeHelper::parse($text);
- }
- public function parseGetStartProvider()
- {
- return [
- [null, 'UTC', null],
- [null, 'US/Pacific', null],
- ['1900', 'UTC', new DT('1900-01-01T00:00:00Z') ],
- ['0014', 'Europe/Vienna', new DT('0014-01-01T00:00:00+01:00')],
- ['2016', 'US/Pacific', new DT('2016-01-01T00:00:00-08:00')],
- ['1900Z', 'US/Pacific', new DT('1900-01-01T00:00:00Z') ],
- ['2016Z', 'Europe/Vienna', new DT('2016-01-01T00:00:00Z') ],
- ['2016+00:00', 'Europe/Vienna', new DT('2016-01-01T00:00:00Z') ],
- ['2016+02:00', 'US/Pacific', new DT('2016-01-01T00:00:00+02:00')],
- ['0000 +02:05', 'US/Pacific', new DT('0000-01-01T00:00:00+02:05')],
- ['2016-08:00', 'UTC', new DT('2016-01-01T00:00:00-08:00')],
- ['2016 -08:00', 'UTC', new DT('2016-01-01T00:00:00-08:00')],
- ['2016-08', 'UTC', new DT('2016-08-01T00:00:00Z') ],
- ['2016-08', 'Europe/Vienna', new DT('2016-08-01T00:00:00+02:00')],
- ['2016-01', 'US/Pacific', new DT('2016-01-01T00:00:00-08:00')],
- ['2016-08Z', 'US/Pacific', new DT('2016-08-01T00:00:00Z') ],
- ['2016-08Z', 'Europe/Vienna', new DT('2016-08-01T00:00:00Z') ],
- ['2016-01+00:00', 'Europe/Vienna', new DT('2016-01-01T00:00:00Z') ],
- ['2016-01+02:00', 'US/Pacific', new DT('2016-01-01T00:00:00+02:00')],
- ['2016-01 +02:00', 'US/Pacific', new DT('2016-01-01T00:00:00+02:00')],
- ['2016-01 -08:00', 'UTC', new DT('2016-01-01T00:00:00-08:00')],
- ['2016-08-02', 'UTC', new DT('2016-08-02T00:00:00Z') ],
- ['2016-08-02', 'Europe/Vienna', new DT('2016-08-02T00:00:00+02:00')],
- ['2016-01-02', 'US/Pacific', new DT('2016-01-02T00:00:00-08:00')],
- ['2016-08-02Z', 'US/Pacific', new DT('2016-08-02T00:00:00Z') ],
- ['2016-08-02Z', 'Europe/Vienna', new DT('2016-08-02T00:00:00Z') ],
- ['2016-01-02+00:00', 'Europe/Vienna', new DT('2016-01-02T00:00:00Z') ],
- ['2016-01-02+02:00', 'US/Pacific', new DT('2016-01-02T00:00:00+02:00')],
- ['2016-01-02-08:13', 'UTC', new DT('2016-01-02T00:00:00-08:13')],
- ['2016-08-02 15:52', 'UTC', new DT('2016-08-02T15:52:00Z') ],
- ['2016-08-02T15:52', 'UTC', new DT('2016-08-02T15:52:00Z') ],
- ['2016-08-02T15:52', 'Europe/Vienna', new DT('2016-08-02T15:52:00+02:00')],
- ['2016-01-02T15:52', 'US/Pacific', new DT('2016-01-02T15:52:00-08:00')],
- ['2016-08-02 15:52Z', 'US/Pacific', new DT('2016-08-02T15:52:00Z') ],
- ['2016-08-02T15:52Z', 'Europe/Vienna', new DT('2016-08-02T15:52:00Z') ],
- ['2016-01-02T15:52+00:00', 'Europe/Vienna', new DT('2016-01-02T15:52:00Z') ],
- ['2016-01-02T15:52+02:00', 'US/Pacific', new DT('2016-01-02T15:52:00+02:00')],
- ['2016-01-02T15:52-08:00', 'UTC', new DT('2016-01-02T15:52:00-08:00')],
- ['2016-08-02 15:52:13', 'UTC', new DT('2016-08-02T15:52:13Z') ],
- ['2016-08-02T15:52:13', 'UTC', new DT('2016-08-02T15:52:13Z') ],
- ['2016-08-02T15:52:13', 'Europe/Vienna', new DT('2016-08-02T15:52:13+02:00')],
- ['2016-01-02T15:52:00', 'US/Pacific', new DT('2016-01-02T15:52:00-08:00')],
- ['2016-08-02 15:52:13Z', 'US/Pacific', new DT('2016-08-02T15:52:13Z') ],
- ['2016-08-02T15:52:13Z', 'Europe/Vienna', new DT('2016-08-02T15:52:13Z') ],
- ['2016-01-02T15:52:13+00:00', 'Europe/Vienna', new DT('2016-01-02T15:52:13Z') ],
- ['2016-01-02T15:52:13+02:00', 'US/Pacific', new DT('2016-01-02T15:52:13+02:00')],
- ['2016-01-02T15:52:13-08:00', 'UTC', new DT('2016-01-02T15:52:13-08:00')],
- ];
- }
- /**
- * @dataProvider parseGetStartProvider
- */
- public function testParseGetStart($text, $timezone, $expected)
- {
- date_default_timezone_set($timezone);
- $this->assertEquals($expected, DateTimeHelper::parseGetStart($text));
- }
- public function parseGetStartInvalidArgumentProvider()
- {
- return [
- [' '],
- [''],
- ['2016--12'],
- ['2016-01-08:00'],
- ['2016-10-12 08:20#01'],
- ['P1Y'],
- [1],
- [false],
- ];
- }
- /**
- * @dataProvider parseGetStartInvalidArgumentProvider
- * @expectedException \InvalidArgumentException
- */
- public function testParseGetStartInvalidArgument($text)
- {
- DateTimeHelper::parseGetStart($text);
- }
- public function deinvertIntervalProvider()
- {
- return [
- [
- DI::createFromDateString('-2 years'),
- ['y' => -2, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
- ],
- [
- DI::createFromDateString('-2 months'),
- ['y' => 0, 'm' => -2, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
- ],
- [
- DI::createFromDateString('-2 days'),
- ['y' => 0, 'm' => 0, 'd' => -2, 'h' => 0, 'i' => 0, 's' => 0],
- ],
- [
- DI::createFromDateString('-2 hours'),
- ['y' => 0, 'm' => 0, 'd' => 0, 'h' => -2, 'i' => 0, 's' => 0],
- ],
- [
- DI::createFromDateString('-2 minutes'),
- ['y' => 0, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => -2, 's' => 0],
- ],
- [
- DI::createFromDateString('-2 seconds'),
- ['y' => 0, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => 0, 's' => -2],
- ],
- [
- (new DT('2016-08'))->diff(new DT('2016-07')),
- ['y' => 0, 'm' => -1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
- ],
- [
- (new DT('2016-08-03'))->diff(new DT('2016-07-03')),
- ['y' => 0, 'm' => -1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
- ],
- [
- (new DT('2016-07-03'))->diff(new DT('2016-08-03')),
- ['y' => 0, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
- ],
- [
- (new DT('2016-08-04'))->diff(new DT('2016-07-03')),
- ['y' => 0, 'm' => -1, 'd' => -1, 'h' => 0, 'i' => 0, 's' => 0],
- ],
- [
- (new DT('2016-07-03'))->diff(new DT('2016-08-04')),
- ['y' => 0, 'm' => 1, 'd' => 1, 'h' => 0, 'i' => 0, 's' => 0],
- ],
- [
- (new DT('2016-08-02'))->diff(new DT('2016-07-03')),
- ['y' => 0, 'm' => 0, 'd' => -30, 'h' => 0, 'i' => 0, 's' => 0],
- ],
- [
- (new DT('2016-07-03'))->diff(new DT('2016-08-02')),
- ['y' => 0, 'm' => 0, 'd' => 30, 'h' => 0, 'i' => 0, 's' => 0],
- ],
- [
- (new DT('2016-08-04 18:10:02'))->diff(new DT('2016-07-03 14:13:03')),
- ['y' => 0, 'm' => -1, 'd' => -1, 'h' => -3, 'i' => -56, 's' => -59],
- ],
- [
- (new DT('2016-07-03 14:13:03'))->diff(new DT('2016-08-04 18:10:02')),
- ['y' => 0, 'm' => 1, 'd' => 1, 'h' => 3, 'i' => 56, 's' => 59],
- ],
- ];
- }
- /**
- * @dataProvider deinvertIntervalProvider
- */
- public function testDeinvertInterval($source, $expected_attr)
- {
- // \DateInterval does not implement clone.
- // @see https://bugs.php.net/bug.php?id=50559
- $source_copy = unserialize(serialize($source));
- $deinverted = DateTimeHelper::deinvertInterval($source_copy);
- $this->assertEquals($source, $source_copy);
- $this->assertEquals(0, $deinverted->invert);
- foreach($expected_attr as $k => $v) {
- $this->assertSame($v, $deinverted->$k);
- }
- }
- public function isoProvider()
- {
- return [
- [null, null],
- [new DT('2016-09-16 21:13+02:00'), '2016-09-16T21:13:00+02:00'],
- // \DateInterval::__construct() does not support negative specifiers.
- // e.g. 'P-1Y'
- // 0 years
- [new DI('P0Y0M0DT0H0M0S'), 'P0S' ],
- [new DI('P0Y0M0DT0H0M6S'), 'PT6S' ],
- [new DI('P0Y0M0DT0H5M0S'), 'PT5M' ],
- [new DI('P0Y0M0DT0H5M6S'), 'PT5M6S' ],
- [new DI('P0Y0M0DT4H0M0S'), 'PT4H' ],
- [new DI('P0Y0M3DT0H0M0S'), 'P3D' ],
- [new DI('P0Y0M3DT0H0M6S'), 'P3DT6S' ],
- [new DI('P0Y0M3DT4H5M6S'), 'P3DT4H5M6S' ],
- [new DI('P0Y2M0DT0H0M0S'), 'P2M' ],
- [new DI('P0Y2M0DT0H0M6S'), 'P2MT6S' ],
- [new DI('P0Y2M3DT0H0M6S'), 'P2M3DT6S' ],
- // >0 years, 0 months
- [new DI('P1Y0M0DT0H0M0S'), 'P1Y' ],
- [new DI('P1Y0M0DT0H0M6S'), 'P1YT6S' ],
- [new DI('P1Y0M0DT0H5M0S'), 'P1YT5M' ],
- [new DI('P1Y0M0DT0H5M6S'), 'P1YT5M6S' ],
- [new DI('P1Y0M0DT4H0M0S'), 'P1YT4H' ],
- [new DI('P1Y0M3DT0H0M0S'), 'P1Y3D' ],
- [new DI('P1Y0M3DT0H0M6S'), 'P1Y3DT6S' ],
- [new DI('P1Y0M3DT4H5M6S'), 'P1Y3DT4H5M6S' ],
- // >0 years, >0 months, 0 days (complete)
- [new DI('P1Y2M0DT0H0M0S'), 'P1Y2M' ],
- [new DI('P1Y2M0DT0H0M6S'), 'P1Y2MT6S' ],
- [new DI('P1Y2M0DT0H5M0S'), 'P1Y2MT5M' ],
- [new DI('P1Y2M0DT0H5M6S'), 'P1Y2MT5M6S' ],
- [new DI('P1Y2M0DT4H0M0S'), 'P1Y2MT4H' ],
- [new DI('P1Y2M0DT4H0M6S'), 'P1Y2MT4H6S' ],
- [new DI('P1Y2M0DT4H5M0S'), 'P1Y2MT4H5M' ],
- [new DI('P1Y2M0DT4H5M6S'), 'P1Y2MT4H5M6S' ],
- // >0 years, >0 months, >0 days (complete)
- [new DI('P1Y2M3DT0H0M0S'), 'P1Y2M3D' ],
- [new DI('P1Y2M3DT0H0M6S'), 'P1Y2M3DT6S' ],
- [new DI('P1Y2M3DT0H5M0S'), 'P1Y2M3DT5M' ],
- [new DI('P1Y2M3DT0H5M6S'), 'P1Y2M3DT5M6S' ],
- [new DI('P1Y2M3DT4H0M0S'), 'P1Y2M3DT4H' ],
- [new DI('P1Y2M3DT4H0M6S'), 'P1Y2M3DT4H6S' ],
- [new DI('P1Y2M3DT4H5M0S'), 'P1Y2M3DT4H5M' ],
- [new DI('P1Y2M3DT4H5M6S'), 'P1Y2M3DT4H5M6S'],
- [
- new DP(
- new DT('2016-08-05T14:50:14+08:00'),
- new DI('P1D'),
- new DT('2016-08-10T14:50:14+08:00')
- ),
- 'R4/2016-08-05T14:50:14+08:00/P1D',
- ],
- [
- new DP(
- new DT('2016-08-05T14:50:14+08:00'),
- new DI('P5D'),
- new DT('2016-08-10T14:50:14+08:00')
- ),
- '2016-08-05T14:50:14+08:00/P5D',
- ],
- [
- new DP(
- new DT('2016-08-05T14:50:14+08:00'),
- new DI('P1Y2M3DT4H5M6S'),
- new DT('2017-10-08T18:55:20+08:00')
- ),
- '2016-08-05T14:50:14+08:00/P1Y2M3DT4H5M6S',
- ],
- [
- new DP(
- new DT('2016-08-05T14:50:14Z'),
- new DI('P1D'),
- 0
- ),
- '2016-08-05T14:50:14+00:00/P1D',
- ],
- [
- new DP(
- new DT('2016-08-05T14:50:14Z'),
- new DI('PT5M'),
- 3
- ),
- 'R3/2016-08-05T14:50:14+00:00/PT5M',
- ],
- [
- new DP('R3/2016-08-05T14:50:14Z/PT5M'),
- 'R3/2016-08-05T14:50:14+00:00/PT5M',
- ],
- [
- new DP('R4/2016-08-05T14:50:14Z/P1Y2M3DT4H5M6S'),
- 'R4/2016-08-05T14:50:14+00:00/P1Y2M3DT4H5M6S',
- ],
- [
- DateTimeHelper::parse('2016-08-05T14:50:14Z'),
- '2016-08-05T14:50:14+00:00/PT1S',
- ],
- [
- DateTimeHelper::parse('2016-08-05Z'),
- '2016-08-05T00:00:00+00:00/P1D',
- ],
- [
- DateTimeHelper::parse('2016-08-05-03:00'),
- '2016-08-05T00:00:00-03:00/P1D',
- ],
- ];
- }
- /**
- * @dataProvider isoProvider
- */
- public function testIso($interval, $iso)
- {
- $this->assertSame($iso, DateTimeHelper::iso($interval));
- }
- public function isoReinitProvider()
- {
- return [
- [new DT('2016-09-16')],
- [new DT('2016-09-16 +02:00')],
- [new DT('2016-09-16 -07:13')],
- [new DT('2016-09-16 21:13')],
- [new DT('2016-09-16 21:13+02:00')],
- [new DT('2016-09-16 14:13-07:00')],
- [new DI('P1Y')],
- [new DI('P1M')],
- [new DI('P1D')],
- [new DI('PT1H')],
- [new DI('PT1M')],
- [new DI('PT1S')],
- [new DI('P3DT1S')],
- [new DI('P1Y2M3DT4H5M6S')],
- ];
- }
- /**
- * @dataProvider isoReinitProvider
- */
- public function testIsoReinit($obj)
- {
- $iso = DateTimeHelper::iso($obj);
- $class = get_class($obj);
- $this->assertEquals($obj, new $class($iso));
- }
- public function isoUnsupportedProvider()
- {
- return [
- [DI::createFromDateString('-2 years')],
- [DI::createFromDateString('-2 months')],
- [DI::createFromDateString('-2 days')],
- [DI::createFromDateString('-2 hours')],
- [DI::createFromDateString('-2 minutes')],
- [DI::createFromDateString('-2 seconds')],
- [(new DT('2016-08-03'))->diff(new DT('2016-07-03'))],
- [(new DT('2016-08-03 10:00:01'))->diff(new DT('2016-08-03 10:00:00'))],
- [new DP(new DT('2016-08-05'), new DI('P1D'), new DT('2016-08-05'))],
- [new DP(new DT('2016-08-05'), new DI('PT1S'), new DT('2016-08-04'))],
- [new \Exception('unsupported class')],
- ];
- }
- /**
- * @dataProvider isoUnsupportedProvider
- * @expectedException \InvalidArgumentException
- */
- public function testIsoUnsupported($interval)
- {
- DateTimeHelper::iso($interval);
- }
- }
|