1
|
|
|
<?php |
2
|
|
|
namespace SubjectivePHP\DateTime\Utilities; |
3
|
|
|
|
4
|
|
|
use DateTimeZone; |
5
|
|
|
use SubjectivePHP\DateTime\Constants\TimezonesInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Static utility class for working with DateTimeZone objects. |
9
|
|
|
*/ |
10
|
|
|
abstract class DateTimeZoneUtil implements TimezonesInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
const OUTLIERS = [ |
16
|
|
|
'WIB' => self::TIMEZONE_ASIA_JAKARTA, //Western Indonesian Time |
17
|
|
|
'FET' => self::TIMEZONE_EUROPE_HELSINKI, //Further-eastern European Time |
18
|
|
|
'AEST' => self::TIMEZONE_AUSTRALIA_TASMANIA, //Australia Eastern Standard Time |
19
|
|
|
'AWST' => self::TIMEZONE_AUSTRALIA_WEST, //Australia Western Standard Time |
20
|
|
|
'WITA' => self::TIMEZONE_ASIA_MAKASSAR, |
21
|
|
|
'AEDT' => self::TIMEZONE_AUSTRALIA_SYDNEY, //Australia Eastern Daylight Time |
22
|
|
|
'ACDT' => self::TIMEZONE_AUSTRALIA_ADELAIDE, //Australia Central Daylight Time |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Returns a DateTimeZone instance for the give nameOrAbbreviation. |
27
|
|
|
* |
28
|
|
|
* @param string $nameOrAbbreviation The timezone nameOrAbbreviation. |
29
|
|
|
* @param DateTimeZone $default The default timezone to return if none can be created. |
30
|
|
|
* |
31
|
|
|
* @return DateTimeZone|null |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
final public static function fromString(string $nameOrAbbreviation, DateTimeZone $default = null) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
try { |
36
|
|
|
return new DateTimeZone($nameOrAbbreviation); |
37
|
|
|
} catch (\Exception $e) { |
38
|
|
|
if (array_key_exists($nameOrAbbreviation, self::OUTLIERS)) { |
39
|
|
|
return new DateTimeZone(self::OUTLIERS[$nameOrAbbreviation]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $default; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns a DateTimeZone object based on gmt offset. |
48
|
|
|
* |
49
|
|
|
* @param int $gmtOffset Offset from GMT in seconds. |
50
|
|
|
* @param bool $isDaylightSavings Daylight saving time indicator. |
51
|
|
|
* |
52
|
|
|
* @return DateTimeZone |
53
|
|
|
*/ |
54
|
|
|
final public static function fromOffset(int $gmtOffset, bool $isDaylightSavings) : DateTimeZone |
55
|
|
|
{ |
56
|
|
|
return self::fromString(timezone_name_from_abbr('', $gmtOffset, (int)$isDaylightSavings)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the long name of the given DateTimeZone. |
61
|
|
|
* |
62
|
|
|
* @param DateTimeZone $timezone The timezone object from which the long name should be obtained. |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
final public static function getLongName(DateTimeZone $timezone) : string |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$nameFromTimeZone = $timezone->getName(); |
69
|
|
|
if (strlen($nameFromTimeZone) > 6) { |
70
|
|
|
return $nameFromTimeZone; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$nameFromAbbr = timezone_name_from_abbr($nameFromTimeZone); |
74
|
|
|
|
75
|
|
|
return $nameFromAbbr === false ? $nameFromTimeZone : $nameFromAbbr; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.