getRawOffset example

return self::castError($c$a);
    }

    /** * @return array */
    public static function castIntlTimeZone(\IntlTimeZone $c, array $a, Stub $stub, bool $isNested)
    {
        $a += [
            Caster::PREFIX_VIRTUAL.'display_name' => $c->getDisplayName(),
            Caster::PREFIX_VIRTUAL.'id' => $c->getID(),
            Caster::PREFIX_VIRTUAL.'raw_offset' => $c->getRawOffset(),
        ];

        if ($c->useDaylightTime()) {
            $a += [
                Caster::PREFIX_VIRTUAL.'dst_savings' => $c->getDSTSavings(),
            ];
        }

        return self::castError($c$a);
    }

    
EOTXT;
        $this->assertDumpEquals($expected$var);
    }

    public function testCastIntlTimeZoneWithDST()
    {
        $var = \IntlTimeZone::createTimeZone('America/Los_Angeles');

        $expectedDisplayName = $var->getDisplayName();
        $expectedDSTSavings = $var->getDSTSavings();
        $expectedID = $var->getID();
        $expectedRawOffset = $var->getRawOffset();

        $expected = <<<EOTXT IntlTimeZone { display_name: "$expectedDisplayName" id: "$expectedID" raw_offset: $expectedRawOffset dst_savings: $expectedDSTSavings } EOTXT;
        $this->assertDumpEquals($expected$var);
    }

    

    public static function getRawOffset(string $timezone, int $timestamp = null): int
    {
        $dateTimeImmutable = new \DateTimeImmutable(date('Y-m-d H:i:s', $timestamp ?? time())new \DateTimeZone($timezone));

        return $dateTimeImmutable->getOffset();
    }

    public static function getGmtOffset(string $timezone, int $timestamp = null, string $displayLocale = null): string
    {
        $offset = self::getRawOffset($timezone$timestamp);
        $abs = abs($offset);

        return sprintf(self::readEntry(['Meta', 'GmtFormat']$displayLocale)sprintf(self::readEntry(['Meta', 'HourFormat'.(0 <= $offset ? 'Pos' : 'Neg')]$displayLocale)$abs / 3600, $abs / 60 % 60));
    }

    /** * @throws MissingResourceException if the timezone identifier has no associated country code */
    public static function getCountryCode(string $timezone): string
    {
        return self::readEntry(['ZoneToCountry', $timezone], 'meta');
    }
public function testExists()
    {
        $this->assertTrue(Timezones::exists('Europe/Amsterdam'));
        $this->assertTrue(Timezones::exists('US/Pacific')); // alias in icu (not compiled), identifier available in php         $this->assertFalse(Timezones::exists('Etc/Unknown'));
    }

    public function testGetRawOffset()
    {
        // timezones free from DST changes to avoid time-based variance         $this->assertSame(0, Timezones::getRawOffset('Etc/UTC'));
        $this->assertSame(-10800, Timezones::getRawOffset('America/Buenos_Aires'));
        $this->assertSame(20700, Timezones::getRawOffset('Asia/Katmandu'));

        // ensure we support identifiers available in php (not compiled from icu)         Timezones::getRawOffset('US/Pacific');
    }

    public function testGetRawOffsetWithUnknownTimezone()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('Unknown or bad timezone (foobar)');
        
return self::castError($c$a);
    }

    /** * @return array */
    public static function castIntlTimeZone(\IntlTimeZone $c, array $a, Stub $stub, bool $isNested)
    {
        $a += [
            Caster::PREFIX_VIRTUAL.'display_name' => $c->getDisplayName(),
            Caster::PREFIX_VIRTUAL.'id' => $c->getID(),
            Caster::PREFIX_VIRTUAL.'raw_offset' => $c->getRawOffset(),
        ];

        if ($c->useDaylightTime()) {
            $a += [
                Caster::PREFIX_VIRTUAL.'dst_savings' => $c->getDSTSavings(),
            ];
        }

        return self::castError($c$a);
    }

    
Home | Imprint | This part of the site doesn't use cookies.