TimeZone.IsAmbiguousTime,为什么GMT模糊时间失败?

时间:2011-02-10 09:31:38

标签: c# .net datetime

我的情况是我的服务器发送CEST时间,我需要在服务器上显示它,因为它是从服务器接收的,除了DST开关.GMT中的模糊时间应该转换为GMT中的明确时间。也许是以下测试可以解释我的意图。

[Test]
public void Should_process_server_time()
{
    var britishZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
    var germanZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
    var firstAmbigiousMarchEntry = new DateTime(2011, 03, 27, 01, 00, 00);
    var secondAmbigiousMarchEntry = new DateTime(2011, 03, 27, 01, 30, 00);

    //Why does this fail even though 2011-03-27 01:00:00 is an ambigous time in GMT?
    Assert.That(britishZone.IsAmbiguousTime(firstAmbigiousMarchEntry));
    Assert.That(britishZone.IsAmbiguousTime(secondAmbigiousMarchEntry));

    var unAmbigiousFirstMarchEntry = TimeZoneInfo.ConvertTime(firstAmbigiousMarchEntry, germanZone, britishZone);
    var unAmbigiousSecondMarchEntry = TimeZoneInfo.ConvertTime(secondAmbigiousMarchEntry, germanZone, britishZone);

    Assert.That(britishZone.IsAmbiguousTime(unAmbigiousFirstMarchEntry), Is.False);
    Assert.That(britishZone.IsAmbiguousTime(unAmbigiousSecondMarchEntry), Is.False);

    Assert.That(unAmbigiousFirstMarchEntry.Hour == 2);
    Assert.That(unAmbigiousFirstMarchEntry.Minute == 0);
    Assert.That(unAmbigiousFirstMarchEntry.Second == 0);

    Assert.That(unAmbigiousSecondMarchEntry.Hour == 2);
    Assert.That(unAmbigiousSecondMarchEntry.Minute == 30);
    Assert.That(unAmbigiousSecondMarchEntry.Second == 0);

}

请帮忙吗?我的目的是将英国地区的模糊时间转换为英国地区的明确时间,即2011-03-27 01:00:00至2011-03-27 02:00:00。为此,我需要确定是否DateTime在GMT区域收到的是不明确的,但是呼叫失败了。 我的系统正在运行英国区域设置。

我尝试使用以下测试,然后通过。现在我很困惑,因为GMT区域IsInvalidTimeIsAmbigoustime之间有什么区别?

[Test]
public void Should_process_server_time()
{
    var britishZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
    var germanZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
    var firstAmbigiousMarchEntry = new DateTime(2011, 03, 27, 01, 00, 00);
    var secondAmbigiousMarchEntry = new DateTime(2011, 03, 27, 01, 30, 00);

    Assert.That(britishZone.IsInvalidTime(firstAmbigiousMarchEntry));
    Assert.That(britishZone.IsInvalidTime(secondAmbigiousMarchEntry));

    var unAmbigiousFirstMarchEntry = TimeZoneInfo.ConvertTime(firstAmbigiousMarchEntry, germanZone, britishZone);
    var unAmbigiousSecondMarchEntry = TimeZoneInfo.ConvertTime(secondAmbigiousMarchEntry, germanZone, britishZone);

    unAmbigiousFirstMarchEntry = unAmbigiousFirstMarchEntry.AddHours(2);
    unAmbigiousSecondMarchEntry = unAmbigiousSecondMarchEntry.AddHours(2);

    Assert.That(britishZone.IsAmbiguousTime(unAmbigiousFirstMarchEntry), Is.False);
    Assert.That(britishZone.IsAmbiguousTime(unAmbigiousSecondMarchEntry), Is.False);

    Assert.That(unAmbigiousFirstMarchEntry.Hour == 2);
    Assert.That(unAmbigiousFirstMarchEntry.Minute == 0);
    Assert.That(unAmbigiousFirstMarchEntry.Second == 0);

    Assert.That(unAmbigiousSecondMarchEntry.Hour == 2);
    Assert.That(unAmbigiousSecondMarchEntry.Minute == 30);
    Assert.That(unAmbigiousSecondMarchEntry.Second == 0);
}

你认为这可以更好地实施吗?

1 个答案:

答案 0 :(得分:5)

我认为答案是GMT永远不会模棱两可。 GMT总是+0。伦敦的时区称为西欧时间,冬季为+0,夏季为+1。