从GMT到本地的时区转换

时间:2013-05-06 20:33:48

标签: c# datetime timezone gmt nodatime

我有以下信息:

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";

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

除了解析所有这些字符串以提取GMT更正并连接日期和时间之外,还有其他库我应该考虑这个吗?或者更直接的方式来获得我需要的东西?

1 个答案:

答案 0 :(得分:2)

它看起来像Windows时区,就像您使用TimeZoneInfo获得的那种,但该服务为您提供了DisplayName属性,而不是Id。这使得使用它有点麻烦。特别是因为显示名称已本地化 - 您有英文名称。在具有不同文化背景的计算机上看起来会有所不同。

您也不能只是偏移 - 因为Windows显示名称始终显示标准偏移,即使该区域处于夏令时。

基于它说“GMT”而不是“UTC”的事实,我猜它们来自较旧的计算机,例如Windows XP,Windows Server 2003或Windows Embedded计算机。例如,您可以在this list上找到它。

确实应该返回该服务的所有者,并要求他们为您提供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;

您需要注意的另一件事 - 如果其中一个日期/时间值不明确,您可能无法获得正确的结果。这将在与夏令时相关的“后退”过渡期间发生。您无能为力,因为您在源数据中没有任何合格信息。

NodaTime - 虽然杰出的库,但对于您的特定问题,不能比这更好。如果没有正确的ID并且映射了模糊的本地日期时间,您仍然会遇到解决时区的相同问题。但只是为了好的衡量,使用NodaTime是一回事:

// 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();