| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace GeoTimeZone; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use DateTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use DateTimeZone; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use ErrorException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use GeoTimeZone\Quadrant\Tree; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class Calculator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected $quadrantTree; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * TimeZone constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @param $dataDirectory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @throws ErrorException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 22 |  |     public function __construct($dataDirectory = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 22 |  |         if (isset($dataDirectory) && is_dir($dataDirectory)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 22 |  |             $this->quadrantTree = new Tree($dataDirectory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 22 |  |             $this->quadrantTree->initializeDataTree(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         }else{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             throw new ErrorException('Invalid data tree directory: ' . $dataDirectory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 22 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Adjust the latitude value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param $latitude | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @return float|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @throws ErrorException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 20 |  |     protected function adjustLatitude($latitude) | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 20 |  |         $newLatitude = $latitude; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 20 |  |         if (null == $latitude || abs($latitude) > Tree::MAX_ABS_LATITUDE) { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 2 |  |             throw new ErrorException('Invalid latitude: ' . $latitude); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 18 |  |         if (abs($latitude) == Tree::MAX_ABS_LATITUDE) { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 4 |  |             $newLatitude = ($latitude <=> 0) * Tree::ABS_LATITUDE_LIMIT; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 18 |  |         return $newLatitude; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * Adjust longitude value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param $longitude | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @return float|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @throws ErrorException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 18 |  |     protected function adjustLongitude($longitude) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 18 |  |         $newLongitude = $longitude; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 18 |  |         if (null == $longitude || abs($longitude) > Tree::MAX_ABS_LONGITUDE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |             throw new ErrorException('Invalid longitude: ' . $longitude); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 16 |  |         if (abs($longitude) == Tree::MAX_ABS_LONGITUDE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 4 |  |             $newLongitude = ($longitude <=> 0) * Tree::ABS_LONGITUDE_LIMIT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 16 |  |         return $newLongitude; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * Get timezone name from a particular location (latitude, longitude) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param $latitude | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param $longitude | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @throws ErrorException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 18 |  |     public function getTimeZoneName($latitude, $longitude) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 18 |  |         $timeZone = Tree::NONE_TIMEZONE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 18 |  |             $latitude = $this->adjustLatitude($latitude); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 16 |  |             $longitude = $this->adjustLongitude($longitude); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 14 |  |             $timeZone = $this->quadrantTree->lookForTimezone($latitude, $longitude); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 11 |  |         }catch (ErrorException $error){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 11 |  |             throw $error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 7 |  |         return $timeZone; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * Get the local date belonging to a particular latitude, longitude and timestamp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param $latitude | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param $longitude | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param $timestamp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return DateTime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @throws ErrorException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 4 |  |     public function getLocalDate($latitude, $longitude, $timestamp) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 4 |  |         $date = new DateTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 4 |  |             $timeZone = $this->getTimeZoneName($latitude, $longitude); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 3 |  |             $date->setTimestamp($timestamp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 3 |  |             if ($timeZone != Tree::NONE_TIMEZONE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 3 |  |                 $date->setTimezone(new DateTimeZone($timeZone)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |         }catch (ErrorException $error){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |             throw $error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 3 |  |         return $date; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * Get timestamp from latitude, longitude and localTimestamp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param $latitude | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @param $longitude | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @param $localTimestamp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @throws ErrorException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 3 |  |     public function getCorrectTimestamp($latitude, $longitude, $localTimestamp) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 3 |  |         $timestamp = $localTimestamp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 3 |  |             $timeZoneName = $this->getTimeZoneName($latitude, $longitude); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 2 |  |             if ($timeZoneName != Tree::NONE_TIMEZONE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 2 |  |                 $date = new DateTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 2 |  |                 $date->setTimestamp($localTimestamp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 2 |  |                 if ($timeZoneName != null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 2 |  |                     $date->setTimezone(new DateTimeZone($timeZoneName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 2 |  |                 $timestamp = $date->getOffset() != false ? $localTimestamp - $date->getOffset() : $localTimestamp; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 1 |  |         }catch(ErrorException $error){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  |             throw $error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 2 |  |         return $timestamp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |  |