Asp.net 确定给定Olson TZID和本地日期时间的时间偏移量?
我需要确定给定事件的Olson TZID和事件的日期时间的时间偏移量 我想我可以在野田佳彦的帮助下完成这项任务,但我是新来的,需要帮助——这是执行这项任务的实际API调用序列的一个示例 关于我们正在使用和正在做的事情的更多细节Asp.net 确定给定Olson TZID和本地日期时间的时间偏移量?,asp.net,sql-server,timezone-offset,nodatime,iana,Asp.net,Sql Server,Timezone Offset,Nodatime,Iana,我需要确定给定事件的Olson TZID和事件的日期时间的时间偏移量 我想我可以在野田佳彦的帮助下完成这项任务,但我是新来的,需要帮助——这是执行这项任务的实际API调用序列的一个示例 关于我们正在使用和正在做的事情的更多细节 我们正在运行基于ASP.NET+SQL Server的网站。我们网站的用户输入并保存在不同地点发生的事件。对于每个事件,Lat/Long和DateTime以及(该事件的)时间偏移是必填字段 数据输入有多种情况,有时先输入Lat/Long,有时先输入DateTime。系统允
我知道如何通过查询该表从Lat/Long获取TZID。
同样,我需要的是确定TZID和DateTime的时间偏移量感谢Jon Skeet和Matt Johnson为Noda Time Google Group提供的答案。Jon解释了如何在给定Olson TZID和NodeTime LocalDateTime值的情况下获取Microsoft BCL DateTimeOffset值。Matt指出了一个确定DST在给定ZoneDateTime时是否处于活动状态的示例: 我将写一篇博文,总结我从Lat/Long中获取Olson TZID、将DateTime字符串解析为LocalDateTime并确定DateTimeOffset的经验。我将在这里展示GetTmzIdByLocation(纬度,经度)和GetRoughDateTimeOffsetByLocation(纬度,经度)方法是如何工作的,以及为什么我需要这两种方法(第一种方法不适用于海洋上的位置)。一旦我写了这篇文章,我会在这里添加评论 注意,在下面的代码中解析DateTime字符串还不是最优的;正如马特在谷歌集团的一篇帖子(上面的链接)中解释的那样,使用野田佳彦时间工具比BCL更好。参见第页的相关问题 我当前的代码:
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提供)
我想我已经在野田佳彦时代谷歌集团得到了Jon Skeet对我问题的正确答案。尽管如此,在这里提问和回答并没有什么坏处,因为其他人也可能会使用它。如果是这样的话,你可以自己回答这个问题,在一段时间后,接受它。
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;
}
}