-
Notifications
You must be signed in to change notification settings - Fork 0
/
Market.php
110 lines (91 loc) · 3.08 KB
/
Market.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
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
<?php
declare(strict_types=1);
namespace Manyou\BingHomepage;
use DateInterval;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use InvalidArgumentException;
use Stringable;
use function abs;
class Market implements Stringable
{
public const ROW = 'ROW';
public const US = 'en-US';
public const CA = 'en-CA';
public const QC = 'fr-CA';
public const UK = 'en-GB';
public const CN = 'zh-CN';
public const JP = 'ja-JP';
public const FR = 'fr-FR';
public const DE = 'de-DE';
public const IN = 'en-IN';
public const BR = 'pt-BR';
public const AU = 'en-AU';
public const IT = 'it-IT';
public const ES = 'es-ES';
public const MAPPINGS = [
self::ROW => 'America/Los_Angeles', // UTC -8 / UTC -7
self::US => 'America/Los_Angeles',
self::AU => 'America/Los_Angeles', // ROW; Australia/Sydney: UTC +10 / UTC +11
self::BR => 'America/Sao_Paulo', // UTC -3
self::CA => 'America/Toronto', // UTC -5 / UTC -4
self::QC => 'America/Toronto',
self::UK => 'Europe/London', // UTC +0 / UTC +1
self::FR => 'Europe/Paris', // UTC +1 / UTC +2
self::IT => 'Europe/Rome',
self::ES => 'Europe/Madrid',
self::DE => 'Europe/Berlin',
self::IN => 'Asia/Kolkata', // UTC +5:30
self::CN => 'Asia/Shanghai', // UTC +8
self::JP => 'Asia/Tokyo', // UTC +9
];
/** @var string */
private $name;
/** @var DateTimeZone */
private $timezone;
public function __construct(string $name, ?DateTimeZone $tz = null)
{
if ($tz === null) {
if (! isset(self::MAPPINGS[$name])) {
throw new InvalidArgumentException("Market {$name} is unknown and no time zone provided");
}
$tz = new DateTimeZone(self::MAPPINGS[$name]);
}
$this->name = $name;
$this->timezone = $tz;
}
public function getToday(?DateTimeImmutable $today = null): DateTimeImmutable
{
if ($today === null) {
return new DateTimeImmutable('today', $this->timezone);
}
return $today->setTimezone($this->timezone)->setTime(0, 0, 0);
}
public function getDate(DateTimeInterface $date): DateTimeImmutable
{
return new DateTimeImmutable($date->format('Y-m-d'), $this->timezone);
}
/** Get the date "$offset" days before today */
public function getDateBefore(int $offset, ?DateTimeImmutable $today = null): DateTimeImmutable
{
$today = $this->getToday($today);
$invert = $offset < 0 ? 1 : 0;
$offset = (string) abs($offset);
$interval = new DateInterval("P{$offset}D");
$interval->invert = $invert;
return $today->sub($interval);
}
public function getName(): string
{
return $this->name;
}
public function __toString(): string
{
return $this->name;
}
public function getTimeZone(): DateTimeZone
{
return $this->timezone;
}
}