Home > Software engineering >  How to properly use London time regardless of BST/GMT
How to properly use London time regardless of BST/GMT

Time:07-12

I have this method:

public static void Main()
{
    var gmtTime = new DateTime(2013, 08, 15, 08, 53, 22);

    TimeZoneInfo.ConvertTime(gmtTime, TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"));
    
    var b = gmtTime.ToUniversalTime();
    
    Console.WriteLine(b);
}

on the 15th August 2013 the UK was in BST (UTC 1) so I was hoping the date would be changed to 07:53 because that's what the UTC would have been. This is working when run locally. But when run in https://dotnetfiddle.net/ or azure devops pipeline using "Azure Pipelines" (set to UTC) it fails.

According to erics answer here: C# british summer time (BST) timezone abbreviation

GMT standard time should be BST in the summer and GMT at other times; which is what I was hoping for

CodePudding user response:

To put the comment into an answer:

I tried this in dotnetfiddle...

using System;
                    
public class Program
{
    public static void Main()
    {
        var gmtTime = new DateTime(2013, 08, 15, 08, 53, 22);

        Console.WriteLine("{0} - {1} - {2}",gmtTime, gmtTime.IsDaylightSavingTime(), gmtTime.Kind);
        
        var tz = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
        Console.WriteLine("{0} is {1}at that date {2}", tz.DisplayName, tz.IsDaylightSavingTime(gmtTime)?"":"not ", tz.DaylightName);
        Console.WriteLine("Brute Force: {0} - {1} => {2}", gmtTime, tz.GetUtcOffset(gmtTime), gmtTime - tz.GetUtcOffset(gmtTime));
        
        Console.WriteLine();
        
        gmtTime = new DateTime(2013, 12, 15, 08, 53, 22);
        Console.WriteLine("{0} - {1} - {2}",gmtTime, gmtTime.IsDaylightSavingTime(), gmtTime.Kind);
        Console.WriteLine("{0} is {1}at that date {2}", tz.DisplayName, tz.IsDaylightSavingTime(gmtTime)?"":"not ", tz.DaylightName);
        Console.WriteLine("Brute Force: {0} - {1} => {2}", gmtTime, tz.GetUtcOffset(gmtTime), gmtTime - tz.GetUtcOffset(gmtTime));
    }
}

which produces the output:

08/15/2013 08:53:22 - False - Unspecified
(UTC 00:00) United Kingdom Time is at that date British Summer Time
Brute Force: 08/15/2013 08:53:22 - 01:00:00 => 08/15/2013 07:53:22

12/15/2013 08:53:22 - False - Unspecified
(UTC 00:00) United Kingdom Time is not at that date British Summer Time
Brute Force: 12/15/2013 08:53:22 - 00:00:00 => 12/15/2013 08:53:22
  • Related