Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Commit

Permalink
Re-use timezone offset parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
msjyoo committed Oct 4, 2014
1 parent eeda22d commit e0a7944
Showing 1 changed file with 43 additions and 64 deletions.
107 changes: 43 additions & 64 deletions src/pocketmine/PocketMine.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ function dummy(){
}
}

function detect_system_timezone()
{
function detect_system_timezone(){
switch(Utils::getOS()){
case 'win':
$regex = '/(?:Time Zone:\s*\()(UTC)(\+*\-*\d*\d*\:*\d*\d*)(?:\))/';
Expand All @@ -181,37 +180,9 @@ function detect_system_timezone()

if($offset == ""){
return "UTC";
}else{
//Make signed offsets unsigned for date_parse
if(strpos($offset, '-') !== false){
$negative_offset = true;
$offset = str_replace('-', '', $offset);
}else if(strpos($offset, '+') !== false){
$negative_offset = false;
$offset = str_replace('+', '', $offset);
}else{
return false;
}

$parsed = date_parse($offset);
$offset = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];

//After date_parse is done, put the sign back
if($negative_offset == true){
$offset = -abs($offset);
}

//And then, look the offset up.
//timezone_name_from_abbr is not used because it returns false on some(most) offsets because it's mapping function is weird.
//That's been a bug in PHP since 2008!
foreach(timezone_abbreviations_list() as $zones){
foreach($zones as $timezone){
if($timezone['offset'] == $offset){
return $timezone['timezone_id'];
}
}
}
}

return parse_offset($offset);
break;
case 'linux':
// Ubuntu / Debian.
Expand All @@ -236,44 +207,14 @@ function detect_system_timezone()

if($offset == "+00:00"){
return "UTC";
}else{
//Make signed offsets unsigned for date_parse
if(strpos($offset, '-') !== false){
$negative_offset = true;
$offset = str_replace('-', '', $offset);
}else if(strpos($offset, '+') !== false){
$negative_offset = false;
$offset = str_replace('+', '', $offset);
}else{
return false;
}

$parsed = date_parse($offset);
$offset = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];

//After date_parse is done, put the sign back
if($negative_offset == true){
$offset = -abs($offset);
}

//And then, look the offset up.
//timezone_name_from_abbr is not used because it returns false on some(most) offsets because it's mapping function is weird.
//That's been a bug in PHP since 2008!
foreach(timezone_abbreviations_list() as $zones){
foreach($zones as $timezone){
if($timezone['offset'] == $offset){
return $timezone['timezone_id'];
}
}
}
}

return false;
return parse_offset($offset);
break;
case 'mac':
if(is_link('/etc/localtime')){
$filename = readlink('/etc/localtime');
if (strpos($filename, '/usr/share/zoneinfo/') === 0){
if(strpos($filename, '/usr/share/zoneinfo/') === 0){
$timezone = substr($filename, 20);
return $timezone;
}
Expand All @@ -285,6 +226,44 @@ function detect_system_timezone()
return false;
break;
}
}

/**
* @param string $offset In the format of +09:00, +02:00, -04:00 etc.
* @return string
*/
function parse_offset($offset){
//Make signed offsets unsigned for date_parse
if(strpos($offset, '-') !== false){
$negative_offset = true;
$offset = str_replace('-', '', $offset);
}else{
if(strpos($offset, '+') !== false){
$negative_offset = false;
$offset = str_replace('+', '', $offset);
}else{
return false;
}
}

$parsed = date_parse($offset);
$offset = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];

//After date_parse is done, put the sign back
if($negative_offset == true){
$offset = -abs($offset);
}

//And then, look the offset up.
//timezone_name_from_abbr is not used because it returns false on some(most) offsets because it's mapping function is weird.
//That's been a bug in PHP since 2008!
foreach(timezone_abbreviations_list() as $zones){
foreach($zones as $timezone){
if($timezone['offset'] == $offset){
return $timezone['timezone_id'];
}
}
}

return false;
}
Expand Down

0 comments on commit e0a7944

Please sign in to comment.