mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
commit
d020ec3958
@ -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."
|
||||
);
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user