我有一个在我的
上运行的Windows服务德克萨斯州服务器中心时间。
这将检查所有有效的提醒,并比较用户想要的提醒时间,并在与用户所需时间匹配时发送提醒。
方案
这就是我写的逻辑
DateTime CurrentDate = DateTime.Now.ToUniversalTime();
TimeSpan currentTime = DateTime.Now.ToUniversalTime().TimeOfDay;
if (Reminder.DailyReminders.Any(x => currentTime.Subtract(x.ReminderTime).TotalMinutes < 5
&& currentTime.Subtract(x.ReminderTime).TotalMinutes > 0))
{
if (Reminder.ReminderMedhodID.Equals(1))
_email.ComposeEmail(Reminder);
}
我的问题 * currentTime *总是落后于用户请求提醒时间1小时所以我的提醒会延迟1小时。
注意:currentTime来自下面
TimeSpan currentTime = DateTime.Now.ToUniversalTime().TimeOfDay;
我不确定这是否是处理此要求的最佳方法。考虑到这是其中一种方式,可以帮助解决这个问题吗?
感谢彼得的回答
任何人都可以帮我了解如何通过日光考虑来获取用户输入时间
这是我到目前为止所拥有的
public TimeSpan ConvertToUTCTime(string dateStr)
{
DateTime localDateTime = DateTime.Parse(dateStr); // Local .NET timeZone.
DateTime utcDateTime = localDateTime.ToUniversalTime();
string clTimeZoneKey = TimeZone.CurrentTimeZone.StandardName;
TimeZoneInfo clTimeZone = TimeZoneInfo.FindSystemTimeZoneById(clTimeZoneKey);
DateTime clDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, clTimeZone);
if (clTimeZone.IsDaylightSavingTime(localDateTime))
{
// Get DayLight local time in UTC
// Yet to be implemented
}
return clDateTime.TimeOfDay;
}
我用这个工作了 http://msdn.microsoft.com/en-us/library/system.globalization.daylighttime.aspx
答案 0 :(得分:2)
嗯...我们目前在美国大部分地区都使用夏令时(虽然印第安纳州有一些地方确实/确实使用过EST?)因为EDT比EST早一个小时,所以你的逻辑是正确的。输入(EST)不正确。