It is extremely important that any application where the users are from different geographic locations handle date and time in a meaningful way. For an example, let’s say the application has users ranging from United States to Australia and right now, a user from Sydney, Australia creates a record in the database. So through out the application if we have only considered Sydney time, the created time for that particular record will be in Sydney time. If a user from Seattle, United States sees that record created time, it definitely is confusing because that time has not yet arrived to Seattle.
So in a world wide application, it is important to consider users’ time zones when maintaining date and times. There are variety of SDK for this such as Noda Time. But in this post, let’s see how we can consider not the time zones, but the geographic locations when converting the date time. For that we can use Google Time Zone API to convert date and time between geographic locations considering locations’ geographic coordinates.
Please note that to use Google Time Zone API, you will need to have a API key which can be acquired for free. Free API will have some request limitations, but it is more than enough to evaluate the functionality. In here, I am not going to explain how you can obtain the API key, please read this post to know how you can do it.
After getting the API key, next is to use it. Google Time Zone API expects following parameters.
- Timestamp specifies the given time as seconds since midnight, January 1, 1970 UTC. The Time Zone API uses the timestamp to determine whether or not Daylight Savings should be applied.
- Location's geographic coordinates
- API Key
- Language (optional)
If the request to Google TimeZone API gets succeeded, it will return a result containing details such as the offset for daylight-savings time in seconds (dstOffset), the offset from UTC in seconds for the given location(rawOffset), time zone name etc. The converted time of a given location is the sum of the timestamp parameter, dstOffset and rawOffset. Since it is again a Timestamp, we need to convert it back to DateTime value.
I am creating a console application and I am creating a class named “GoogleTimeZone”. There I have couple of local variables.Now let's create following helper methods.
First method is a method to return the Timestamp of a given DateTime.
Then the following method will do the opposite which is converting of Timestamp to DateTime.
I have the following helper class to hold coordinates.
We now have the location and the time stamp. Following method will call the Google Time Zone API and get the converted time zone result.I am grouping up the result to a class named GoogleTimeZoneResult as follows.
Finally I have the following public method which will trigger all the above methods.
That’s it. now let’s test the functionality using some test DateTime and calling the above public method from the Main.
Colombo, Sri Lanka
|Colombo, Sri Lanka|
|Seattle, United States|