-
Notifications
You must be signed in to change notification settings - Fork 0
/
CurrentTime.php
73 lines (59 loc) · 2.05 KB
/
CurrentTime.php
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
<?php
declare(strict_types=1);
namespace Manyou\BingHomepage;
use DateTimeImmutable;
use DateTimeZone;
use function array_filter;
class CurrentTime
{
/** @var DateTimeImmutable */
private $now;
/** @var DateTimeImmutable */
private $theLaterDate;
/**
* @var int Time zone offset of the current natural date line,
* the meridian where the time is zero o'clock now
*/
private $midnightOffset;
public function __construct(?DateTimeImmutable $now = null)
{
$utc = new DateTimeZone('UTC');
$this->now = $now === null ? new DateTimeImmutable('now', $utc) : $now->setTimezone($utc);
$this->setTheLaterDate();
$this->setMidnightOffset();
}
private function setTheLaterDate()
{
if ((int) $this->now->format('G') < 12) {
// For time zones later than UTC (have an earlier date),
// the moment of date change is 00:00 on today's date of UTC
$this->theLaterDate = $this->now->setTime(0, 0, 0);
} else {
// For time zones earlier than UTC (have a later date),
// the moment of date change is 00:00 on tomorrow's date of UTC
$this->theLaterDate = $this->now->modify('+1 day midnight');
}
}
public function getTheLaterDate(): DateTimeImmutable
{
return $this->theLaterDate;
}
private function setMidnightOffset()
{
// General solution:
// $now->diff($the_moment_of_date_change)
// which is equivalent to
// $the_moment_of_date_change - $now
$this->midnightOffset = $this->theLaterDate->getTimestamp() - $this->now->getTimestamp();
}
public function hasBecomeTheLaterDate(DateTimeZone $tz): bool
{
return (int) $this->now->setTimezone($tz)->format('Z') >= $this->midnightOffset;
}
public function getMarketsHaveBecomeTheLaterDate(array $markets): array
{
return array_filter($markets, function (Market $market) {
return $this->hasBecomeTheLaterDate($market->getTimeZone());
});
}
}