0

I have an ASP.NET web app that I'm displaying event DateTimeOffsets with.

I have a DateTimeOffset from the database, and I can display it, but I want to also display the timezone.

DateTime eventOccurance = getDateTimeFromDB();
Console.out.WriteLine(eventOccurance.ToString());

The output would be

6/11/2015 1:02:03 AM -07:00

What I want is to also add in what timezone it is base on the offset. So for this example the offset of -7 hours indicates that it is Mountain Standard Time.

6/11/2015 1:02:03 AM -07:00 (MST)

I can't find any way to do this. The service supports only the United States (it is emergency services related), so I could write a function that figures what time zone you're in myself, but I'm looking for a better standard way to do it.

I was hoping there was a function in TimeZone or TimeZoneInfo that would take as a parameter an offset, and return an appropriate TimeZone, but from what I can tell, that does not exist.

edit

Commenter expressed potential similarity to How to display DateTime with an abbreviated Time Zone?

It is similar to that post, but not identical, because he is using the current time zone, and trying to abbreviate it. I care less about abbreviating, and more about figuring out what time zone a DateTimeOffset is actually in.

edit 2

Thanks Mark. Sorry I couldn't find that on my own. I assure you, I looked.

(...)timezoneoffset of UTC and calculate timezone in C#?

Turns out the problem here is that the offset to TimeZone relationship is a one-to-many relationship, and therefore there is no functionality for what I'm trying to accomplish, other than just writing it myself.

Community
  • 1
  • 1
Dan
  • 660
  • 6
  • 15
  • If there were such a function, you'd have to pass the whole DateTimeOffset, since which time zone has which offset depends on the date (whether it's in ST or DT) and the time (< 02:00, etc.). It would also need the state, since some states don't follow ST or DT. So, yeah, not really practical. If you want to keep the information about the original time zone, you'll have to store it with the DateTime. – Heretic Monkey Jun 16 '15 at 19:03
  • It is similar to that post, but not identical, because he is using the current time zone, and trying to abbreviate it. I care less about abbreviating, and more about figuring out what time zone a datetimeoffset is actually in. I do also have GPS location data with each of these, but I don't really want to have to use that either. Your point of the Whole DateTimeOffset being required since time zone may change with date is well taken however. And I know some states don't follow DST (like my state, AZ). We determined it's ok to just put Mountain or Pacific time in those cases. – Dan Jun 16 '15 at 19:24
  • It's unlikely you'll be able to turn a time zone offset into a time zone abbreviation. Offsets aren't mapped 1-1 with time zones. Since you're restricted to the US, your best bet might be to just build a dictionary with US time zones. Even then you have some complications with DST to deal with. For example: Is the date during Daylight Saving? How would you know without knowing the time zone? – Andrew Whitaker Jun 16 '15 at 20:25

0 Answers0