From eeda22d0badbcd187b02999138becae0e771fd2a Mon Sep 17 00:00:00 2001 From: Michael Yoo Date: Sun, 5 Oct 2014 01:29:07 +0930 Subject: [PATCH] Add portable linux timezone detection for incompatible linux distributions. --- src/pocketmine/PocketMine.php | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/pocketmine/PocketMine.php b/src/pocketmine/PocketMine.php index e897a5c9f..3a6590469 100644 --- a/src/pocketmine/PocketMine.php +++ b/src/pocketmine/PocketMine.php @@ -230,6 +230,44 @@ namespace pocketmine { } } + //Portable method for incompatible linux distributions. + + $offset = exec('date +%:z'); + + 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; break; case 'mac':