File tree 2 files changed +38
-0
lines changed
Filter options
src/Symfony/Component/Intl
2 files changed +38
-0
lines changed
Original file line number Diff line number Diff line change @@ -529,4 +529,18 @@ public function testExists()
529
529
$ this ->assertTrue (Timezones::exists ('Europe/Amsterdam ' ));
530
530
$ this ->assertFalse (Timezones::exists ('Etc/Unknown ' ));
531
531
}
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
+ }
532
546
}
Original file line number Diff line number Diff line change 12
12
namespace Symfony \Component \Intl ;
13
13
14
14
use Symfony \Component \Intl \Exception \MissingResourceException ;
15
+ use Symfony \Component \Intl \Exception \RuntimeException ;
15
16
16
17
/**
17
18
* Gives access to timezone-related ICU data.
@@ -52,6 +53,29 @@ public static function getNames(string $displayLocale = null): array
52
53
return self ::asort (self ::readEntry (['Names ' ], $ displayLocale ), $ displayLocale );
53
54
}
54
55
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
+
55
79
protected static function getPath (): string
56
80
{
57
81
return Intl::getDataDirectory ().'/ ' .Intl::TIMEZONE_DIR ;
You can’t perform that action at this time.
0 commit comments