Ruel »
10 February 2011 »
In Internet, Programming, Python »
There are many ways to obtain the current timezone from an IP Address. One is by using a database, and looking up the address. This is an effective way, but the database must be updated frequently. Another way is by using an API that displays location information (and timezone) from a given IP. And this is what I will show you in this post.
Actually, I built a web application that needs the visitor’s current date. And after a couple of hours of googling, I found a reliable API that can help me, IPInfoDB.
The API is pretty simple, and it’s free (registration required). To use it, just pass your API key and the IP address you want to lookup.
http://api.ipinfodb.com/v2/ip_query.php?key=<your_api_key>&ip=75.125.71.106&output=json&timezone=true
It will return something like:
{
"Status" : "OK",
"CountryCode" : "US",
"CountryName" : "United States",
"RegionCode" : "48",
"RegionName" : "Texas",
"City" : "Houston",
"ZipPostalCode" : "77002",
"Latitude" : "29.7523",
"Longitude" : "-95.367",
"Gmtoffset" : "-21600",
"Dstoffset" : "0",
"TimezoneName" : "America/Chicago",
"Isdst" : "0",
"Ip" : "75.125.71.106"
}
Now, it gave us enough data to accomplish our task. Well actually, the only data we need is Gmtoffset. The next thing we do is add that to the current UTC time. We can do that in Python:
>>> import datetime
>>> gmtoffset = '-21600'
>>> utctime = datetime.datetime.utcnow()
>>> utctime.strftime('%X %x')
'06:37:48 02/10/11'
>>> iptime = utctime + datetime.timedelta(0, int(gmtoffset))
>>> iptime.strftime('%X %x')
'00:37:48 02/10/11'
Take note that the GMT offset is given in seconds. And if we will convert -21600 seconds to hours, we will have exactly, -6 hours. The datetime.timedelta() method made the addition possible for us, and we just have to supply the number of seconds to add.
Continue reading...
Tags: address, datetime, getting, ip, python, time
Ruel »
04 February 2011 »
In Life »
We all love the concept of time travel, and scientifically, I do believe in time travel. But, only to the future. As stated on the Grandfather Paradox, time travel to the past seems impossible. And based on the Twin Paradox, time travel to the future seems possible. Well, last night, I was given a chance to travel to the past.
It was one of the weirdest dreams I had lately. Started when I was walking on my way to school, I closed my eyes. The moment I opened it, I was in the classroom, with my classmates, but all were complete strangers to one another, except me. I realized I somehow traveled to the past, to the date of our first day in college (in LPU). I started talking to my classmates, saying I was from the future, and I don’t have an idea how I got here. I proved it by saying details about each one of them. And I can’t describe their faces.
On that very moment, I woke up. I was shocked because I thought the whole thing was real (happens every time to me). It kept me awake for 30 mins, starting from 4:00AM in the morning. I was literally staring at the ceiling, and it made me think.
What if that really happened?
If that really happened, I think I have the chance to make things right, not only for myself, but for the whole world. It sounds sill isn’t it? But really, there are a lot of things that happened from June 2009 up to this time, and all those things can be changed. But the fear of making things worse entered in my head. There are a lot of people died, in my country for example: the massacre in Maguindanao, hostage crisis, vehicular accidents, huge storms. The IPv4 depletion, Egypt Revolution. I believe I can change all of those. But yeah, when the time I publicly announce those events, it may trigger a change, and two things can happen. Something better, and something worse.
This dream taught me, that everything that happened, good or bad, was part of God’s plan. And it always lead to something better. We may not be satisfied what our eyes tell us right now. And all we need is to trust the Lord, and his word.
Continue reading...
Tags: change, dream, past, time, travel