-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimezone.php
171 lines (131 loc) · 4.39 KB
/
Timezone.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Base;
// timezone
// class with static methods to deal with timezone
final class Timezone extends Root
{
// config
protected static array $config = [
'current'=>null, // conserve le timezone courant pour le set/reset
'default'=>'UTC' // timezone par défaut, lorsque value est true
];
// is
// retourne vrai si la timezone existe
final public static function is(mixed $value):bool
{
return is_string($value) && in_array($value,self::all(),true);
}
// get
// retourne le timezone courant
final public static function get():string
{
return date_default_timezone_get();
}
// set
// change le timezone courant
// cette valeur prend le dessus sur le ini default_timezone
// possible de set dans ini aussi
final public static function set(bool|string $value,bool $ini=false):bool
{
$return = false;
if($value === true)
$value = self::$config['default'];
if(is_string($value))
{
self::$config['current'] = self::get();
$return = date_default_timezone_set($value);
if($return === true && $ini === true)
Ini::setTimezone($value);
}
return $return;
}
// reset
// remet le timezone de ini comme timezone courant
final public static function reset(bool $ini=false):bool
{
$return = false;
$timezone = null;
if($ini === false)
$timezone = self::$config['current'];
if($timezone === null)
$timezone = Ini::getTimezone();
$return = date_default_timezone_set($timezone);
self::$config['current'] = $timezone;
return $return;
}
// name
// retourne le nom d'un timezone à partir d'une abbréviation
final public static function name(string $abbr,int $offset=-1,int $isDst=-1):?string
{
return timezone_name_from_abbr($abbr,$offset,$isDst);
}
// location
// retourne l'emplacement d'un timezone à partir d'un nom de timezone
final public static function location(string $value):array
{
return timezone_location_get(timezone_open($value));
}
// transitions
// retourne les transitions d'un timezone à partir d'un nom de timezone
final public static function transitions(string $value,?int $begin=null,?int $end=null):array
{
$return = [];
$timezone = timezone_open($value);
if(!empty($begin))
{
if(!empty($end))
$return = timezone_transitions_get($timezone,$begin,$end);
else
$return = timezone_transitions_get($timezone,$begin);
}
else
$return = timezone_transitions_get($timezone);
return $return;
}
// suninfo
// retourne les informations relatives au soleil pour une timezone
// possible de fournir un tableau avec une clé latitude et une clé longitude
final public static function suninfo(string|array $value,mixed $timestamp=null,mixed $format=null):?array
{
$return = null;
$timestamp = Datetime::time($timestamp);
$timezone = null;
if(is_string($value))
{
$timezone = $value;
$value = self::location($value);
}
if(is_array($value) && is_int($timestamp) && array_key_exists('latitude',$value) && array_key_exists('longitude',$value))
{
$return = date_sun_info($timestamp,$value['latitude'],$value['longitude']);
if(!empty($format) && !empty($return))
$return = Datetime::formats($format,$return);
}
return $return;
}
// version
// retourne la version de la db timezone
final public static function version():string
{
return timezone_version_get();
}
// abbreviations
// retourne un tableau multidimensionnel avec tous les timezone dans leur abbréviations
final public static function abbreviations():array
{
return timezone_abbreviations_list();
}
// all
// retourne un tableau avec tous les timezone
final public static function all():array
{
return timezone_identifiers_list();
}
}
?>