Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for rainlab/blog-plugin#334 #528

Merged
merged 1 commit into from
Oct 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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