TimeZoneInfo.ConvertTimeFromUtc is not converting old dates to then daylight saving time

Vikram V

I'm using TimeZoneInfo.ConvertTimeFromUtc method to convert date time to the target date time. Current its UTC+10 in Sydney. If i run the code its perfectly converting time to UTC+10. But in Jan with out daylight saving It was UTC+11. If I input a Jan date run the code. Its still converting it as UTC+10 instead of UTC+11.

Below is the sample code.

      TimeZoneInfo ESTZone = TimeZoneInfo.FindSystemTimeZoneById("E. Australia Standard Time");
      DateTime date = Convert.ToDateTime("2019-01-23 13:15:23.6090752");
      DateTime SydneyDateTime = TimeZoneInfo.ConvertTimeFromUtc(date, TimeZoneInfo.FindSystemTimeZoneById(Timezone));
      string EST = SydneyDateTime.ToString() + " / " + SydneyDateTime.IsDaylightSavingTime().ToString() + " / " + ESTZone.IsDaylightSavingTime(SydneyDateTime)+" / " + ESTZone.DaylightName + " / " + ESTZone.StandardName;
      DateTime IndiaDateTime = TimeZoneInfo.ConvertTimeFromUtc(date, TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"));


      DateTime date2 = Convert.ToDateTime("2019-05-23 13:15:23.6090752");
      DateTime SydneyDateTime2 = TimeZoneInfo.ConvertTimeFromUtc(date2, TimeZoneInfo.FindSystemTimeZoneById(Timezone));
      string EST2 = SydneyDateTime2.ToString() + " / " + SydneyDateTime2.IsDaylightSavingTime().ToString() + " / " + ESTZone.IsDaylightSavingTime(SydneyDateTime2) + " / " + ESTZone.DaylightName + " / " + ESTZone.StandardName;

Output Result:

EST = "23-01-2019 23:15:23 / False / False / E. Australia Summer Time / E. Australia Standard Time"

EST2="23-05-2019 23:15:23 / False / False / E. Australia Summer Time / E. Australia Standard Time"

In both the cases, its adding 10 hrs, but in January it should add 11 hrs.

Am I missing anything in the code.

Ian

You're using the wrong timezone. "E. Australia Standard Time" corresponds to Brisbane time, and that timezone does not observe DST so your winter and summer times come back with the same UTC offset.

For Sydney, you need to use "AUS Eastern Standard Time". ConvertTimeFromUtc will apply the UTC offset as appropriate for that timezone and give you the times you're expecting.

Here is your snippet including the use of the appropriate time zone, and a bit of editing around the Timezone initialisation so it compiled:

    public void ConvertTimes()
    {
        string Timezone = "AUS Eastern Standard Time";

        TimeZoneInfo ESTZone = TimeZoneInfo.FindSystemTimeZoneById(Timezone);
        DateTime date = Convert.ToDateTime("2019-01-23 13:15:23.6090752");
        DateTime SydneyDateTime = TimeZoneInfo.ConvertTimeFromUtc(date, TimeZoneInfo.FindSystemTimeZoneById(Timezone));
        string EST = SydneyDateTime.ToString() + " / " + SydneyDateTime.IsDaylightSavingTime().ToString() + " / " + ESTZone.IsDaylightSavingTime(SydneyDateTime) + " / " + ESTZone.DaylightName + " / " + ESTZone.StandardName;
        DateTime IndiaDateTime = TimeZoneInfo.ConvertTimeFromUtc(date, TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"));


        DateTime date2 = Convert.ToDateTime("2019-05-23 13:15:23.6090752");
        DateTime SydneyDateTime2 = TimeZoneInfo.ConvertTimeFromUtc(date2, TimeZoneInfo.FindSystemTimeZoneById(Timezone));
        string EST2 = SydneyDateTime2.ToString() + " / " + SydneyDateTime2.IsDaylightSavingTime().ToString() + " / " + ESTZone.IsDaylightSavingTime(SydneyDateTime2) + " / " + ESTZone.DaylightName + " / " + ESTZone.StandardName;
    }

This gives the output:

EST = "24/01/2019 00:15:23 / False / True / AUS Eastern Daylight Time / AUS Eastern Standard Time"

EST2 = "23/05/2019 23:15:23 / True / False / AUS Eastern Daylight Time / AUS Eastern Standard Time"

which I think is what you were expecting.

Incidentally, DateTime.IsDaylightSavingTime() uses your local timezone to determine whether the time is within DST. It might be misleading looking at it here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Daylight Saving Time not working in TimeZoneInfo, when converting from UTC to Local

daylight saving time issue on converting date through epoch time in sqlite

Taking care of daylight saving time when converting timezone

Get Daylight Saving Transition Dates For Time Zones in Java

Javascript - subtract two dates in milliseconds considering daylight time saving

Daylight Saving Time - Java

Disable daylight saving time

Detect daylight saving time in bash

A Need To Check For Daylight Saving Time

@DateTimeFormat with daylight saving error when converting

parse time zone and convert to daylight saving time

Handling of switch to daylight saving time with POSIXct in R

Issue with end of month date in DAYLIGHT SAVING TIME

How does Daylight Saving Time work on a computer?

How to set daylight saving time in MySQL?

Confusing test fail - daylight saving time

Daylight Saving Time change with an absolute date

Avoid Daylight Saving Time in calculating weeks in between

momentjs daylight saving time after 2038

Is SimpleDateFormat parse - Daylight Saving Time Aware?

daylight saving time influences the result of mktime

Jboss Java Date daylight saving time

Spring @DateTimeFormat conversion error with Daylight Saving Time

Daylight Saving time from DB2

Determine daylight saving time for US ZoneIds in Java

How to check Daylight Saving Time with Temporal in Javascript?

Check if Daylight Saving Time (DST) is in effect with Julia

How to detect Daylight Saving Time transition in android

Managing Time zone and Daylight Saving in SQL Server