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