使用UTC时间在C#.Net 4.0中提醒服务

时间:2012-08-08 17:57:06

标签: c#-4.0 timezone reminders

我有一个在我的

上运行的Windows服务
  

德克萨斯州服务器中心时间。

这将检查所有有效的提醒,并比较用户想要的提醒时间,并在与用户所需时间匹配时发送提醒。

方案

  1. 用户来自EST
  2. 用户通过我的网站使用UI为* 1:25 PM * 设置提醒
  3. 在提交时,我的C#业务逻辑将此时间转换为UTC,然后存储在我的数据库中。这将成为 '18:25:00'
  4. 我的商家逻辑将从数据库中提取所有活动提醒
  5. 如果当前UTC时间提醒设置时间 差异小于5分钟,则检查提醒时间,然后它会发送通知那个客户。
  6. 这就是我写的逻辑

     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

1 个答案:

答案 0 :(得分:2)

嗯...我们目前在美国大部分地区都使用夏令时(虽然印第安纳州有一些地方确实/确实使用过EST?)因为EDT比EST早一个小时,所以你的逻辑是正确的。输入(EST)不正确。