Repository navigation
Expand file tree
/
Copy pathDatePoint.php
More file actions
177 lines (145 loc) · 6.16 KB
/
Copy pathDatePoint.php
File metadata and controls
177 lines (145 loc) · 6.16 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Clock;
/**
* An immmutable DateTime with stricter error handling and return types than the native one.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
final class DatePoint extends \DateTimeImmutable
{
/**
* @throws \DateMalformedStringException When $datetime is invalid
*/
public function __construct(string $datetime = 'now', ?\DateTimeZone $timezone = null, ?parent $reference = null)
{
if (null === $reference) {
$clock = Clock::get();
// copying the clock's date into $this goes through its local time, which is ambiguous during the DST fall-back hour
if ('' !== $datetime && $clock instanceof NativeClock) {
parent::__construct($datetime, $timezone ??= $clock->getTimezone());
// the current time is read in the timezone passed to the constructor, not in the one found in $datetime
if ($timezone->getName() !== $this->getTimezone()->getName()) {
parent::__construct($datetime, $this->getTimezone());
}
return;
}
$reference = $clock->now();
}
$now = $reference;
if ('now' !== $datetime) {
if (!$now instanceof static) {
$now = static::createFromInterface($now);
}
$builtInDate = new parent($datetime, $timezone ?? $now->getTimezone());
$timezone = $builtInDate->getTimezone();
$parsed = date_parse($datetime);
if (false !== $parsed['year'] && false !== $parsed['month'] && false !== $parsed['day']) {
// the native result does not depend on the current time when $datetime has a full date
$now = $builtInDate;
} else {
$now = $now->setTimezone($timezone);
// the native constructor starts from midnight when $datetime has a date but no time, modify() does not
if (false === $parsed['hour'] && (false !== $parsed['month'] || false !== $parsed['day'])) {
$datetime = 'midnight '.$datetime;
} elseif (false === $parsed['fraction'] && (false !== $parsed['year'] || false !== $parsed['hour'])) {
// it also resets the microseconds when $datetime sets a field without a fraction
$now = \PHP_INT_SIZE >= 8 ? $now->setTimestamp($now->getTimestamp()) : static::createFromFormat('U', $now->format('U'))->setTimezone($timezone);
}
$now = $now->modify($datetime);
}
} elseif (null !== $timezone) {
$now = $now->setTimezone($timezone);
}
$this->__unserialize((array) $now);
// copying $now goes through its local time, which is ambiguous during the DST fall-back hour
if ($this != $now) {
// PHP picks the DST occurrence when the timezone is part of the string
$date = $now->format('Y-m-d H:i:s.u e');
if ($now == new parent($date)) {
parent::__construct($date);
}
}
}
/**
* @throws \DateMalformedStringException When $format or $datetime are invalid
*/
public static function createFromFormat(string $format, string $datetime, ?\DateTimeZone $timezone = null): static
{
return parent::createFromFormat($format, $datetime, $timezone) ?: throw new \DateMalformedStringException(static::getLastErrors()['errors'][0] ?? 'Invalid date string or format.');
}
public static function createFromInterface(\DateTimeInterface $object): static
{
return parent::createFromInterface($object);
}
public static function createFromMutable(\DateTime $object): static
{
return parent::createFromMutable($object);
}
public static function createFromTimestamp(int|float $timestamp): static
{
return parent::createFromTimestamp($timestamp);
}
#[\NoDiscard('as DatePoint is immutable')]
public function add(\DateInterval $interval): static
{
return parent::add($interval);
}
#[\NoDiscard('as DatePoint is immutable')]
public function sub(\DateInterval $interval): static
{
return parent::sub($interval);
}
/**
* @throws \DateMalformedStringException When $modifier is invalid
*/
#[\NoDiscard('as DatePoint is immutable')]
public function modify(string $modifier): static
{
return parent::modify($modifier);
}
#[\NoDiscard('as DatePoint is immutable')]
public function setTimestamp(int $value): static
{
return parent::setTimestamp($value);
}
#[\NoDiscard('as DatePoint is immutable')]
public function setDate(int $year, int $month, int $day): static
{
return parent::setDate($year, $month, $day);
}
#[\NoDiscard('as DatePoint is immutable')]
public function setISODate(int $year, int $week, int $day = 1): static
{
return parent::setISODate($year, $week, $day);
}
#[\NoDiscard('as DatePoint is immutable')]
public function setTime(int $hour, int $minute, int $second = 0, int $microsecond = 0): static
{
return parent::setTime($hour, $minute, $second, $microsecond);
}
#[\NoDiscard('as DatePoint is immutable')]
public function setTimezone(\DateTimeZone $timezone): static
{
return parent::setTimezone($timezone);
}
public function getTimezone(): \DateTimeZone
{
return parent::getTimezone() ?: throw new \DateInvalidTimeZoneException('The DatePoint object has no timezone.');
}
#[\NoDiscard('as DatePoint is immutable')]
public function setMicrosecond(int $microsecond): static
{
if ($microsecond < 0 || $microsecond > 999999) {
throw new \DateRangeError('DatePoint::setMicrosecond(): Argument #1 ($microsecond) must be between 0 and 999999, '.$microsecond.' given');
}
return parent::setMicrosecond($microsecond);
}
}