感谢 Jon Skeet 和 Matt Johnson 在 Noda Time Google Group http://goo.gl/I9unm0上提供答案。Jon 解释了如何在给定 Olson TZID 和 NodaTime LocalDateTime 值的情况下获取 Microsoft BCL DateTimeOffset 值。Matt 指出了一个在给定 ZonedDateTime 的情况下确定 DST 是否处于活动状态的示例:NodaTime 中的 System.TimeZoneInfo.IsDaylightSavingTime 等效项是什么?
我将写一篇博文总结我从 Lat/Long 获取 Olson TZID、将 DateTime 字符串解析为 LocalDateTime 并确定 DateTimeOffset 的经验。我将在那里展示 GetTmzIdByLocation(latitude, longitude) 和 GetRoughDateTimeOffsetByLocation(latitude, longitude) 方法的工作原理以及为什么我需要这两种方法(第一种方法不适用于海洋位置)。一旦我写了这篇文章,我会在这里添加评论。
请注意,在下面的代码中解析 DateTime 字符串还不是最佳的;正如马特在 Google Group 帖子(上面的链接)中解释的那样,使用 Noda Time 工具比使用 BCL 更好。请参阅http://goo.gl/ZRZ7XP上的相关问题
我当前的代码:
public object GetDateTimeOffset(string latitude, string longitude, string dateTime)
{
var tzFound = false;
var isDST = false;
var tmpDateTime = new DateTimeOffset(DateTime.Now).DateTime;
if (!String.IsNullOrEmpty(dateTime))
{
try
{
// Note: Looks stupid? I need to throw away TimeZone Offset specified in dateTime string (if any).
// Funny thing is that calling DateTime.Parse(dateTime) would automatically modify DateTime for its value in a system timezone.
tmpDateTime = DateTimeOffset.Parse(dateTime).DateTime;
}
catch (Exception) { }
}
try
{
var tmzID = GetTmzIdByLocation(latitude, longitude);
DateTimeOffset result;
if (String.IsNullOrEmpty(tmzID) || tmzID.ToLower() == "uninhabited") // TimeZone is unknown, it's probably an ocean, so we would just return time offest based on Lat/Long.
{
var offset = GetRoughDateTimeOffsetByLocation(latitude, longitude);
result = new DateTimeOffset(tmpDateTime, TimeSpan.FromMinutes(offset * 60)); // This only works correctly if tmpDateTime.Kind = Unspecified, see http://goo.gl/at3Vba
} // A known TimeZone is found, we can adjust for DST using Noda Time calls below.
else
{
tzFound = true;
// This was provided by Jon Skeet
var localDateTime = LocalDateTime.FromDateTime(tmpDateTime); // See Noda Time docs at http://goo.gl/XseiSa
var dateTimeZone = DateTimeZoneProviders.Tzdb[tmzID];
var zonedDateTime = localDateTime.InZoneLeniently(dateTimeZone); // See Noda Time docs at http://goo.gl/AqE8Qo
result = zonedDateTime.ToDateTimeOffset(); // BCL DateTimeOffset
isDST = zonedDateTime.IsDaylightSavingsTime();
}
return new { result = result.ToString(IncidentDateFormat), tzFound, isDST };
}
catch (Exception ex)
{
IMAPLog.LogEvent(System.Reflection.MethodBase.GetCurrentMethod().Name, "", ex);
throw new CustomHttpException("Unable to get timezone offset.");
}
}
用于确定 DST 是否处于活动状态的扩展方法(由 Matt Johnson 提供)NodaTime 中的 System.TimeZoneInfo.IsDaylightSavingTime 等效项是什么?
public static class NodaTimeUtil
{
// An extension method by Matt Johnson - on Stack Overflow at http://goo.gl/ymy7Wb
public static bool IsDaylightSavingsTime(this ZonedDateTime zonedDateTime)
{
var instant = zonedDateTime.ToInstant();
var zoneInterval = zonedDateTime.Zone.GetZoneInterval(instant);
return zoneInterval.Savings != Offset.Zero;
}
}