-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConvert.php
More file actions
53 lines (39 loc) · 1.35 KB
/
Copy pathConvert.php
File metadata and controls
53 lines (39 loc) · 1.35 KB
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
<?php declare(strict_types=1);
namespace h4kuna\DataType\Date;
use DateTime;
use DateTimeImmutable;
use Nette\StaticClass;
final class Convert
{
use StaticClass;
public static function timestampToImmutable(int $timestamp): DateTimeImmutable
{
return (new DateTimeImmutable())->setTimestamp($timestamp);
}
public static function toMutable(DateTime|DateTimeImmutable $dateTime): DateTime
{
return $dateTime instanceof DateTime ? clone $dateTime : DateTime::createFromImmutable($dateTime);
}
public static function bySource(
DateTime|DateTimeImmutable $source,
DateTime|DateTimeImmutable $dateTime
): DateTime|DateTimeImmutable
{
/** @var class-string<DateTime|DateTimeImmutable> $sourceClass */
$sourceClass = $source::class;
if ($sourceClass === $dateTime::class) {
return $dateTime;
}
$result = $sourceClass::createFromFormat($dateTime::RFC3339_EXTENDED, $dateTime->format($dateTime::RFC3339_EXTENDED));
assert($result !== false);
return $result;
}
public static function toImmutableMidnight(DateTime|DateTimeImmutable $dateTime): DateTimeImmutable
{
return self::toImmutable($dateTime)->setTime(0, 0, 0, 0);
}
public static function toImmutable(DateTime|DateTimeImmutable $dateTime): DateTimeImmutable
{
return $dateTime instanceof DateTimeImmutable ? $dateTime : DateTimeImmutable::createFromMutable($dateTime);
}
}