1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the DateTime library. |
4
|
|
|
* |
5
|
|
|
* (c) Artem Henvald <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Fresh\DateTime; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* DateTimeHelper. |
17
|
|
|
* |
18
|
|
|
* @author Artem Henvald <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class DateTimeHelper implements DateTimeHelperInterface |
21
|
|
|
{ |
22
|
|
|
private const INTERNAL_DATE_FORMAT = 'Y-m-d'; |
23
|
|
|
|
24
|
|
|
/** @var mixed[] */ |
25
|
|
|
private $datesCache = []; |
26
|
|
|
|
27
|
|
|
/** @var \DateTimeZone|null */ |
28
|
|
|
private $timeZoneUtc; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function createDateTimeZone(string $timeZoneName = 'UTC'): \DateTimeZone |
34
|
|
|
{ |
35
|
|
|
return new \DateTimeZone($timeZoneName); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function createDateTimeZoneUtc(): \DateTimeZone |
42
|
|
|
{ |
43
|
|
|
if (!$this->timeZoneUtc instanceof \DateTimeZone) { |
44
|
|
|
$this->timeZoneUtc = new \DateTimeZone('UTC'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->timeZoneUtc; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function getCurrentTimestamp(): int |
54
|
|
|
{ |
55
|
|
|
return \time(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function getCurrentDatetime(?\DateTimeZone $timeZone = null): \DateTime |
62
|
|
|
{ |
63
|
|
|
return new \DateTime('now', $timeZone); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function getCurrentDatetimeUtc(): \DateTime |
70
|
|
|
{ |
71
|
|
|
return new \DateTime('now', $this->createDateTimeZoneUtc()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function getCurrentDatetimeImmutable(?\DateTimeZone $timeZone = null): \DateTimeImmutable |
78
|
|
|
{ |
79
|
|
|
return new \DateTimeImmutable('now', $timeZone); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function getCurrentDatetimeImmutableUtc(): \DateTimeImmutable |
86
|
|
|
{ |
87
|
|
|
return new \DateTimeImmutable('now', $this->createDateTimeZoneUtc()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function getDatesFromDateRangeAsArrayOfObjects(DateRangeInterface $dateRange): array |
94
|
|
|
{ |
95
|
|
|
$cacheKeyForDateRange = $this->getCacheKeyForDateRange($dateRange); |
96
|
|
|
|
97
|
|
|
if (!isset($this->datesCache[$cacheKeyForDateRange])) { |
98
|
|
|
$since = DateTimeCloner::cloneIntoDateTime($dateRange->getSince()); |
99
|
|
|
$since->setTime(0, 0); |
100
|
|
|
|
101
|
|
|
$till = DateTimeCloner::cloneIntoDateTime($dateRange->getTill()); |
102
|
|
|
$till->setTime(23, 59, 59); |
103
|
|
|
|
104
|
|
|
$datesAsObjects = []; |
105
|
|
|
$period = new \DatePeriod($since, new \DateInterval('P1D'), $till); |
106
|
|
|
foreach ($period as $date) { |
107
|
|
|
$datesAsObjects[] = $date; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->datesCache[$cacheKeyForDateRange] = $datesAsObjects; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->datesCache[$cacheKeyForDateRange]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function getDatesFromDateRangeAsArrayOfStrings(DateRangeInterface $dateRange): array |
120
|
|
|
{ |
121
|
|
|
$datesAsObjects = $this->getDatesFromDateRangeAsArrayOfObjects($dateRange); |
122
|
|
|
|
123
|
|
|
$datesAsStrings = []; |
124
|
|
|
foreach ($datesAsObjects as $datesAsObject) { |
125
|
|
|
$datesAsStrings[] = $datesAsObject->format(self::INTERNAL_DATE_FORMAT); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $datesAsStrings; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param DateRangeInterface $dateRange |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
private function getCacheKeyForDateRange(DateRangeInterface $dateRange): string |
137
|
|
|
{ |
138
|
|
|
$since = $dateRange->getSince(); |
139
|
|
|
$till = $dateRange->getTill(); |
140
|
|
|
|
141
|
|
|
return \sprintf( |
142
|
|
|
'%s_%s_%s_%s', |
143
|
|
|
$since->format(self::INTERNAL_DATE_FORMAT), |
144
|
|
|
$since->getTimezone()->getName(), |
145
|
|
|
$till->format(self::INTERNAL_DATE_FORMAT), |
146
|
|
|
$till->getTimezone()->getName() |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|