0

I create a local DateTime where the offset should be 00:01:00 (GMT Standard Time). However when converting to UTC it merely specifies the kind as UTC, and doesn't actually convert it.

    [Test]
    public void GivenLocalDateTime_WhenConverted_ThenBecomesUtc()
    {
        // Arrange
        var local = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Local);
        var expected = new DateTime(2019, 12, 31, 23, 0, 0, DateTimeKind.Utc);

        // Act
        var result = local.ToUniversalTime();

        // Assert
        Assert.AreEqual(DateTimeOffset.Now.Offset, TimeSpan.FromHours(1)); // Passes
        Assert.AreEqual(expected.Kind, result.Kind); // Passes
        Assert.AreEqual(expected, result); // Fails

        /*
            Expected: 2019-12-31 23:00:00
            But was:  2020-01-01 00:00:00
        */
    }

I've tried several routes. Using TimeZoneInfo...

var result = TimeZoneInfo.ConvertTimeToUtc(local, TimeZoneInfo.Local);

And parsing it as a string...

var result = DateTime.Parse(local.ToString("O"), CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal | DateTimeStyles.AdjustToUniversal);

None of these work. I feel like all of these should work. What am I missing?

numberjak
  • 1,065
  • 4
  • 13
  • 28
  • 5
    Which time zone are you in? Note that `new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Local)` might be standard time, while `DateTimeOffset.Now.Offset` might be DST. – Klaus Gütter Aug 26 '20 at 09:52
  • I'm in London which looking at TimezoneInfo.Local shows as +00:00:00 – numberjak Aug 26 '20 at 09:56
  • Why is there a difference? – numberjak Aug 26 '20 at 09:56
  • 3
    Because of daylight saving time – Klaus Gütter Aug 26 '20 at 09:56
  • Should I be using DST over Standard Time? – numberjak Aug 26 '20 at 09:57
  • 5
    "I create a local DateTime where the offset should be 00:01:00 (GMT Standard Time)." - Why would you expect the offset to be 1 hour, when you're in London and you've specified a date in January? The UTC offset in January is 0. – Jon Skeet Aug 26 '20 at 10:03
  • 1
    Ah I see! I think choosing January 1st for my test really confused me then! Thanks – numberjak Aug 26 '20 at 10:07
  • "var local = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Local);" - This means January first, 2020. Since your'e in London, it means GMT - and GMT *always* exactly match UTC. The UK use BST (British Summer Time) for DST(Daylight Saving Time) only in summer; January is not in summer. – Zohar Peled Aug 26 '20 at 10:10

1 Answers1

2

This verifies that you are currently (August) 1 hour ahead of UTC:

Assert.AreEqual(DateTimeOffset.Now.Offset, TimeSpan.FromHours(1)); // Passes

While the actual test converts a date in January when there is probably a different UTC offset. So if you have configured DST, it is expected that your test fails.

Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
  • Do you know how to convert a DateTime from local to UTC using DST instead of Standard Time? – numberjak Aug 26 '20 at 10:00
  • I don't understand; the data chosen in your test (01.01.2020) definitely has no DST in London. – Klaus Gütter Aug 26 '20 at 10:01
  • 2
    @numberjak: Why would you want to use daylight saving time on a date when daylight saving time isn't observed? Any time you feel you need to do that, there's probably a bigger problem somewhere. – Jon Skeet Aug 26 '20 at 10:03
  • @numberjak I think there is a fundamental misunderstanding of how DST works going on here. You may want to take one or two steps back and do some reading. (No offense) – Fildor Aug 26 '20 at 10:07