Skip to content

Commit

Permalink
Merge pull request #8037 from Sesquipedalian/time_improvements
Browse files Browse the repository at this point in the history
Improvements to SMF\Time
  • Loading branch information
Sesquipedalian authored Jan 24, 2024
2 parents cc65418 + 8cf4aa4 commit ca41b56
Showing 1 changed file with 58 additions and 7 deletions.
65 changes: 58 additions & 7 deletions Sources/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public function __set(string $prop, mixed $value): void

case 'iso_gmdate':
$tz = $this->getTimezone();
$this->setTimezone(timezone_open('UTC'));
$this->setTimezone(new \DateTimeZone('UTC'));
$this->modify($value);
$this->setTimezone($tz);
break;
Expand All @@ -283,8 +283,8 @@ public function __set(string $prop, mixed $value): void
case 'tz':
case 'tzid':
case 'timezone':
if (in_array($value, timezone_identifiers_list(\DateTimeZone::ALL_WITH_BC))) {
$this->setTimezone(timezone_open($value));
if ($value instanceof \DateTimeZone || (is_string($value) && in_array($value, \DateTimeZone::listIdentifiers(\DateTimeZone::ALL_WITH_BC)))) {
$this->setTimezone($value);
}
break;

Expand Down Expand Up @@ -419,8 +419,8 @@ public function __isset(string $prop): bool
}

/**
* Like DateTime::format(), except that it can accept both DateTime format
* specifiers and strftime format specifiers (but not both at once).
* Like DateTime::format(), except that it can accept either DateTime format
* specifiers or strftime format specifiers (but not both at once).
*
* This does not use the system's strftime library or locale setting when
* formatting using strftime format specifiers, so results may vary in a few
Expand Down Expand Up @@ -682,6 +682,26 @@ function ($matches) {
return $prefix . $result;
}

/**
* Sets the time zone for the SMF\Time object.
*
* @param \DateTimeZone|string $timezone The desired time zone. Can be a
* \DateTimeZone object or a valid time zone identifier string.
* @return staitc An reference to this object.
*/
public function setTimezone(\DateTimeZone|string $timezone): static
{
if ($timezone instanceof \DateTimeZone) {
date_timezone_set($this, $timezone);
} elseif (in_array($timezone, \DateTimeZone::listIdentifiers(\DateTimeZone::ALL_WITH_BC))) {
date_timezone_set($this, new \DateTimeZone($timezone));
} else {
throw new \ValueError();
}

return $this;
}

/***********************
* Public static methods
***********************/
Expand All @@ -703,6 +723,39 @@ public static function create(string $datetime = 'now', \DateTimeZone|string|nul
return new self($datetime, $timezone);
}

/**
* Convert a \DateTimeInterface object to a Time object.
*
* @param string $object A \DateTimeInterface object.
* @param Time A Time object.
*/
public static function createFromInterface(\DateTimeInterface $object): static
{
return new self($object->format('Y-m-d H:i:s.u e'));
}

/**
* Convert a \DateTime object to a Time object.
*
* @param string $object A \DateTime object.
* @param Time A Time object.
*/
public static function createFromMutable(\DateTime $object): static
{
return self::createFromInterface($object);
}

/**
* Convert a \DateTimeImmutable object to a Time object.
*
* @param string $object A \DateTimeImmutable object.
* @param Time A Time object.
*/
public static function createFromImmutable(\DateTimeImmutable $object): static
{
return self::createFromInterface($object);
}

/**
* Replacement for strftime() that is compatible with PHP 8.1+.
*
Expand Down Expand Up @@ -1068,7 +1121,6 @@ protected static function strftimePartialFormat(string $type, string $format): s
$format = implode('', $format_parts);

// Finally, strip out any unwanted leftovers.
// For info on the charcter classes used here, see https://www.php.net/manual/en/regexp.reference.unicode.php and https://www.regular-expressions.info/unicode.html
$format = preg_replace(
[
// Anything that isn't a specification, punctuation mark, or whitespace.
Expand Down Expand Up @@ -1188,7 +1240,6 @@ protected static function datetimePartialFormat(string $type, string $format): s
);

// Finally, strip out any unwanted leftovers.
// For info on the charcter classes used here, see https://www.php.net/manual/en/regexp.reference.unicode.php and https://www.regular-expressions.info/unicode.html
$format = preg_replace(
[
// Anything that isn't a specification, punctuation mark, or whitespace.
Expand Down

0 comments on commit ca41b56

Please sign in to comment.