从GMT到本地的时区转换

本文关键字:时区 转换 GMT | 更新日期: 2023-09-27 18:12:17

我有以下信息:

string shippedTime = "10:53 AM";
string shippedDate = "12/12/2012";
string shippedTimeZone = "(GMT-05:00) Eastern Time (US & Canada)";
string receivedTime = "10:45 AM";
string receievedDate = "12/13/2012";

我需要找出运送时间和接收时间(在当地时区)之间的差异,以小时为单位,考虑到时区和夏令时。这是我从外部跟踪系统中提取的数据,这就是我得到的数据。因此,shippedTimeZone格式是一种未知的—我不知道该系统中有多少时区以及确切的字符串格式是什么—或者更确切地说,我不能保证格式总是相同的,这使得解析有点脆弱。

除了解析所有这些字符串以提取GMT更正并连接日期和时间外,我是否应该为此查看其他库?还是用更直接的方法得到我想要的?

从GMT到本地的时区转换

这看起来像一个Windows时区,就像你用TimeZoneInfo得到的那种,但是服务给你的是DisplayName属性而不是Id属性。这使得使用它有点麻烦。特别是因为显示名称是本地化的-你有英文名称。它在具有不同文化设置的计算机上看起来会有所不同。

也不能直接去掉偏移量——因为Windows显示名称总是显示标准偏移量,即使该区域是夏令时。

基于它说的是"GMT"而不是"UTC"的事实,我猜它们来自较旧的计算机,如Windows XP, Windows Server 2003,或者可能是Windows嵌入式计算机。例如,你可以在这个列表中找到它。

真的应该回去找那个服务的所有者,让他们给你TimeZoneInfo.Id而不是TimeZoneInfo.DisplayName的值。假设您不能这样做,这里有一种方法可以让它与您拥有的东西一起工作:

// Look up the time zone.  This won't work on non-English language computers!
// (setting an English CultureInfo won't help you here)
var shippedTZ = TimeZoneInfo.GetSystemTimeZones()
    .FirstOrDefault(x => x.DisplayName == shippedTimeZone.Replace("GMT", "UTC"));
if (shippedTZ == null)
    throw new Exception("Time Zone Not Found!");
// You may want to handle the exception by hardcoding some specific time zones
// Let's turn your date/time strings into an actual DateTime
var shippedDT = DateTime.ParseExact(shippedDate + " " + shippedTime,
                            "MM/dd/yyyy hh:mm tt", CultureInfo.InvariantCulture);
// Then we'll use the time zone to get a DateTimeOffset.
var shippedDTO = new DateTimeOffset(shippedDT,
                                    shippedTZ.GetUtcOffset(shippedDT));
// And the same thing for the received date...
var receivedTZ = TimeZoneInfo.Local;
var receivedDT = DateTime.ParseExact(receievedDate + " " + receivedTime,
                            "MM/dd/yyyy hh:mm tt", CultureInfo.InvariantCulture);
var receivedDTO = new DateTimeOffset(receivedDT,
                                     receivedTZ.GetUtcOffset(receivedDT));
// Now you can subtract them
TimeSpan elapsed = receivedDTO - shippedDTO;

这里你需要注意的另一件事是——如果其中一个日期/时间值是模糊的,你可能得不到正确的结果。这将发生在与日光节约时间相关的"回退"过渡期间。您对此无能为力,因为源数据中没有任何合格信息。

nodeatime -虽然是一个杰出的库,但对于您的特定问题,没有比这更好的了。您仍然会遇到相同的问题,即在没有正确ID的情况下解析时区,并映射模棱两可的本地日期时间。但是为了更好地衡量,下面是使用nodeatime:

的相同事情
// Try to locate the time zone
var bclZoneProvider = DateTimeZoneProviders.Bcl;
var zoneShipped = bclZoneProvider.Ids
    .Select(x => bclZoneProvider[x])
    .Cast<BclDateTimeZone>()
    .FirstOrDefault(x => x.DisplayName == shippedTimeZone.Replace("GMT", "UTC"));
if (zoneShipped == null)
    throw new Exception("Time Zone Not Found!");
// You wanted the system time zone
var zoneReceived = bclZoneProvider.GetSystemDefault();
// Parse the date/time values as a LocalDateTime
var pattern = LocalDateTimePattern
                  .CreateWithInvariantCulture("MM/dd/yyyy hh:mm tt");
var ldtShipped = pattern.Parse(shippedDate + " " + shippedTime).Value;
var ldtReceived = pattern.Parse(receievedDate + " " + receivedTime).Value;
// Assign them to the zones.
// "Leniently" means to use the standard offset when there is an ambiguity.
var zdtShipped = ldtShipped.InZoneLeniently(zoneShipped);
var zdtReceived = ldtReceived.InZoneLeniently(zoneReceived);
// Subtract them to get the Duration you are looking for.
Duration elapsed = zdtReceived.ToInstant() - zdtShipped.ToInstant();