.NET: daylight saving time accounting

I have a method that creates UTC DateTime user input using the GMT offset by their geographic location:

 public static DateTime LocalToUtc (int year, int month, int day, int hour, decimal gmtOffset) { // argument validation here var dateTime = new DateTime(year, month, day).AddHours(hour); var dateTimeOffset = new DateTimeOffset(dateTime, TimeSpan.FromHours(gmtOffset)); return dateTimeOffset.UtcDateTime; } 

The problem is that this feature is disabled for an hour if it saves daylight in the user’s time zone.

Thus, although my personal GMT offset is -8, the current time zone offset is -7 due to daylight saving time.

How to change the function above to take into account daylight saving time? Do I need to somehow create any time zone object from the GMT offset and get its timezone offset?

+4
source share
4 answers

There is no way to do this without knowing the actual time zone: multiple time zones have the same basic UTC offset, but with different rules for daylight saving time. For example, W. Europe Standard and W. Central Africa Standard Time have an offset of +01: 00, but the former supports DST and the latter does not. Therefore, the bias is not enough to decide whether DST is applied or not ...

Your method should accept the TimeZoneInfo parameter instead of gmtOffset . Thus, you can simply use the TimeZoneInfo.ConvertTime method to convert the date, it automatically considers DST.

+2
source

DateTime already has methods for this: ToLocalTime() and ToUniversalTime() . What is wrong with that?

EDIT:

Based on the comment that the author wants to convert to utc from a time zone other than the current time zone of computers, I refer you to John Skets’ answer here

From the MSDN documentation :

 string displayName = "(GMT+06:00) Antarctica/Mawson Time"; string standardName = "Mawson Time"; TimeSpan offset = new TimeSpan(06, 00, 00); TimeZoneInfo mawson = TimeZoneInfo.CreateCustomTimeZone(standardName, offset, displayName, standardName); Console.WriteLine("The current time is {0} {1}", TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, mawson), mawson.StandardName); 
+1
source

You should use the TimeZone.GetUtcOffset method if you do not want to use the built-in UTC method.

http://msdn.microsoft.com/en-us/library/system.timezone.getutcoffset.aspx

This will give you your offset to UTC. You can also use built-in methods to get UTC time from local times.

http://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime(v=VS.100).aspx

0
source

Since it seems you are asking users for a time offset, which may not necessarily be in the local time zone of the machine, the time of the local machine will not work. But using the TimeZoneInfo class, you should be able to create an instance that takes into account the offset status and DST, and use the built-in methods from there.

Edit: On my machine, at least TimeZoneInfo.GetSystemTimeZones() seems to return all valid time zones. This can be easily displayed in the drop-down menu so that the user can select.

0
source

All Articles