-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 56b5d97
Showing
6 changed files
with
22,782 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
Example | ||
------- | ||
|
||
curl -H "Authorization: Basic geoloqi:api" "http://timezone-api.geoloqi.com/timezone?latitude=45.5118&longitude=-122.6433" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
include('inc.php'); | ||
|
||
$db = new PDO(PDO_DSN, PDO_USER, PDO_PASS, array(PDO::ATTR_PERSISTENT => false)); | ||
|
||
$timezone = timezoneFromLocation($argv[1], $argv[2]); | ||
|
||
if($timezone) { | ||
$tz = new DateTimeZone($timezone); | ||
$now = new DateTime(date('c')); | ||
$now->setTimeZone($tz); | ||
|
||
echo "timezone: " . $timezone . "\n"; | ||
echo "offset: " . $now->format('P') . "\n"; | ||
echo "seconds: " . (int)$now->format('Z') . "\n"; | ||
} else { | ||
echo "No data found for " . $argv[1] . ", " . $argv[2] . "\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
include('inc.php'); | ||
|
||
$db = new PDO(PDO_DSN, PDO_USER, PDO_PASS, array(PDO::ATTR_PERSISTENT => false)); | ||
|
||
|
||
/* | ||
$query = $db->prepare('CREATE TABLE "timezone" ( | ||
"id" SERIAL, | ||
"location" "geography", | ||
"timezone" varchar(100) | ||
)'); | ||
$query->execute(); | ||
$query = $db->prepare('ALTER TABLE "timezone" OWNER TO gisgroup'); | ||
$query->execute(); | ||
*/ | ||
|
||
|
||
$data = file('tz_cities.txt'); | ||
|
||
foreach($data as $line) { | ||
if(preg_match('/([0-9\.-]+) ([0-9\.-]+) (.+)/', $line, $match)) { | ||
$latitude = $match[1]; | ||
$longitude = $match[2]; | ||
$timezone = trim($match[3]); | ||
|
||
echo $latitude . ', ' . $longitude . "\t" . $timezone . "\n"; | ||
$query = $db->prepare('INSERT INTO timezone (location, timezone) VALUES (ST_GeographyFromText(\'SRID=4326;POINT(' . $longitude . ' ' . $latitude . ')\'), \'' . $timezone . '\')'); | ||
$query->execute(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
define('PDO_DSN', 'pgsql:dbname=timezone;host=127.0.0.1'); | ||
define('PDO_USER', 'loqi'); | ||
define('PDO_PASS', 'tinydino'); | ||
|
||
function timezoneFromLocation($latitude, $longitude) { | ||
global $db; | ||
|
||
$result = FALSE; | ||
|
||
// First check for cities within 100km | ||
$timezones = $db->prepare("SELECT timezone FROM timezone WHERE ST_DWithin(ST_GeographyFromText('SRID=4326;POINT(" . $longitude . ' ' . $latitude . ")'), location, 100000, false) order by st_distance(ST_GeographyFromText('SRID=4326;POINT(" . $longitude . ' ' . $latitude . ")'), location) LIMIT 1"); | ||
$timezones->execute(); | ||
|
||
while($tz = $timezones->fetch()) { | ||
$result = $tz['timezone']; | ||
} | ||
|
||
// $latN = $latitude - 1 % 180; | ||
// $latS = $latitude + 1 % 180; | ||
// $lngE = $longitude + 1 % 360; | ||
// $lngW = $longitude - 1 % 360; | ||
|
||
// $timezones = $db->prepare("SELECT timezone FROM timezone WHERE lat > $latN AND lat <= $latS AND lng > $lngW AND lng < $lngE ORDER BY st_distance(ST_GeographyFromText('SRID=4326;POINT(" . $longitude . ' ' . $latitude . ")'), location) LIMIT 1"); | ||
// $timezones->execute(); | ||
// while($tz = $timezones->fetch()) { | ||
// $result = $tz['timezone']; | ||
// } | ||
|
||
if($result) | ||
return $result; | ||
|
||
// If nothing found that close, check again within 6000km | ||
$timezones = $db->prepare("SELECT timezone FROM timezone WHERE ST_DWithin(ST_GeographyFromText('SRID=4326;POINT(" . $longitude . ' ' . $latitude . ")'), location, 6000000, false) order by st_distance(ST_GeographyFromText('SRID=4326;POINT(" . $longitude . ' ' . $latitude . ")'), location) LIMIT 1"); | ||
$timezones->execute(); | ||
while($tz = $timezones->fetch()) { | ||
$result = $tz['timezone']; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
function get($k) { | ||
return array_key_exists($k, $_GET) ? $_GET[$k] : FALSE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
include('inc.php'); | ||
|
||
if(preg_match('|^/timezone/([0-9\.-]+)/([0-9\.-]+)$|', $_SERVER['REQUEST_URI'], $match) || (get('latitude') && get('longitude'))) { | ||
$db = new PDO(PDO_DSN, PDO_USER, PDO_PASS, array(PDO::ATTR_PERSISTENT => TRUE)); | ||
|
||
$latitude = get('latitude') ?: $match[1]; | ||
$longitude = get('longitude') ?: $match[2]; | ||
|
||
$timezone = timezoneFromLocation($latitude, $longitude); | ||
|
||
header('Content-type: application/json'); | ||
|
||
if($timezone) { | ||
$tz = new DateTimeZone($timezone); | ||
$now = new DateTime(date('c')); | ||
$now->setTimeZone($tz); | ||
|
||
echo json_encode(array( | ||
'timezone' => $timezone, | ||
'offset' => $now->format('P'), | ||
'seconds' => (int)$now->format('Z') | ||
)); | ||
} else { | ||
echo json_encode(array( | ||
'error' => 'no_data' | ||
)); | ||
} | ||
|
||
} else { | ||
header('HTTP/1.1 404 Not Found'); | ||
header('Content-type: application/json'); | ||
echo json_encode(array( | ||
'error' => 'not_found' | ||
)); | ||
} | ||
|
Oops, something went wrong.