Google Map Arrival Time into Datetime Object

Goal: Get the arrival time from Google Map’s API and convert it into a datetime object

So I started to incorporated my Google Map Duration code into Rideminder, but then I realized that it doesn’t matter how long the duration of the trip is! HA! I care about what google has estimated as their arrival time. If I can get the that then I can send it into my database. So lets play with getting the arrival time from what I have.

parsepmI had to change my rawjson request from ‘duration’ to ‘arrival_time’, which worked wonderfully! — this  is how I parse out the data that I am looking for from my rawjson that I get from my google map api request, more information about that is from an earlier post — This gives me data like: “3:40pm”, “11:12am” and “8:00pm.” So instead of working with milliseconds I decided to change it into a datetime object (since my postgres has a column type for datetime). I know that I’ll have to parse this data out anyways, I went ahead and did that before my datetime research. I saved the parsed rawjson as arrival_time_raw and then made a arrival_time_raw_split and split it on the ‘:’. I placed an if statement that if the last two items in the arrival_time_raw are ‘pm’ for it set arrival_time_hour to 12 (so it changes the pm into 24hour time). Then I processed the hour and min split items. Cool!

datetimeplayNow I need to figure out this whole datetime thing. I started with the docs: 8.1 Datetime. There are so many different options and choices. Going through it, I’m thinking datetime.datetime will work for me. But I wanted to find other sources. I found an article by Marina Mele called “13 Useful Tips About Python datetime Objects“. I totally loved it since it had short, simple examples. I think I appreciate having something simple at first, so my mind can digest the concept in pieces versus learning about the concept and ALL THE THINGS it can do. I played around in -i python (this is interactive python, for more info). I created datetime objects and played with the idea of finding the differences between two datetime objects. Then I found out that I can create datetime objects with the current information and edit them.

replaceTo use the arrival time to make a datetime object with its information, you can create a “now” with ‘datetime.datetime.now().’ Then with the parsed out json of the arrival time, I can place the future time (arrival time) into the now datetime object using .replace() – creating my arrival time’s datetime object. Yeah!

I removed two of the original functions and replaced it with one that processes rawjson, to parse out the arrival time and creates the datetime object with the arrival time information.

Check out the source code

datetime

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s