Search code examples
c#datetimetimezone-offsetgoogle-maps-timezone

Time zone conversion using time zone city coordinates and local time


I'm developing a scheduling web site where you can specify a date, time, time zone city. I want to save this in the backend in UTC time. So I have to convert it somehow on the server. I also want to provide the ability to convert to a different time zone on-the-fly.

In the Javascript I have a date/time value as well as a time zone city (from http://www.citytimezones.info which gives me geographical coordinates). I don't have the UTC time, only the time in the specified time zone.

Now I need to convert this future local time to UTC. Of course, it has to take into account the daylight savings time offset that will apply at that time.

Moreover I would like to convert to a different local time using a second time zone city.

I thought about using the Google Maps Time Zone API but this forces you to pass in the UTC time, which I don't have because I'm trying to calculate it.

In other words, ideally I need a system that will take these parameters:

  • Future time/date
  • Source time zone city coordinates
  • Destination time zone city coordinates (or null = UTC time)

and will return the converted time.

Any idea what I could do to make this happen?

This could be either a web service or a Windows C# library.


Solution

  • Split the problem into two parts:

    • Determining the time zone from location cooridnates
    • Converting time between time zones

    You will find several ways to to do each.

    For step 1, you mentioned Google Time Zone API, and CityTimeZones. Both of those are listed here, along with several other methods.

    For step 2, your choices for .NET are primarily between Noda Time, or TimeZoneInfo. Martin gave several good examples of each in his answer.

    Also, consider that scheduling is a more difficult scenario than most. You should read this post for more details.