0

I have a timezoneinfo Central Europe Standard Time, which is showing UTC +01:00, when i execute the code it shows 2 hrs before time instead of 1 hr. Below is the sample code

static void Main(string[] args)
    {
        var strTimeZoneInfo = "Central Europe Standard Time";
        var datetimeDST = Convert.ToDateTime("2019-07-18 18:17:00");
        var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(strTimeZoneInfo);
        var dtUTC= TimeZoneInfo.ConvertTimeToUtc(datetimeDST, timeZoneInfo);
        Console.WriteLine(Convert.ToString(dtUTC));
        Console.ReadKey();
    }

So instead of 2019-07-18 17:17:00 i am getting 2019-07-18 16:17:00. Can someone explain how to resolve this without using Noda or other external library.

Referred Below links but not clear how to resolve this problem.

Also referred this link's answer but it's not working.

Nae
  • 14,209
  • 7
  • 52
  • 79
SidD
  • 5,697
  • 4
  • 18
  • 30
  • 2
    What do you mean by "which is showing UTC +01:00"? The time zone with ID of "Central Europe Standard Time" has an offset of UTC+2 on July 18th 2019 - ignore the confusing "standard time" part of the ID; it just means "central Europe" (e.g. Paris, Berlin). I'd *expect* it to convert to 16:17:00. That's all in the first link you included though, so I'm not sure I understand what you're asking. – Jon Skeet Jul 22 '19 at 22:19
  • Thanks for replying @JonSkeet, i followed your answers too, by "which is showing UTC +01:00" i mean when i debug timezone info shows (UTC+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague, so since its UTC +1 is should result in 2019-07-18 17:17:00 and not 2019-07-18 16:17:00. – SidD Jul 22 '19 at 22:26
  • 5
    No, that's just showing that the standard offset of the time zone is UTC+1. It doesn't mean that it's UTC+1 *all the time*. Right now, it's UTC+2 in all those places. `TimeZoneInfo` is doing the right thing, it's just your expectations that were incorrect. – Jon Skeet Jul 22 '19 at 22:37
  • https://en.wikipedia.org/wiki/Daylight_saving_time – mjwills Jul 22 '19 at 22:46
  • Thanks for the answers, its just the display name UTC+1 was creating the problem. – SidD Jul 23 '19 at 08:59

1 Answers1

3

Even though "Central Europe Standard Time" is the Id property for that TimeZoneInfo, but it actually handles both Standard and Daylight time within that time zone depending on the date you're talking about. I understand it is confusing because that TimeZoneInfo includes "UTC+01:00" in the Display Name, even though it's currently in Daylight Savings Time.

But in this case, the date you're giving it is during a Daylight Savings Time period, so the offset should be +02:00, not +01:00. The library is producing the correct results.

var strTimeZoneInfo = "Central Europe Standard Time";
var datetimeDST = Convert.ToDateTime("2019-07-18 18:17:00");
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(strTimeZoneInfo);
Console.WriteLine(timeZoneInfo.DisplayName);
Console.WriteLine(timeZoneInfo.StandardName);
Console.WriteLine(timeZoneInfo.DaylightName);
Console.WriteLine(timeZoneInfo.IsDaylightSavingTime(datetimeDST));
Console.WriteLine(timeZoneInfo.GetUtcOffset(datetimeDST));
var dtUTC = TimeZoneInfo.ConvertTimeToUtc(datetimeDST, timeZoneInfo);
Console.WriteLine(dtUTC.ToString(CultureInfo.InvariantCulture));

Output:

(UTC+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague
Central Europe Standard Time
Central Europe Daylight Time
True
02:00:00
07/18/2019 16:17:00

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315