Skip to content

Commit 9468c1f

Browse files
committed
Add Source Code
- Date, DateTime, and UtcDateTime. - DateInterface, and DateTimeInterface.
1 parent 3e3edce commit 9468c1f

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

src/Date.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Darsyn\DateTime;
4+
5+
class Date extends DateTime implements DateInterface
6+
{
7+
public function __construct(?string $datetime = null, ?\DateTimeZone $timezone = null)
8+
{
9+
$datetime = new \DateTime($datetime ?: 'now', $timezone);
10+
parent::__construct($datetime->format('Y-m-d\T00:00:00.000000\Z'), new \DateTimeZone('UTC'));
11+
}
12+
13+
public function __toString(): string
14+
{
15+
return $this->format(DateTimeInterface::UTC_DATE);
16+
}
17+
}

src/DateInterface.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Darsyn\DateTime;
4+
5+
interface DateInterface extends DateTimeInterface
6+
{
7+
}

src/DateTime.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Darsyn\DateTime;
4+
5+
class DateTime extends \DateTimeImmutable implements DateTimeInterface
6+
{
7+
/** {@inheritdoc} */
8+
public function __construct(?string $datetime = null, ?\DateTimeZone $timezone = null)
9+
{
10+
$datetime = new \DateTime($datetime ?: 'now', $timezone);
11+
parent::__construct($datetime->format('Y-m-d\TH:i:s.000000P'), $timezone);
12+
}
13+
14+
/**
15+
* {@inheritdoc}
16+
* @throws \InvalidArgumentException
17+
*/
18+
public static function createFromFormat($format, $time, ?\DateTimeZone $timezone = null): self
19+
{
20+
// DateTimeImmutable's createFromFormat() method returns instances of DateTimeImmutable rather than static
21+
// (child class), so we'll unfortunately have to replicate some of the constructor logic here.
22+
if (!\is_object($datetime = \DateTime::createFromFormat($format, $time, $timezone))) {
23+
throw new \InvalidArgumentException('Value not compatible with date format.');
24+
}
25+
return new static($datetime->format(DateTimeInterface::NO_TIMEZONE), $datetime->getTimezone());
26+
}
27+
28+
/** {@inheritdoc} */
29+
public static function createFromMutable($datetime): self
30+
{
31+
return static::createFromObject($datetime);
32+
}
33+
34+
/** {@inheritdoc} */
35+
public static function createFromObject(\DateTimeInterface $datetime): DateTimeInterface
36+
{
37+
return new static($datetime->format(DateTimeInterface::NO_TIMEZONE), $datetime->getTimezone());
38+
}
39+
40+
/** {@inheritdoc} */
41+
public static function createFromTimestamp(int $timestamp): DateTimeInterface
42+
{
43+
return static::createFromFormat('U', (string) $timestamp);
44+
}
45+
46+
/** {@inheritdoc} */
47+
public function jsonSerialize(): string
48+
{
49+
return $this->__toString();
50+
}
51+
52+
/** {@inheritdoc} */
53+
public function __toString(): string
54+
{
55+
return $this->format(DateTimeInterface::RFC3339);
56+
}
57+
}

src/DateTimeInterface.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Darsyn\DateTime;
4+
5+
interface DateTimeInterface extends \DateTimeInterface, \JsonSerializable
6+
{
7+
public const UTC_DATE = 'Y-m-d';
8+
public const UTC_DATETIME = 'Y-m-d\TH:i:s\Z';
9+
public const NO_TIMEZONE = 'Y-m-d\TH:i:s';
10+
11+
/**
12+
* @param \DateTimeInterface $datetime
13+
* @return \Darsyn\DateTime\DateTimeInterface
14+
*/
15+
public static function createFromObject(\DateTimeInterface $datetime): self;
16+
17+
/**
18+
* @param int $timestamp
19+
* @return \Darsyn\DateTime\DateTimeInterface
20+
*/
21+
public static function createFromTimestamp(int $timestamp): self;
22+
23+
/**
24+
* @return string
25+
*/
26+
public function __toString(): string;
27+
}

src/UtcDateTime.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Darsyn\DateTime;
4+
5+
class UtcDateTime extends DateTime
6+
{
7+
/** {@inheritdoc} */
8+
public function __construct(?string $datetime = null, ?\DateTimeZone $timezone = null)
9+
{
10+
$datetime = new \DateTime($datetime ?: 'now', $timezone);
11+
$datetime->setTimezone($tz = new \DateTimeZone('UTC'));
12+
parent::__construct($datetime->format('Y-m-d\TH:i:s.000000\Z'), $tz);
13+
}
14+
15+
/** {@inheritdoc} */
16+
public function __toString(): string
17+
{
18+
return $this->format(DateTimeInterface::UTC_DATETIME);
19+
}
20+
}

0 commit comments

Comments
 (0)