1

I have a web application (Rails 4) using Google maps. When a user clicks on a point on the map, the Google map API returns the latitude and longitude. Now, I want to get the current time at that latitude and longitude as well and I'm not sure which route to take to do this. I'd appreciate your advice:

  • Use Google Timezone API: the problem here is that Google requires me sending a timestamp. My understanding is that timestamp is the current UTC time that I send from the server to the client and then client's computer sends this timestamp to Google server. Now suppose the user is on the website from 11:55pm to 12:05am. At 12:05am, the user is still having 11:55pm as timestamp (1 day difference) unless I refresh it. Getting current time shouldn't be this complicated and requiring me refreshing and so on.
  • Use TimeZone gem: The way I can think of implementing this is that when user clicks on the map, the client sends an Ajax call to my server and then my server sends a request to geonames.org and then the current times is sent to the client. Here there are two server hoppings and I expect delay.

Any easier/faster solution? I'm open to using any other API, gem, etc.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
user2725109
  • 2,286
  • 4
  • 27
  • 46
  • Hmmm.. The question marked as duplicate is about JavaScript with jQuery. This question is tagged as `ruby-on-rails`. I don't think it's a good match for duplicate. Voting to reopen. – Matt Johnson-Pint Jul 17 '14 at 15:38

1 Answers1

1

My understanding is that timestamp is the current UTC time that I send from the server to the client and then client's computer sends this timestamp to Google server.

The timestamp is whatever you decide to send. Sure, you can have the server generate a timestamp and have the client hold onto it and then pass it along to the request, but then you'll have the behavior you described. There's no good reason to do this.

If you're calling the API from the client, then your question is not related to Rails, but to JavaScript. There, you would let JavaScript get the current time from the clock of the computer it's running on (as per the answer linked in the question comments).

However, rails runs on the server, so if you instead elect to have Rails call the API from your server, then you would pass along a timestamp generated by your Rails code.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Thanks for your answer. But what happens when the clock on client's computer is off or not accurate? Is there a way to not rely on the client's clock? (That's why I wanted to rely on either Rails server or Google server for an accurate "timestamp"). – user2725109 Jul 16 '14 at 21:21
  • Then make your call *from rails*. I am not a rails programmer, but it should be relatively straightforward for how to make an HTTP call from within your server-side application code. – Matt Johnson-Pint Jul 16 '14 at 21:55
  • 1
    This may be helpful: [How make a HTTP GET request using Ruby on Rails?](http://stackoverflow.com/q/4581075/634824) – Matt Johnson-Pint Jul 16 '14 at 21:58