Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Adds a default value for micro/milliseconds if we detect that those values are required but not present.
  • Loading branch information
Luke Towers authored Oct 14, 2020
1 parent 549895c commit ac0215b
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,29 +541,61 @@ public function freshTimestamp()
*/
protected function asDateTime($value)
{
// If this value is already a Argon instance, we shall just return it as is.
// This prevents us having to re-instantiate a Argon instance when we know
// it already is one, which wouldn't be fulfilled by the DateTime check.
if ($value instanceof Argon) {
return $value;
}

// If the value is already a DateTime instance, we will just skip the rest of
// these checks since they will be a waste of time, and hinder performance
// when checking the field. We will just return the DateTime right away.
if ($value instanceof DateTimeInterface) {
return new Argon(
$value->format('Y-m-d H:i:s.u'),
$value->getTimezone()
);
}

// If this value is an integer, we will assume it is a UNIX timestamp's value
// and format a Carbon object from this timestamp. This allows flexibility
// when defining your date fields as they might be UNIX timestamps here.
if (is_numeric($value)) {
return Argon::createFromTimestamp($value);
}

// If the value is in simply year, month, day format, we will instantiate the
// Carbon instances from that format. Again, this provides for simple date
// fields on the database, while still supporting Carbonized conversion.
if ($this->isStandardDateFormat($value)) {
return Argon::createFromFormat('Y-m-d', $value)->startOfDay();
}

return Argon::createFromFormat(
str_replace('.v', '.u', $this->getDateFormat()),
$value
);
$format = $this->getDateFormat();

// https://bugs.php.net/bug.php?id=75577
if (version_compare(PHP_VERSION, '7.3.0-dev', '<')) {
$format = str_replace('.v', '.u', $format);
}

// If the value is expected to end in milli or micro seconds but doesn't
// then we should attempt to fix it as it's most likely from the datepicker
// which doesn't support sending micro or milliseconds
// @see https://github.com/rainlab/blog-plugin/issues/334
if (str_contains($format, '.') && !str_contains($value, '.')) {
if (ends_with($format, '.u')) {
$value .= '.000000';
}
if (ends_with($format, '.v')) {
$value .= '.000';
}
}

// Finally, we will just assume this date is in the format used by default on
// the database connection and use that format to create the Carbon object
// that is returned back out to the developers after we convert it here.
return Argon::createFromFormat($format, $value);
}

/**
Expand Down

0 comments on commit ac0215b

Please sign in to comment.