0

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

Kieran
  • 612
  • 6
  • 22
  • 3
    `ConvertTime` returns a new instance. It does not mutate the original. => https://dotnetfiddle.net/D3kg2S – Fildor Jul 07 '22 at 16:11
  • Ah, that makes sense thank you. But how comes the output is 09 instead of 07? – Kieran Jul 07 '22 at 16:16
  • I honestly don't know. That's exactly that kind of stuff, why I personally always use `DateTimeOffset` => https://dotnetfiddle.net/OT3eip – Fildor Jul 07 '22 at 16:18
  • what would you do if you didn't know what the offset should be, you just knew it should be a London time dynamically? – Kieran Jul 07 '22 at 16:20
  • Let me see ... give me a minute ... – Fildor Jul 07 '22 at 16:21
  • Hmmm, interesting. Nothing seems to work properly in dotnetfiddle ... – Fildor Jul 07 '22 at 16:46
  • 1
    What I did manage to do was this: https://dotnetfiddle.net/SbERgr It's a bit brute force, but seems to work. – Fildor Jul 07 '22 at 17:00

1 Answers1

1

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
Fildor
  • 14,510
  • 4
  • 35
  • 67