1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Utility; |
4: | |
5: | use DateInterval; |
6: | use DateTimeImmutable; |
7: | use DateTimeInterface; |
8: | use DateTimeZone; |
9: | use InvalidArgumentException; |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | final class Date extends AbstractUtility |
17: | { |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | public static function immutable(DateTimeInterface $datetime): DateTimeImmutable |
24: | { |
25: | return $datetime instanceof DateTimeImmutable |
26: | ? $datetime |
27: | : DateTimeImmutable::createFromMutable($datetime); |
28: | } |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | public static function timezone($timezone = null): DateTimeZone |
37: | { |
38: | if ($timezone instanceof DateTimeZone) { |
39: | return $timezone; |
40: | } |
41: | if ($timezone === null) { |
42: | $timezone = date_default_timezone_get(); |
43: | } |
44: | return new DateTimeZone($timezone); |
45: | } |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | public static function duration($interval): int |
53: | { |
54: | if (!$interval instanceof DateInterval) { |
55: | if ( |
56: | \PHP_VERSION_ID < 80000 |
57: | && Regex::match('/W.+D/', $interval) |
58: | ) { |
59: | throw new InvalidArgumentException(sprintf( |
60: | 'Invalid $interval: %s', |
61: | $interval, |
62: | )); |
63: | } |
64: | $interval = new DateInterval($interval); |
65: | } |
66: | |
67: | $then = new DateTimeImmutable(); |
68: | $now = $then->add($interval); |
69: | |
70: | return $now->getTimestamp() - $then->getTimestamp(); |
71: | } |
72: | |
73: | |
74: | |
75: | |
76: | |
77: | |
78: | |
79: | public static function maybeSetTimezone(DateTimeInterface $datetime, $timezone = null): DateTimeImmutable |
80: | { |
81: | $datetime = self::immutable($datetime); |
82: | $tz = $datetime->getTimezone()->getName(); |
83: | if ($tz === 'UTC' && ($timezone !== null || date_default_timezone_get() !== 'UTC')) { |
84: | $timezone = self::timezone($timezone); |
85: | if ($tz !== $timezone->getName()) { |
86: | return $datetime->setTimezone($timezone); |
87: | } |
88: | } |
89: | return $datetime; |
90: | } |
91: | } |
92: | |