Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ac4b322

Browse filesBrowse files
committed
feature #31295 [Intl] Add timezone offset utilities (ro0NL)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Intl] Add timezone offset utilities | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no | Tests pass? | yes (including intl-data group) | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Commits ------- b166e33 [Intl] Add timezone offset utilities
2 parents 002b48d + b166e33 commit ac4b322
Copy full SHA for ac4b322

File tree

2 files changed

+38
-0
lines changed
Filter options

2 files changed

+38
-0
lines changed

‎src/Symfony/Component/Intl/Tests/TimezonesTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Intl/Tests/TimezonesTest.php
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,4 +529,18 @@ public function testExists()
529529
$this->assertTrue(Timezones::exists('Europe/Amsterdam'));
530530
$this->assertFalse(Timezones::exists('Etc/Unknown'));
531531
}
532+
533+
public function testGetRawOffset()
534+
{
535+
$this->assertSame(0, Timezones::getRawOffset('Etc/UTC'));
536+
$this->assertSame(-10800, Timezones::getRawOffset('America/Buenos_Aires'));
537+
$this->assertSame(20700, Timezones::getRawOffset('Asia/Katmandu'));
538+
}
539+
540+
public function testGetGmtOffset()
541+
{
542+
$this->assertSame('GMT+00:00', Timezones::getGmtOffset('Etc/UTC'));
543+
$this->assertSame('GMT-03:00', Timezones::getGmtOffset('America/Buenos_Aires'));
544+
$this->assertSame('GMT+05:45', Timezones::getGmtOffset('Asia/Katmandu'));
545+
}
532546
}

‎src/Symfony/Component/Intl/Timezones.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Intl/Timezones.php
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Intl;
1313

1414
use Symfony\Component\Intl\Exception\MissingResourceException;
15+
use Symfony\Component\Intl\Exception\RuntimeException;
1516

1617
/**
1718
* Gives access to timezone-related ICU data.
@@ -52,6 +53,29 @@ public static function getNames(string $displayLocale = null): array
5253
return self::asort(self::readEntry(['Names'], $displayLocale), $displayLocale);
5354
}
5455

56+
public static function getRawOffset(string $timezone, int $timestamp = null): int
57+
{
58+
if (null === $timestamp) {
59+
$timestamp = time();
60+
}
61+
62+
$transitions = (new \DateTimeZone($timezone))->getTransitions($timestamp, $timestamp);
63+
64+
if (!isset($transitions[0]['offset'])) {
65+
throw new RuntimeException('No timezone transitions available.');
66+
}
67+
68+
return $transitions[0]['offset'];
69+
}
70+
71+
public static function getGmtOffset(string $timezone, int $timestamp = null): string
72+
{
73+
$offset = self::getRawOffset($timezone, $timestamp);
74+
$abs = abs($offset);
75+
76+
return sprintf('GMT%s%02d:%02d', 0 <= $offset ? '+' : '-', $abs / 3600, $abs / 60 % 60);
77+
}
78+
5579
protected static function getPath(): string
5680
{
5781
return Intl::getDataDirectory().'/'.Intl::TIMEZONE_DIR;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.