Skip to content

Commit

Permalink
First commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronpk committed Aug 11, 2012
0 parents commit 56b5d97
Show file tree
Hide file tree
Showing 6 changed files with 22,782 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
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"

18 changes: 18 additions & 0 deletions cli.php
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";
}
33 changes: 33 additions & 0 deletions import.php
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();
}
}

45 changes: 45 additions & 0 deletions inc.php
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;
}
37 changes: 37 additions & 0 deletions index.php
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'
));
}

Loading

0 comments on commit 56b5d97

Please sign in to comment.