2

Does the TimeZoneInfo.ConvertTimeFromUtc method read the daylight saving settings from the registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zone to calculate the local time?

If so, does it work for only the current time and not for any past years or dates? I would like to know if I can convert a past date time in 'Eastern Standard Time' like '2-1-2010 3:00 PM' to GMT or UTC which follows the DLS (Day Light Saving) for the year 2010? Take another year like '2-1-2006 3:00 PM' as an example also. As you read here, the DST has changed in 2007 for the US.

May I ask, what is the best solution if I have to display an appointment time from a SQL database in two different locals like Malaysia and New York (USA Eastern Standard Time) for a visitor in the past 5 years?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
wonderful world
  • 10,969
  • 20
  • 97
  • 194

2 Answers2

3

Does the TimeZoneInfo.ConvertTimeFromUtc method read the daylight saving settings from the registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zone to calculate the local time?

Yes, except the last key name is Time Zones (you dropped an s). You can read about how this works in this MSDN blog article.

If so, does it work for only the current time and not for any past years or dates?

It will work for any past years that exist in the Windows registry data. You can examine each subkey in the registry to see how far back you can convert for each zone. However, there are various errors and omissions within the data. In general, the historical accuracy of this data is not near as good as other sources such as the IANA/Olson time zone database, which you can use via Noda Time. Read more in the timezone tag wiki.

I would like to know if I can convert a past date time in 'Eastern Standard Time' like '2-1-2010 3:00 PM' to GMT or UTC which follows the DLS (Day Light Saving) for the year 2010? Take another year like '2-1-2006 3:00 PM' as an example also. As you read here, the DST has changed in 2007 for the US.

  • The term is "daylight saving time" - no capital letters, one word for daylight, abbreviated DST

  • Yes, Windows time zone data, and TimeZoneInfo take DST into account.

  • To convert to UTC, you use ConvertTimeToUtc, not ConvertTimeFromUtc.

  • Yes, Windows knows about the 2007 DST change in the US. It does not however, know about earlier changes, such as values before the Uniform Time Act went into effect in 1987. For that, you need the IANA/Olson database.

May I ask, what is the best solution if I have to display an appointment time from a SQL database in two different locals like Malaysia and New York (USA Eastern Standard Time) for a visitor in the past 5 years?

  • Using TimeZoneInfo use the ID "Eastern Standard Time" ID for New York, and the "Singapore Standard Time" ID for Malaysia.

  • Using IANA/Olson time zones with Noda Time, use "America/New_York", and "Asia/Kuala_Lumpur".

  • Either option is fine for the last 5 years you asked about. Malaysia's last time zone change was in 1982.

  • For past events, store UTC times in your database, or store DateTimeOffset values with the correct time and offset for the time zone of the event (See DateTime vs DateTimeOffset). Convert from UTC to the viewer's time zone as needed for display.

  • For scheduling future events, store the local time of the event in your database, and follow the guidance I've written here.

You will also find extended guidance on this topic in my Pluralsight course, Date and Time Fundamentals. Specifically, you should consider watching the sections titled, "Time Zones", "Date and Time in the .NET Framework", and "Introducing Noda Time".

Phil Ross
  • 25,590
  • 9
  • 67
  • 77
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
1

In my experience ConvertTimeFromUtc works fine for past dates.

Best solution for diplaying dates in different locales:

  • store the dates as UTC (you may have to run a script to convert existing entires)
  • use ConvertTimeFromUtc on the relevant timezone when displaying. (if it doesnt work, try changing DateTime.DateTimeKind on the date that is returned from the DB)

(I'm not sure about the US changes on daylight saving, which is normally calculated using a fixed formula.)

la-yumba
  • 81
  • 1
  • 4