4

I am calling a service which standardizes a given address and also gives timezone of the result in UTC offset (ex: -5:00 etc).

Is there a function in C# which takes in timezoneoffset of UTC and calculate timezone(Three characters such as CST, EST, PST) ?

Sat
  • 145
  • 1
  • 1
  • 14
  • 4
    Take care - it is not possible to convert an offset into a timezone - some timezones overlap offsets and the offsets are not consistent with daylight savings. – BillyBigPotatoes Mar 03 '14 at 23:03

2 Answers2

7

No. That's an impossible request in any language, because:

  1. Many different time zones share the same offset.

  2. There is no standard for time zone abbreviations. There is a lot of overlap and ambiguity. For example, "CST" has at least 5 different meanings that I am aware of.

  3. Even if you limit yourself to the United States, you cannot reliably distinguish between offsets. For example, -5 could be EST or it could be CDT.

    There is even one hour a year where EST and CDT are BOTH in effect at the exact same time. For example, this occurred on November 2, 2014, when 1:00 AM EST and 1:00 AM CDT were both in effect simultaneously, and both used the UTC-5 offset.

I am calling a service which standardizes a given address and also gives timezone of the result in UTC offset (ex: -5:00 etc).

Unless the service is returning the offset for a specific date and time, then it could be lying to you. Perhaps it is returning the current offset, but that is not fixed. The same location can cycle through multiple different offsets.

IMO, Services like that should not attempt to resolve an offset, but should instead return a time zone identifier, such as America/New_York.

You should probably read the timezone tag wiki, and you may also be interested in this post.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • You are correct there's no official list of abbreviations. However, TimeZoneInfo.GetSystemTimeZones will give you a list of timezones from the windows registry. TimeZoneInfo don't have abbreviations as specified in the question however they do have a DisplayName and a StandardName – Mick Mar 04 '14 at 04:19
  • @Mick. Yes, and there are lots of other questions on StackOverflow about that. But I don't see how it pertains to the question that was asked. – Matt Johnson-Pint Mar 04 '14 at 04:33
  • @ Matt, I have contacted our address service providers and according to them, the offset is calculated at the given time. This service we use is limited to united states. We have a DB table where we maintain day light savings for each year. Please let me know if there is anything that can help me for calculating timezones by limiting the outcomes to US. – Sat Mar 04 '14 at 15:12
  • 1
    As I said in #3 in my answer, it's still not reliable. Consider that if you get a result of `-05:00` and it happens to be November 2, 2014 in the hour from 1:00 AM through 1:59:59 AM. In that case, `-05:00` could be *either* EST or CDT. [See here for confirmation](http://www.timeanddate.com/worldclock/converted.html?iso=20141102T01&p1=64&p2=179). This happens because in the USA, DST does not happen all at once. Each time zone makes the switch in their own local time, which creates an hour where adjacent time zones share the same time, and an hour where they are 2 hours apart instead of 1. – Matt Johnson-Pint Mar 04 '14 at 16:51
  • 2
    Also consider that the Pacific time zone uses `-07:00` during daylight saving time, but most of Arizona (in the Mountain time zone) doesn't use daylight saving time and has an offset of `-07:00` all year. So if you get a value of `-07:00` from your service during the summer, then it could mean PDT or MST. – Matt Johnson-Pint Mar 04 '14 at 16:56
  • @Matt it becomes relevant if he can use the DisplayName or StandardName instead of these abbreviations – Mick Mar 05 '14 at 03:40
  • @Mick, you still would not be able to reliably obtain the correct `TimeZoneInfo` object from an offset alone, or even an offset + timestamp. – Matt Johnson-Pint Mar 05 '14 at 03:47
  • @Matt, no you would get a collection of them. It sounds like the app is only interested in a subset of these so you could then probably reduce those to a single timezoneinfo and an ID. Still it's better than nothing. – Mick Mar 05 '14 at 23:29
-1

If there is any chance of doing this in C#, I would think NODA Time would do it.

Brad Bruce
  • 7,638
  • 3
  • 39
  • 60