1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
time.php
См. документацию.
1<?php
2
9
10use Bitrix\Main;
11
13{
14 protected static $enabled = 1;
15 protected static $useTimeZones = false;
16
17 public static function Enabled()
18 {
19 return (self::$enabled > 0 && self::OptionEnabled());
20 }
21
22 public static function OptionEnabled()
23 {
24 if (self::$useTimeZones === false)
25 {
26 self::$useTimeZones = COption::GetOptionString("main", "use_time_zones", "N");
27 }
28 return (self::$useTimeZones == "Y");
29 }
30
31 public static function Disable()
32 {
33 self::$enabled--;
34 }
35
36 public static function Enable()
37 {
38 self::$enabled++;
39 }
40
41 public static function GetZones()
42 {
43 IncludeModuleLangFile(__FILE__);
44
45 $aTZ = [];
46 static $aExcept = ["Etc/", "GMT", "UTC", "UCT", "HST", "PST", "MST", "CST", "EST", "CET", "MET", "WET", "EET", "PRC", "ROC", "ROK", "W-SU"];
47 foreach (DateTimeZone::listIdentifiers() as $tz)
48 {
49 foreach ($aExcept as $ex)
50 {
51 if (str_starts_with($tz, $ex))
52 {
53 continue 2;
54 }
55 }
56 try
57 {
58 $oTz = new DateTimeZone($tz);
59 $aTZ[$tz] = ['timezone_id' => $tz, 'offset' => $oTz->getOffset(new DateTime("now", $oTz))];
60 }
61 catch (Throwable)
62 {
63 }
64 }
65
66 uasort($aTZ, function ($a, $b) {
67 if ($a['offset'] == $b['offset'])
68 {
69 return strcmp($a['timezone_id'], $b['timezone_id']);
70 }
71 return ($a['offset'] < $b['offset'] ? -1 : 1);
72 });
73
74 $aZones = ["" => GetMessage("tz_local_time")];
75 foreach ($aTZ as $z)
76 {
77 $offset = '';
78 if ($z['offset'] != 0)
79 {
80 $offset = ' ' . Main\Type\DateTime::secondsToOffset($z['offset'], ':');
81 }
82 $aZones[$z['timezone_id']] = '(UTC' . $offset . ') ' . $z['timezone_id'];
83 }
84
85 return $aZones;
86 }
87
88 public static function SetAutoCookie()
89 {
90 global $APPLICATION, $USER;
91
92 $cookiePrefix = COption::GetOptionString('main', 'cookie_name', 'BITRIX_SM');
93
94 if (self::IsAutoTimeZone($USER->GetParam("AUTO_TIME_ZONE")))
95 {
96 $cookieDate = (new \Bitrix\Main\Type\DateTime())->add("12M");
97 $cookieDate->setDate((int)$cookieDate->format('Y'), (int)$cookieDate->format('m'), 1);
98 $cookieDate->setTime(0, 0);
99
100 $tzFunc = '';
101 if ($USER->IsAuthorized())
102 {
103 CJSCore::Init(['ajax']);
104
105 $tzFunc = '
106 if (timezone !== "' . CUtil::JSEscape($USER->GetParam("TIME_ZONE")) . '")
107 {
108 BX.ready(function () {
109 BX.ajax.runAction(
110 "main.timezone.set",
111 {
112 data: {
113 timezone: timezone
114 }
115 }
116 );
117 });
118 }
119 ';
120 }
121
122 $APPLICATION->AddHeadString(
123 '<script>
124 if (Intl && Intl.DateTimeFormat)
125 {
126 const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
127 document.cookie = "' . $cookiePrefix . '_TZ=" + timezone + "; path=/; expires=' . $cookieDate->format("r") . '";
128 ' . $tzFunc . '
129 }
130 </script>',
131 true
132 );
133 }
134 elseif (isset($_COOKIE[$cookiePrefix . "_TZ"]))
135 {
136 // delete the cookie
137 setcookie($cookiePrefix . "_TZ", "", time() - 3600, "/");
138 }
139 }
140
141 public static function getTzCookie(): ?string
142 {
143 $context = Main\Context::getCurrent();
144 $cookie = $context?->getRequest()->getCookie('TZ');
145
146 return is_string($cookie)? $cookie : null;
147 }
148
149 public static function IsAutoTimeZone($autoTimeZone)
150 {
151 $autoTimeZone = trim((string)$autoTimeZone);
152
153 if ($autoTimeZone === "Y")
154 {
155 return true;
156 }
157
158 if (empty($autoTimeZone))
159 {
160 static $defAutoZone = null;
161 if ($defAutoZone === null)
162 {
163 $defAutoZone = (COption::GetOptionString("main", "auto_time_zone", "N") == "Y");
164 }
165
166 return $defAutoZone;
167 }
168
169 return false;
170 }
171
176 public static function GetCookieValue()
177 {
178 return null;
179 }
180
184 public static function SetCookieValue($timezoneOffset)
185 {
186 }
187
193 public static function GetOffset($USER_ID = null, $forced = false)
194 {
195 global $USER;
196
197 if ($forced)
198 {
199 if (!self::OptionEnabled())
200 {
201 return 0;
202 }
203 }
204 else
205 {
206 if (!self::Enabled())
207 {
208 return 0;
209 }
210 }
211
212 $timeZone = '';
213
214 if ($USER_ID !== null && $USER?->GetID() != $USER_ID)
215 {
216 $dbUser = CUser::GetList('id', 'asc', ['ID_EQUAL_EXACT' => $USER_ID], ['FIELDS' => ['TIME_ZONE', 'TIME_ZONE_OFFSET']]);
217 if (($arUser = $dbUser->Fetch()))
218 {
219 if (empty($arUser["TIME_ZONE"]))
220 {
221 // deprecated, return last known offset from the DB
222 return intval($arUser["TIME_ZONE_OFFSET"]);
223 }
224 $timeZone = $arUser["TIME_ZONE"];
225 }
226 }
227 elseif (is_object($USER))
228 {
229 // current user
230 $timeZone = (string)$USER->GetParam("TIME_ZONE");
231 if (empty($timeZone))
232 {
233 if (self::IsAutoTimeZone($USER->GetParam("AUTO_TIME_ZONE")))
234 {
235 if (($cookie = static::getTzCookie()) !== null)
236 {
237 // auto time zone from the cookie
238 $timeZone = $cookie;
239 }
240 }
241 }
242 }
243
244 return static::getTimezoneOffset($timeZone);
245 }
246
252 public static function getTimezoneOffset(string $timeZone): int
253 {
254 if (!self::OptionEnabled())
255 {
256 return 0;
257 }
258 if ($timeZone == '')
259 {
260 //default server time zone
261 $timeZone = COption::GetOptionString("main", "default_time_zone", "");
262 }
263
264 return static::calculateOffset($timeZone);
265 }
266
272 public static function calculateOffset(string $timeZone): int
273 {
274 if ($timeZone != '')
275 {
276 try //possible DateTimeZone incorrect timezone
277 {
278 $localOffset = (new DateTime())->getOffset();
279
280 $userTime = new DateTime('now', new DateTimeZone($timeZone));
281 $userOffset = $userTime->getOffset();
282
283 return $userOffset - $localOffset;
284 }
285 catch (Throwable)
286 {
287 }
288 }
289
290 return 0;
291 }
292}
global $APPLICATION
Определения include.php:80
static secondsToOffset($seconds, $delimiter='')
Определения datetime.php:71
static Init($arExt=array(), $bReturn=false)
Определения jscore.php:66
Определения time.php:13
static getTzCookie()
Определения time.php:141
static SetCookieValue($timezoneOffset)
Определения time.php:184
static GetCookieValue()
Определения time.php:176
static Disable()
Определения time.php:31
static GetZones()
Определения time.php:41
static GetOffset($USER_ID=null, $forced=false)
Определения time.php:193
static OptionEnabled()
Определения time.php:22
static IsAutoTimeZone($autoTimeZone)
Определения time.php:149
static calculateOffset(string $timeZone)
Определения time.php:272
static $enabled
Определения time.php:14
static getTimezoneOffset(string $timeZone)
Определения time.php:252
static $useTimeZones
Определения time.php:15
static SetAutoCookie()
Определения time.php:88
static Enable()
Определения time.php:36
static Enabled()
Определения time.php:17
global $USER
Определения csv_new_run.php:40
$context
Определения csv_new_setup.php:223
$aZones
Определения options.php:104
$z
Определения options.php:31
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
GetMessage($name, $aReplace=null)
Определения tools.php:3397
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$cookiePrefix
Определения quickway.php:248
else $a
Определения template.php:137