我需要确定事件的Olson TZID和事件的DateTime的时间偏移。
我想我可以在Noda Time的帮助下完成它,但我是新来的,需要帮助 - 实际API调用序列的一个例子来执行此任务。
我们正在使用和做的更多细节。
我可以自由下载并使用任何必要的工具或数据。我已经从http://efele.net/maps/tz/下载了shapefile,并使用Shape2SQL http://goo.gl/u7AUy将它们转换为带有TZID和GEOM的SQL Server表。
我知道如何通过查询该表从Lat / Long 获取TZID。
我需要的是从TZID和DateTime确定时间偏移。
答案 0 :(得分:1)
感谢Jon Skeet和Matt Johnson为Noda Time Google Group http://goo.gl/I9unm0提供答案。 Jon解释了如何在给定Olson TZID和NodaTime LocalDateTime值的情况下获得Microsoft BCL DateTimeOffset值。 Matt指出了一个确定DST是否在ZonedDateTime:What is the System.TimeZoneInfo.IsDaylightSavingTime equivalent in NodaTime?
时处于活动状态的示例我将写一篇博客文章,总结我从Lat / Long获取Olson TZID的经验,将DateTime字符串解析为LocalDateTime并确定DateTimeOffset。我将在那里展示GetTmzIdByLocation(纬度,经度)和GetRoughDateTimeOffsetByLocation(纬度,经度)方法如何工作以及为什么我需要两者(第一种方法不适用于海洋上的位置)。一旦我写完这篇文章,我会在这里添加评论。
注意,在下面的代码中解析DateTime字符串还不是最优的;正如Matt在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提供)What is the System.TimeZoneInfo.IsDaylightSavingTime equivalent in NodaTime?
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;
}
}