Merge pull request #7599 from open-sausages/pulls/4.0/time-and-relative-date-in-silverstripe

WIP - BUG Fix don't treat zero-date as invalid
This commit is contained in:
Daniel Hensby 2017-11-16 12:48:51 +00:00 committed by GitHub
commit d020ec3958
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -67,6 +67,9 @@ class DBDate extends DBField
} else {
// Convert US date -> iso, fix y2k, etc
$value = $this->fixInputDate($value);
if (is_null($value)) {
return null;
}
$source = strtotime($value); // convert string to timestamp
}
if ($value === false) {
@ -529,6 +532,9 @@ class DBDate extends DBField
// split
list($year, $month, $day, $time) = $this->explodeDateString($value);
if ((int)$year === 0 && (int)$month === 0 && (int)$day === 0) {
return null;
}
// Validate date
if (!checkdate($month, $day, $year)) {
throw new InvalidArgumentException(
@ -568,7 +574,7 @@ class DBDate extends DBField
if ($parts[0] < 1000 && $parts[2] > 1000) {
$parts = array_reverse($parts);
}
if ($parts[0] < 1000) {
if ($parts[0] < 1000 && (int)$parts[0] !== 0) {
throw new InvalidArgumentException(
"Invalid date: '$value'. Use " . self::ISO_DATE . " to prevent this error."
);

View File

@ -218,6 +218,12 @@ class DBDateTest extends SapphireTest
$date = DBField::create_field('Date', 0);
$this->assertEquals('1970-01-01', $date->getValue(), 'Zero is UNIX epoch date');
$date = DBField::create_field('Date', '0000-00-00 00:00:00');
$this->assertNull($date->getValue(), '0000-00-00 00:00:00 is set as NULL');
$date = DBField::create_field('Date', '00/00/0000');
$this->assertNull($date->getValue(), '00/00/0000 is set as NULL');
}
public function testDayOfMonth()