2016-10-14 03:30:05 +02:00
|
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
|
2018-06-15 01:04:12 +02:00
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-01-26 05:20:08 +01:00
|
|
|
|
use SilverStripe\i18n\i18n;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests for {@link Datetime} class.
|
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
|
class DBDatetimeTest extends SapphireTest
|
|
|
|
|
{
|
2017-03-24 04:00:54 +01:00
|
|
|
|
protected function setUp()
|
2017-01-26 05:20:08 +01:00
|
|
|
|
{
|
|
|
|
|
parent::setUp();
|
|
|
|
|
i18n::set_locale('en_NZ');
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
|
public function testNowWithSystemDate()
|
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
|
$systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
$nowDatetime = DBDatetime::now();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($systemDatetime->Date(), $nowDatetime->Date());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNowWithMockDate()
|
|
|
|
|
{
|
|
|
|
|
// Test setting
|
|
|
|
|
$mockDate = '2001-12-31 22:10:59';
|
|
|
|
|
DBDatetime::set_mock_now($mockDate);
|
2017-01-26 05:20:08 +01:00
|
|
|
|
$systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
$nowDatetime = DBDatetime::now();
|
|
|
|
|
$this->assertNotEquals($systemDatetime->Date(), $nowDatetime->Date());
|
|
|
|
|
$this->assertEquals($nowDatetime->getValue(), $mockDate);
|
|
|
|
|
|
|
|
|
|
// Test clearing
|
|
|
|
|
DBDatetime::clear_mock_now();
|
2017-01-26 05:20:08 +01:00
|
|
|
|
$systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
$nowDatetime = DBDatetime::now();
|
|
|
|
|
$this->assertEquals($systemDatetime->Date(), $nowDatetime->Date());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSetNullAndZeroValues()
|
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
|
$date = DBDatetime::create_field('Datetime', '');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
$this->assertNull($date->getValue(), 'Empty string evaluates to NULL');
|
|
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
|
$date = DBDatetime::create_field('Datetime', null);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
$this->assertNull($date->getValue(), 'NULL is set as NULL');
|
|
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
|
$date = DBDatetime::create_field('Datetime', false);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
$this->assertNull($date->getValue(), 'Boolean FALSE evaluates to NULL');
|
|
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
|
$date = DBDatetime::create_field('Datetime', '0');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
$this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'String zero is UNIX epoch time');
|
|
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
|
$date = DBDatetime::create_field('Datetime', 0);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
$this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'Numeric zero is UNIX epoch time');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExtendedDateTimes()
|
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
|
$date = DBDatetime::create_field('Datetime', '1600-10-10 15:32:24');
|
|
|
|
|
$this->assertEquals('10 Oct 1600 15 32 24', $date->Format('d MMM y H m s'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
|
$date = DBDatetime::create_field('Datetime', '3000-10-10 15:32:24');
|
|
|
|
|
$this->assertEquals('10 Oct 3000 15 32 24', $date->Format('d MMM y H m s'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-15 01:04:12 +02:00
|
|
|
|
/**
|
|
|
|
|
* Coverage for dates using hindi-numerals
|
|
|
|
|
*/
|
|
|
|
|
public function testHindiNumerals()
|
|
|
|
|
{
|
|
|
|
|
// Parent locale is english; Can be localised to arabic
|
|
|
|
|
$date = DBDatetime::create_field('Datetime', '1600-10-10 15:32:24');
|
|
|
|
|
$this->assertEquals('10 Oct 1600 15 32 24', $date->Format('d MMM y H m s'));
|
|
|
|
|
$this->assertEquals('١٠ أكتوبر ١٦٠٠ ١٥ ٣٢ ٢٤', $date->Format('d MMM y H m s', 'ar'));
|
|
|
|
|
|
|
|
|
|
// Parent locale is arabic; Datavalue uses ISO date
|
|
|
|
|
i18n::set_locale('ar');
|
|
|
|
|
$date = DBDatetime::create_field('Datetime', '1600-10-10 15:32:24');
|
|
|
|
|
$this->assertEquals('١٠ أكتوبر ١٦٠٠ ١٥ ٣٢ ٢٤', $date->Format('d MMM y H m s'));
|
|
|
|
|
$this->assertEquals('1600-10-10 15:32:24', $date->getValue());
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
|
public function testNice()
|
|
|
|
|
{
|
2018-10-04 03:51:24 +02:00
|
|
|
|
$date = DBDatetime::create_field('Datetime', '2001-12-11 22:10:59');
|
|
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
|
// note: Some localisation packages exclude the ',' in default medium format
|
2018-10-04 03:51:24 +02:00
|
|
|
|
i18n::set_locale('en_NZ');
|
|
|
|
|
$this->assertRegExp('#11/12/2001(,)? 10:10 PM#i', $date->Nice());
|
|
|
|
|
|
|
|
|
|
i18n::set_locale('en_US');
|
|
|
|
|
$this->assertRegExp('#Dec 11(,)? 2001(,)? 10:10 PM#i', $date->Nice());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDate()
|
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
|
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
$this->assertEquals('31/12/2001', $date->Date());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTime()
|
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
|
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
|
2017-05-18 12:01:55 +02:00
|
|
|
|
$this->assertRegexp('#10:10:59 PM#i', $date->Time());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTime24()
|
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
|
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
$this->assertEquals('22:10', $date->Time24());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testURLDateTime()
|
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
|
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
|
|
|
|
|
$this->assertEquals('2001-12-31%2022%3A10%3A59', $date->URLDateTime());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAgoInPast()
|
|
|
|
|
{
|
|
|
|
|
DBDatetime::set_mock_now('2000-12-31 12:00:00');
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'10 years ago',
|
2017-01-26 05:20:08 +01:00
|
|
|
|
DBDatetime::create_field('Datetime', '1990-12-31 12:00:00')->Ago(),
|
2016-12-16 05:34:21 +01:00
|
|
|
|
'Exact past match on years'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'10 years ago',
|
2017-01-26 05:20:08 +01:00
|
|
|
|
DBDatetime::create_field('Datetime', '1990-12-30 12:00:00')->Ago(),
|
2016-12-16 05:34:21 +01:00
|
|
|
|
'Approximate past match on years'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'1 year ago',
|
2017-01-26 05:20:08 +01:00
|
|
|
|
DBDatetime::create_field('Datetime', '1999-12-30 12:00:12')->Ago(true, 1),
|
2016-12-16 05:34:21 +01:00
|
|
|
|
'Approximate past match in singular, significance=1'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'12 months ago',
|
2017-01-26 05:20:08 +01:00
|
|
|
|
DBDatetime::create_field('Datetime', '1999-12-30 12:00:12')->Ago(),
|
2016-12-16 05:34:21 +01:00
|
|
|
|
'Approximate past match in singular'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'50 mins ago',
|
2017-01-26 05:20:08 +01:00
|
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:10:11')->Ago(),
|
2016-12-16 05:34:21 +01:00
|
|
|
|
'Approximate past match on minutes'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'59 secs ago',
|
2017-01-26 05:20:08 +01:00
|
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:59:01')->Ago(),
|
2016-12-16 05:34:21 +01:00
|
|
|
|
'Approximate past match on seconds'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'less than a minute ago',
|
2017-01-26 05:20:08 +01:00
|
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:59:01')->Ago(false),
|
2016-12-16 05:34:21 +01:00
|
|
|
|
'Approximate past match on seconds with $includeSeconds=false'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'1 min ago',
|
2017-01-26 05:20:08 +01:00
|
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:58:50')->Ago(false),
|
2016-12-16 05:34:21 +01:00
|
|
|
|
'Test between 1 and 2 minutes with includeSeconds=false'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'70 secs ago',
|
2017-01-26 05:20:08 +01:00
|
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:58:50')->Ago(true),
|
2016-12-16 05:34:21 +01:00
|
|
|
|
'Test between 1 and 2 minutes with includeSeconds=true'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'4 mins ago',
|
2017-01-26 05:20:08 +01:00
|
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 11:55:50')->Ago(),
|
2016-12-16 05:34:21 +01:00
|
|
|
|
'Past match on minutes'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'1 hour ago',
|
2017-01-26 05:20:08 +01:00
|
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 10:50:58')->Ago(true, 1),
|
2016-12-16 05:34:21 +01:00
|
|
|
|
'Past match on hours, significance=1'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'3 hours ago',
|
2017-01-26 05:20:08 +01:00
|
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 08:50:58')->Ago(),
|
2016-12-16 05:34:21 +01:00
|
|
|
|
'Past match on hours'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
DBDatetime::clear_mock_now();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAgoInFuture()
|
|
|
|
|
{
|
|
|
|
|
DBDatetime::set_mock_now('2000-12-31 00:00:00');
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'in 10 years',
|
2017-01-26 05:20:08 +01:00
|
|
|
|
DBDatetime::create_field('Datetime', '2010-12-31 12:00:00')->Ago(),
|
2016-12-16 05:34:21 +01:00
|
|
|
|
'Exact past match on years'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'in 1 hour',
|
2017-01-26 05:20:08 +01:00
|
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 1:01:05')->Ago(true, 1),
|
2016-12-16 05:34:21 +01:00
|
|
|
|
'Approximate past match on minutes, significance=1'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'in 61 mins',
|
2017-01-26 05:20:08 +01:00
|
|
|
|
DBDatetime::create_field('Datetime', '2000-12-31 1:01:05')->Ago(),
|
2016-12-16 05:34:21 +01:00
|
|
|
|
'Approximate past match on minutes'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
DBDatetime::clear_mock_now();
|
|
|
|
|
}
|
2018-01-24 16:47:00 +01:00
|
|
|
|
|
|
|
|
|
public function testRfc3999()
|
|
|
|
|
{
|
|
|
|
|
// Dates should be formatted as: 2018-01-24T14:05:53+00:00
|
|
|
|
|
$date = DBDatetime::create_field('Datetime', '2010-12-31 16:58:59');
|
|
|
|
|
$this->assertEquals('2010-12-31T16:58:59+00:00', $date->Rfc3339());
|
|
|
|
|
}
|
2019-07-05 05:57:23 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $adjustment
|
|
|
|
|
* @param string $expected
|
|
|
|
|
* @dataProvider modifyProvider
|
|
|
|
|
*/
|
|
|
|
|
public function testModify($adjustment, $expected)
|
|
|
|
|
{
|
|
|
|
|
DBDatetime::set_mock_now('2019-03-03 12:00:00');
|
|
|
|
|
$result = DBDatetime::now()->modify($adjustment)->Rfc2822();
|
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array[]
|
|
|
|
|
*/
|
|
|
|
|
public function modifyProvider()
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
['+1 day', '2019-03-04 12:00:00'],
|
|
|
|
|
['-1 day', '2019-03-02 12:00:00'],
|
|
|
|
|
['+24 hours', '2019-03-04 12:00:00'],
|
|
|
|
|
['-24 hours', '2019-03-02 12:00:00'],
|
|
|
|
|
['+2 weeks', '2019-03-17 12:00:00'],
|
|
|
|
|
['-2 weeks', '2019-02-17 12:00:00'],
|
|
|
|
|
['+2 years', '2021-03-03 12:00:00'],
|
|
|
|
|
['-2 years', '2017-03-03 12:00:00'],
|
|
|
|
|
['+35 minutes', '2019-03-03 12:35:00'],
|
|
|
|
|
['-35 minutes', '2019-03-03 11:25:00'],
|
|
|
|
|
['+3 hours', '2019-03-03 15:00:00'],
|
|
|
|
|
['-3 hours', '2019-03-03 09:00:00'],
|
|
|
|
|
['+59 seconds', '2019-03-03 12:00:59'],
|
|
|
|
|
['-59 seconds', '2019-03-03 11:59:01'],
|
|
|
|
|
];
|
|
|
|
|
}
|
2010-10-15 03:28:43 +02:00
|
|
|
|
}
|