- Модуль: main
- Путь к файлу: ~/bitrix/modules/main/classes/general/time.php
- Класс: CTimeZone
- Вызов: CTimeZone::GetOffset
static function GetOffset($USER_ID = null, $forced = false)
{
global $USER;
if ($forced)
{
if (!self::OptionEnabled())
{
return 0;
}
}
else
{
if (!self::Enabled())
{
return 0;
}
}
try //possible DateTimeZone incorrect timezone
{
$timeZone = '';
if ($USER_ID !== null)
{
$dbUser = CUser::GetList('id', 'asc', ['ID_EQUAL_EXACT' => $USER_ID], ['FIELDS' => ['AUTO_TIME_ZONE', 'TIME_ZONE', 'TIME_ZONE_OFFSET']]);
if (($arUser = $dbUser->Fetch()))
{
if (self::IsAutoTimeZone(trim($arUser["AUTO_TIME_ZONE"])))
{
// can't detect auto timezone for a non-current user, return actual offset from the DB
return intval($arUser["TIME_ZONE_OFFSET"]);
}
$timeZone = $arUser["TIME_ZONE"];
}
}
elseif (is_object($USER))
{
// current user
$autoTimeZone = $USER->GetParam("AUTO_TIME_ZONE") ?: '';
if (self::IsAutoTimeZone(trim($autoTimeZone)))
{
if (($cookie = static::getTzCookie()) !== null)
{
// auto time zone from the cookie
$timeZone = $cookie;
}
elseif (($cookie = static::GetCookieValue()) !== null)
{
//auto time offset from old cookie - deprecated
$localOffset = (new DateTime())->getOffset();
$userOffset = -($cookie) * 60;
return $userOffset - $localOffset;
}
}
else
{
// user set time zone manually
$timeZone = $USER->GetParam("TIME_ZONE");
}
}
if ($timeZone == '')
{
//default server time zone
$timeZone = COption::GetOptionString("main", "default_time_zone", "");
}
if ($timeZone != '')
{
$localOffset = (new DateTime())->getOffset();
$userTime = new DateTime('now', new DateTimeZone($timeZone));
$userOffset = $userTime->getOffset();
return $userOffset - $localOffset;
}
}
catch (Throwable $e)
{
}
return 0;
}