0
votes

I am using the TimeZoneInfo class to convert a Datetime value to UTC. However, the conversion does not take care of Daylight Savings Time automatically. How can I achieve that.

For instance, I am trying to convert the following DateTime to UTC:

//meetingDate is "10/04/2019 12:00:00" PM

var timeZoneId="AUS Eastern Standard Time";

DateTime dtUTC = TimeZoneInfo.ConvertTime(meetingDate, TimeZoneInfo.FindSystemTimeZoneById(timeZoneId), TimeZoneInfo.Utc);

the value returned is: 10/04/2019 02:00:00 UTC (am)

And this is because the Time zone in my operative system for "AUS Eastern Standard Time" is UTC + 10:00 (See image below) But because the "Adjust for daylight saving time Automatically" Setting is enabled , I am able to see the right time on my windows 10 (which is actually at this specific moment (1st April 2019) UTC + 11)

How can i get the same behaviour using TimeZoneInfo.ConvertTime method.

That if i pass the value "10/04/2019 12:00:00 PM" I should get back: 10/04/2019 01:00:00 am UTC

P.S: I have checked out the following stack overflow question but it is converting from UTC, So it does not apply to my question.

How to use TimeZoneInfo to get local time during Daylight Savings Time?

Image Windows 10 Timezone:

Image Windows 10 Timezone

1
Duplicate where?D.B
@Ashkan Mobayen Khiabani don't see any question that relate to what i am specifically looking forD.B

1 Answers

3
votes

The code is already doing the right thing and adjusting for daylight savings. "AUS Eastern Standard Time" is the Windows equivalent of the IANA Australia/Sydney zone. The transitions for Australia/Sydney are shown in my Noda Time site, and they indicate that there's a transition on April 6th 4pm UTC (so April 7th 3am local time) from UTC+11 to UTC+10.

That means by April 10th - the date of the local time you're asking to be converted - the offset is indeed UTC+10, and 10/04/2019 02:00:00 UTC (am) is the correct result. If it gave a result of 1am (so subtracting a UTC offset of +11) that would be incorrect because that wouldn't be adjusting from the daylight saving transition on April 6th.