php DateTimeZone->getOffset() returns invalid offset for GMT timezones
Hopefully someone notices what is the cause for the problem I'm having.
For all other timezones such as "Europe/Helsinki" the offs开发者_开发知识库et calculation below returns correct values, but for some reason I get negative value for 'Etc/GMT+2' style timezones and vice versa (e.g. for Etc/GMT-2 I get positive value, 7200).
$dateTimeZone = new DateTimeZone('Etc/GMT+2');
echo $dateTimeZone->getOffset(new DateTime("now", new DateTimezone( 'UTC' )));
Expected: 7200 Result: -7200
The Etc/GMT+2
timezone is what would otherwise be called "GMT -2" or represented like <date time>-02:00
.
This is due to the way that POSIX represents those types of timezone offsets:
The offset specifies the time value you must add to the local time to get a Coordinated Universal Time value. It has syntax like [+|-]hh[:mm[:ss]]. This is positive if the local time zone is west of the Prime Meridian and negative if it is east. The hour must be between 0 and 23, and the minute and seconds between 0 and 59.
From TZ Variable.
And:
Numeric time zone abbreviations typically count hours east of UTC, e.g., +09 for Japan and -10 for Hawaii. However, the POSIX TZ environment variable uses the opposite convention. For example, one might use TZ="JST-9" and TZ="HST10" for Japan and Hawaii, respectively. If the tz database is available, it is usually better to use settings like TZ="Asia/Tokyo" and TZ="Pacific/Honolulu" instead, as this should avoid confusion, handle old time stamps better, and insulate you better from any future changes to the rules. One should never set POSIX TZ to a value like "GMT-9", though, since this would falsely claim that local time is nine hours ahead of UTC and the time zone is called "GMT".
From Sources for Time Zone and Daylight Saving Time Data - Time notation.
From http://www.php.net/manual/en/timezones.others.php#64310
The plus and minus signs (+/-) are not intuitive. For example,
"Etc/GMT-10" actually refers to the timezone "(GMT+10:00) Canberra,Sydney,Melbourne".
精彩评论