3

I'm rebuilding a site originally written in PHP into ASP.NET. The original application was storing a user's time zone offset for displaying times in their local time. I want to convert this from the numerical offset to the time zone name, if possible, in order to be more accurate.

My initial findings suggest this isn't possible directly. So I'm thinking of sort of looping through, finding the first one that has the same offset and use that. Is there a simpler/more elegant way of doing it that I'm not aware of?

Thanks in advance!

Just for reference, the PHP version database column looks like this (where 0.00 would be equivalent to UTC):

Time_Zone
---------
17.00
10.00
-6
-7.00
12.00 
0.00
-6.00
-8.00
ahwm
  • 672
  • 9
  • 23
  • What is that data? Hours offset from UTC? – Rick S Jun 28 '17 at 19:29
  • Yes, that's hours offset. – ahwm Jun 28 '17 at 19:29
  • Are you just needing to display the time to the user based on that offset? Or do you need to know which time zone it is? Couldn't you just convert your date/time to UTC and then add the offset? – Rick S Jun 28 '17 at 19:30
  • @RickS if so OP could use `AddHours`? `DateTime.Now.AddHours(xx)`, where `xx` is that offset converted to a `double` – VDWWD Jun 28 '17 at 19:30
  • @VDWWD I considered that as an option. But I wanted to use .NET's Time Zone list for the user to select from. The original site just had a text box where the user could type in `-7` or whatever. Daylight savings would not necessarily be accurate if I stuck with this option. – ahwm Jun 28 '17 at 19:33
  • Take a look at [this](https://stackoverflow.com/questions/832986/how-to-work-with-time-zones-in-asp-net). It might help. – VDWWD Jun 28 '17 at 19:34
  • That's actually exactly what I was planning on doing. I just wanted to see if there was a simpler method than looping through the Windows time zones and comparing their offset to the one in the old database. It's not looking promising, however. – ahwm Jun 28 '17 at 19:38
  • I also want to add that I've worked with time zone differences in other projects I've worked on, but that was us building from scratch and not having to worry about historical data like this. – ahwm Jun 28 '17 at 19:39

0 Answers0