6

We are downloading data from our trading partner's API. Till now we were working with "E. Australia Standard Time" and it was working fine.

After daylight saving started our trading partner said that they are working with "Australian Eastern Daylight Time (AEDT)".

I have used following code to convert from UTC to "E. Australia Standard Time".

DateTime utcTime = DateTime.UtcNow;
TimeZoneInfo objTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("E. Australia Standard Time");
DateTime TPDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, objTimeZoneInfo);

But I am getting following errors when I use "E. Australia Daylight Time" or "Australian Eastern Daylight Time (AEDT)"

The time zone ID 'E. Australia Daylight Time' was not found on the local computer.
The time zone ID 'Australian Eastern Daylight Time (AEDT)' was not found on the local computer.

What timezone id should I pass to FindSystemTimeZoneById() method to convert correctly to Australian Eastern Daylight Time (AEDT)?

Yash
  • 356
  • 1
  • 5
  • 22
  • 2
    Well, your first one has a typo. Daylingh? I don't know if it will work any better correct, of course. – ProgrammingLlama Nov 22 '18 at 15:35
  • 3
    You can't use a non-existent timezone. Timezones do **not** refer to winter or summer time. The *same* timezone has rules that controll its *offset* during winter or summer. – Panagiotis Kanavos Nov 22 '18 at 15:39
  • 1
    Why the question? Are you getting the wrong time with `"E. Australia Standard Time"` ? – Panagiotis Kanavos Nov 22 '18 at 15:41
  • @Panagiotis I imagine it's something to do with some places in Australia not doing daylight saving (Brisbane, for example) and others doing it (Melbourne). OP you may want to extract [this list](https://stackoverflow.com/a/7908482/3181933) to find the correct name. – ProgrammingLlama Nov 22 '18 at 15:42
  • @PanagiotisKanavos "E. Australia Standard Time" is working fine. But our trading partner is in "Australian Eastern Daylight Time (AEDT)" which is (UTC+11:00) So we have to use that. – Yash Nov 22 '18 at 15:43
  • @John that's part of the timezone rules as well. – Panagiotis Kanavos Nov 22 '18 at 15:44
  • @Yash no you don't. There's no such **timezone**. The timezone is `E. Australia Standard Time`. During summer it has one offset, during winter it has another. When converting from/to UTC .NET uses those timezone rules to check the actual date and determine which offset to use for the conversion. Write a simple test and pass two different dates, one in the summer, one in the winter with the *same* time. Check what the time looks like *after* the conversion – Panagiotis Kanavos Nov 22 '18 at 15:44
  • @PanagiotisKanavos this timezone is there. please check following link: https://www.australia.gov.au/about-australia/facts-and-figures/time-zones-and-daylight-saving and scroll till last section "Daylight saving" – Yash Nov 22 '18 at 15:46
  • 1
    @Yash that's not a Windows timezone. You are confusing different things. The list of Windows timezone names [is here](https://stackoverflow.com/questions/7908343/list-of-timezone-ids-for-use-with-findtimezonebyid-in-c) – Panagiotis Kanavos Nov 22 '18 at 15:47
  • @Panagiotis I meant that OP is probably using the wrong timezone name for the location because the rules seem somewhat complicated (in winter all of the Australia Eastern places use Standard, but then in summer, Brisbane retains the Standard time whereas other places switch to daylight saving). – ProgrammingLlama Nov 22 '18 at 15:48
  • 2
    @Yash btw if you really care about timezones use the NodaTime library and the [IANA timezone names](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones). `Australia/Melbourne` is a lot clearer than Windows timezone names. The IANA timezone database and names have become the de-facto standard – Panagiotis Kanavos Nov 22 '18 at 15:49
  • 2
    Follow the accepted answer of the below link: [TimeZoneInfo.ConvertTime from PST to UTC to AEST](https://stackoverflow.com/questions/40644722/timezoneinfo-converttime-from-pst-to-utc-to-aest-off-by-one-hour) – Ali Azam Nov 22 '18 at 15:51

1 Answers1

17

You probably want to use "AUS Eastern Standard Time" (for Canberra, Hobart, Melbourne and Sydney). Despite having the word "Standard" in the name, this accounts for daylight savings time and uses UTC+10 in winter and UTC+11 in summer:

var tz = TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time");

TimeZoneInfo.ConvertTimeFromUtc(new DateTime(2018,1,1,0,0,0,DateTimeKind.Utc), tz); 
    // => 01/01/2018 11:00:00
TimeZoneInfo.ConvertTimeFromUtc(new DateTime(2018,7,1,0,0,0,DateTimeKind.Utc), tz); 
    // => 01/07/2018 10:00:00

The "E. Australia Standard Time" time zone is for Brisbane, where they do not observe daylight savings time.

You can get a complete list of available time zones using the TimeZoneInfo.GetSystemTimeZones() method, or by running tzutil /l at the command line.

Phil Ross
  • 25,590
  • 9
  • 67
  • 77
  • 2
    That's a great example why one should use NodaTime and IANA timezone names! – Panagiotis Kanavos Nov 22 '18 at 15:50
  • Thank you so much, I think I got my answer. I have just one more question. Once day light saving is completed.. Will "AUS Eastern Standard Time" give me the standard time difference (UTC+10:00)? – Yash Nov 22 '18 at 15:51
  • @Yash As Panagiotis said, the rules are built into the TimeZoneInfo (check the object's properties). – ProgrammingLlama Nov 22 '18 at 15:56
  • 2
    @Yash Yes. The time zone includes the rules that define when the clocks are set forward and back. It will use the appropriate standard or daylight savings offset for the date and time you are converting. – Phil Ross Nov 22 '18 at 15:57