0

I receive a strange result when I try to find a time zone for "GMT Standard Time": enter image description here It returns UTC +00:00 when I expect to receive +01:00. The next weird thing is when I convert UTC time to "GMT", then 1 hour is added to the time.

This is an example of the code:

var gmtTimeZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
var timeUtc = DateTime.UtcNow;
var gmtTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, gmtTimeZone);

Am I doing something wrong? How to get the correct display name? I need to allow the user to select a timezone and it's important to show the correct name.

Sheinar
  • 362
  • 3
  • 14
  • *"It returns UTC +00:00 when I expect to receive +01:00"* Why should it be +01? – Cid Aug 18 '22 at 10:01
  • [Is this relevant?](https://stackoverflow.com/questions/2292334/difference-between-utc-and-gmt-standard-time-in-net). Note that GMT does NOT include DST adjustments. – Matthew Watson Aug 18 '22 at 10:02
  • How are you getting the time? A DataTime object is passed between computer (or databases) using UTC so no conversions are required. Issue occur when you convert string to DataTime. When conversion from string to DateTime the PC time zone settings are used for the conversion unless the string contains a timezone. When the string contains a time zone the time zone is used in string instead of the PC time zone. – jdweng Aug 18 '22 at 10:18
  • 1
    @MatthewWatson: Unfortunately the time zone ID of "GMT Standard Time" is very misleading. It really means the equivalent of the IANA Europe/London time zone, so I'd *expect* it to include savings. – Jon Skeet Aug 18 '22 at 10:37
  • "It returns UTC +00:00 when I expect to receive +01:00." - *what* returns UTC+00? The `TimeZoneInfo` itself isn't just a UTC offset. If you mean the `BaseUtcOffset` property, it's entirely correct for that to be UTC+0. Please clarify what you're actually worried about. – Jon Skeet Aug 18 '22 at 10:38

1 Answers1

1

The issue is that this is a TimeZoneInfo conversion.

From .NET 6.0 TimeZoneInfo.ConvertTimeFromUtc description

When performing the conversion, the ConvertTimeFromUtc method applies any adjustment rules in effect in the destinationTimeZone time zone.

Thus as, at the time of posting, GMT Standard Time as used by the United Kingdom is adjusted by the Daylight saving time of 1 hour.

Windows Default Time Zones shows that "GMT Standard Time" applies to the United Kingdom only.

This can be verified as follows:

var gmtTimeZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
var timeUtc = DateTime.UtcNow;
var gmtTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, gmtTimeZone);
var daylightSaving = gmtTime.IsDaylightSavingTime() ? "Yes" : "No";

Console.WriteLine($"UTC time : {timeUtc}");
Console.WriteLine($"GMT timezone time : {gmtTime}");
Console.WriteLine($"GMT timezone daylight saving? : { daylightSaving}");

gives:

UTC time : 18/08/2022 10:27:32
GMT timezone time : 18/08/2022 11:27:32
GMT timezone daylight saving? : Yes

If you wish to just use local time (as set on the user's system) then you can just use DateTime.ToLocalTime() e.g.

Console.WriteLine($"utc from gmtTime : {utc}");
Console.WriteLine($"utc to local : {utc.ToLocalTime()}");

gives:

utc from gmtTime : 18/08/2022 10:38:36
utc to local : 18/08/2022 11:38:36

To remove ambiguities when selecting a time zone I would recommend that you use the ISO3166 code e.g. "GB" instead of "GMT Standard Time".

ChrisBD
  • 9,104
  • 3
  • 22
  • 35