|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Soluble Japha |
|
7
|
|
|
* |
|
8
|
|
|
* @link https://github.com/belgattitude/soluble-japha |
|
9
|
|
|
* @copyright Copyright (c) 2013-2020 Vanvelthem Sébastien |
|
10
|
|
|
* @license MIT License https://github.com/belgattitude/soluble-japha/blob/master/LICENSE.md |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Soluble\Japha\Util; |
|
14
|
|
|
|
|
15
|
|
|
use Soluble\Japha\Bridge; |
|
16
|
|
|
use Soluble\Japha\Interfaces; |
|
17
|
|
|
use DateTimeZone; |
|
18
|
|
|
|
|
19
|
|
|
class TimeZone |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Whether to activate TimeZone local cache. |
|
23
|
|
|
* |
|
24
|
|
|
* @var bool |
|
25
|
|
|
*/ |
|
26
|
|
|
protected static $enableTzCache = true; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Bridge\Adapter |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $ba; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Cache for availableTz. |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $availableTz; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Cache for default TimeZone. |
|
42
|
|
|
* |
|
43
|
|
|
* @var Interfaces\JavaObject|null Java(java.util.Timezone) |
|
44
|
|
|
*/ |
|
45
|
|
|
protected static $defaultTz; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var Interfaces\JavaClass Java(java.util.Timezone) |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $timeZoneClass; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param Bridge\Adapter $ba |
|
54
|
|
|
*/ |
|
55
|
16 |
|
public function __construct(Bridge\Adapter $ba) |
|
56
|
|
|
{ |
|
57
|
16 |
|
$this->ba = $ba; |
|
58
|
16 |
|
$this->timeZoneClass = $ba->javaClass('java.util.TimeZone'); |
|
59
|
16 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Return java available timezone ids. |
|
63
|
|
|
* |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function getAvailableIDs(): array |
|
67
|
|
|
{ |
|
68
|
1 |
|
if ($this->availableTz === null) { |
|
69
|
1 |
|
$this->availableTz = []; |
|
70
|
1 |
|
$available = $this->timeZoneClass->getAvailableIDs(); |
|
|
|
|
|
|
71
|
1 |
|
foreach ($available as $id) { |
|
72
|
1 |
|
$this->availableTz[] = (string) $id; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
return $this->availableTz; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Return default jvm TimeZone. |
|
81
|
|
|
* |
|
82
|
|
|
* If TimeZone::enableTzCache() is active (by default), |
|
83
|
|
|
* the default JVM timezone object will be locally cached on the |
|
84
|
|
|
* PHP side for performance reasons. |
|
85
|
|
|
* |
|
86
|
|
|
* The behaviour could potentially lead to unexpected results if |
|
87
|
|
|
* your code modify the default JVM timezone during execution. |
|
88
|
|
|
* (i.e. by calling java.util.TimeZone::setDefault() in your code). |
|
89
|
|
|
* |
|
90
|
|
|
* If you don't want to rely on automatic timezone caching, you can |
|
91
|
|
|
* disable it at bootstrap (Soluble\Japha\Util\TimeZone::disableTzCache) |
|
92
|
|
|
* or simply call this method with $enableTzCache=false |
|
93
|
|
|
* |
|
94
|
|
|
* |
|
95
|
|
|
* @param bool $enableTzCache enable local caching of default timezone |
|
96
|
|
|
* |
|
97
|
|
|
* @return Interfaces\JavaObject Java('java.util.TimeZone') |
|
98
|
|
|
*/ |
|
99
|
7 |
|
public function getDefault($enableTzCache = true): Interfaces\JavaObject |
|
100
|
|
|
{ |
|
101
|
7 |
|
$enableCache = $enableTzCache && self::$enableTzCache; |
|
102
|
7 |
|
if (!$enableCache || self::$defaultTz === null) { |
|
103
|
4 |
|
self::$defaultTz = $this->timeZoneClass->getDefault(); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
7 |
|
return self::$defaultTz; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Create a Java(java.util.TimeZone) object from id. |
|
111
|
|
|
* |
|
112
|
|
|
* @throws Exception\UnsupportedTzException |
|
113
|
|
|
* @throws Exception\InvalidArgumentException |
|
114
|
|
|
* @throws \Soluble\Japha\Bridge\Exception\JavaException |
|
115
|
|
|
* |
|
116
|
|
|
* @param string|DateTimeZone $id string identifier or php DateTimeZone |
|
117
|
|
|
* |
|
118
|
|
|
* @return Interfaces\JavaObject Java('java.util.TimeZone') |
|
119
|
|
|
*/ |
|
120
|
11 |
|
public function getTimeZone($id): Interfaces\JavaObject |
|
121
|
|
|
{ |
|
122
|
11 |
|
if ($id instanceof DateTimeZone) { |
|
123
|
1 |
|
$phpTimezone = $id->getName(); |
|
124
|
11 |
|
} elseif (is_string($id) && trim($id) != '') { |
|
125
|
10 |
|
$phpTimezone = $id; |
|
126
|
|
|
} else { |
|
127
|
1 |
|
throw new Exception\InvalidArgumentException('Method getTimeZone($id) require argument to be datetimeZone or a non empty string'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @var Interfaces\JavaClass |
|
132
|
|
|
*/ |
|
133
|
10 |
|
$tz = $this->timeZoneClass->getTimeZone($phpTimezone); |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @var string |
|
137
|
|
|
*/ |
|
138
|
10 |
|
$javaTimezone = (string) $tz->getID(); |
|
139
|
10 |
|
if ($javaTimezone === 'GMT' && $phpTimezone !== 'GMT') { |
|
140
|
4 |
|
$msg = sprintf( |
|
141
|
4 |
|
"The timezone id '%s' could not be understood by JVM (JVM returned defaulted to GMT)", |
|
142
|
4 |
|
$id instanceof DateTimeZone ? $id->getName() : $id |
|
143
|
|
|
); |
|
144
|
4 |
|
throw new Exception\UnsupportedTzException($msg); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
6 |
|
return $tz; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Set default JVM/servlet timezone. |
|
152
|
|
|
* |
|
153
|
|
|
* @throws \Soluble\Japha\Bridge\Exception\JavaException |
|
154
|
|
|
* @throws Exception\UnsupportedTzException |
|
155
|
|
|
* |
|
156
|
|
|
* @param string|Interfaces\JavaObject|DateTimeZone $timeZone timezone id, Java(java.util.Timezone) or php DateTimeZone |
|
157
|
|
|
*/ |
|
158
|
8 |
|
public function setDefault($timeZone): void |
|
159
|
|
|
{ |
|
160
|
8 |
|
if (is_string($timeZone) || $timeZone instanceof DateTimeZone) { |
|
161
|
7 |
|
$timeZone = $this->getTimeZone($timeZone); |
|
162
|
|
|
} |
|
163
|
5 |
|
$this->timeZoneClass->setDefault($timeZone); |
|
|
|
|
|
|
164
|
5 |
|
self::$defaultTz = $timeZone; |
|
165
|
5 |
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Enable local timezone cache. |
|
169
|
|
|
* |
|
170
|
|
|
* TimeZone::enableTzCache() enable local object caching |
|
171
|
|
|
* for defaultTimezone |
|
172
|
|
|
* |
|
173
|
|
|
* This behaviour could potentially lead to unexpected results if |
|
174
|
|
|
* your code modify the default JVM timezone during execution. |
|
175
|
|
|
* (i.e. by calling java.util.TimeZone::setDefault() in your code). |
|
176
|
|
|
* |
|
177
|
|
|
* If you don't want to rely on automatic timezone caching, you can |
|
178
|
|
|
* disable it at bootstrap (Soluble\Japha\Util\TimeZone::disableTzCache) |
|
179
|
|
|
*/ |
|
180
|
8 |
|
public static function enableTzCache(): void |
|
181
|
|
|
{ |
|
182
|
8 |
|
self::$enableTzCache = true; |
|
183
|
8 |
|
self::$defaultTz = null; |
|
184
|
8 |
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Disable local timezone cache. |
|
188
|
|
|
* |
|
189
|
|
|
* TimeZone::disbaleTzCache() disable local object caching |
|
190
|
|
|
* for defaultTimezone |
|
191
|
|
|
*/ |
|
192
|
1 |
|
public static function disableTzCache(): void |
|
193
|
|
|
{ |
|
194
|
1 |
|
self::$enableTzCache = false; |
|
195
|
1 |
|
self::$defaultTz = null; |
|
196
|
1 |
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|