-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateTime.php
58 lines (50 loc) · 1.88 KB
/
DateTime.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php declare(strict_types=1);
namespace Darsyn\DateTime;
class DateTime extends \DateTimeImmutable implements DateTimeInterface
{
/** {@inheritdoc} */
public function __construct(?string $datetime = null, ?\DateTimeZone $timezone = null)
{
$datetime = new \DateTime($datetime ?: 'now', $timezone);
if ($timezone instanceof \DateTimeZone) {
$datetime->setTimezone($timezone);
}
parent::__construct($datetime->format(DateTimeInterface::NO_TIMEZONE), $timezone ?? $datetime->getTimezone());
}
/**
* {@inheritdoc}
* @throws \InvalidArgumentException
*/
public static function createFromFormat($format, $time, ?\DateTimeZone $timezone = null): self
{
if (!\is_object($datetime = \DateTime::createFromFormat($format, $time, $timezone))) {
throw new \InvalidArgumentException('Value not compatible with date format.');
}
return new static($datetime->format(DateTimeInterface::RFC3339), $timezone ?? $datetime->getTimezone());
}
/** {@inheritdoc} */
public static function createFromMutable($datetime): self
{
return static::createFromObject($datetime);
}
/** {@inheritdoc} */
public static function createFromObject(\DateTimeInterface $datetime): DateTimeInterface
{
return new static($datetime->format(DateTimeInterface::NO_TIMEZONE), $datetime->getTimezone());
}
/** {@inheritdoc} */
public static function createFromTimestamp(int $timestamp, ?\DateTimeZone $timezone = null): DateTimeInterface
{
return static::createFromFormat('U', (string) $timestamp, $timezone);
}
/** {@inheritdoc} */
public function jsonSerialize(): string
{
return $this->__toString();
}
/** {@inheritdoc} */
public function __toString(): string
{
return $this->format(DateTimeInterface::RFC3339);
}
}