Skip to content

Commit

Permalink
fix: Time loses microseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jul 26, 2024
1 parent 6d0e882 commit 06bbcca
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions system/I18n/TimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ public function __construct(?string $time = null, $timezone = null, ?string $loc
if ($time === '' && static::$testNow instanceof self) {
if ($timezone !== null) {
$testNow = static::$testNow->setTimezone($timezone);
$time = $testNow->format('Y-m-d H:i:s');
$time = $testNow->format('Y-m-d H:i:s.u');
} else {
$timezone = static::$testNow->getTimezone();
$time = static::$testNow->format('Y-m-d H:i:s');
$time = static::$testNow->format('Y-m-d H:i:s.u');
}
}

Expand All @@ -97,7 +97,7 @@ public function __construct(?string $time = null, $timezone = null, ?string $loc
if ($time !== '' && static::hasRelativeKeywords($time)) {
$instance = new DateTime('now', $this->timezone);
$instance->modify($time);
$time = $instance->format('Y-m-d H:i:s');
$time = $instance->format('Y-m-d H:i:s.u');
}

parent::__construct($time, $this->timezone);
Expand Down Expand Up @@ -253,7 +253,7 @@ public static function createFromFormat($format, $datetime, $timezone = null)
throw I18nException::forInvalidFormat($format);
}

return new self($date->format('Y-m-d H:i:s'), $timezone);
return new self($date->format('Y-m-d H:i:s.u'), $timezone);
}

/**
Expand Down Expand Up @@ -283,7 +283,7 @@ public static function createFromTimestamp(int $timestamp, $timezone = null, ?st
*/
public static function createFromInstance(DateTimeInterface $dateTime, ?string $locale = null)
{
$date = $dateTime->format('Y-m-d H:i:s');
$date = $dateTime->format('Y-m-d H:i:s.u');
$timezone = $dateTime->getTimezone();

return new self($date, $timezone, $locale);
Expand Down Expand Up @@ -348,7 +348,7 @@ public static function setTestNow($datetime = null, $timezone = null, ?string $l
if (is_string($datetime)) {
$datetime = new self($datetime, $timezone, $locale);
} elseif ($datetime instanceof DateTimeInterface && ! $datetime instanceof self) {
$datetime = new self($datetime->format('Y-m-d H:i:s'), $timezone);
$datetime = new self($datetime->format('Y-m-d H:i:s.u'), $timezone);
}

static::$testNow = $datetime;
Expand Down Expand Up @@ -941,9 +941,9 @@ public function equals($testTime, ?string $timezone = null): bool

$ourTime = $this->toDateTime()
->setTimezone(new DateTimeZone('UTC'))
->format('Y-m-d H:i:s');
->format('Y-m-d H:i:s.u');

return $testTime->format('Y-m-d H:i:s') === $ourTime;
return $testTime->format('Y-m-d H:i:s.u') === $ourTime;
}

/**
Expand All @@ -956,15 +956,15 @@ public function equals($testTime, ?string $timezone = null): bool
public function sameAs($testTime, ?string $timezone = null): bool
{
if ($testTime instanceof DateTimeInterface) {
$testTime = $testTime->format('Y-m-d H:i:s');
$testTime = $testTime->format('Y-m-d H:i:s.u O');
} elseif (is_string($testTime)) {
$timezone = $timezone ?: $this->timezone;
$timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);
$testTime = new DateTime($testTime, $timezone);
$testTime = $testTime->format('Y-m-d H:i:s');
$testTime = $testTime->format('Y-m-d H:i:s.u O');
}

$ourTime = $this->toDateTimeString();
$ourTime = $this->format('Y-m-d H:i:s.u O');

return $testTime === $ourTime;
}
Expand Down

0 comments on commit 06bbcca

Please sign in to comment.