1

我在理解如何将用户当前日期和时间与服务器时间相匹配时遇到了一些麻烦。

示例:假设我有一个用户可以注册自己的网站。一种配置文件选项是他们可以选择当地时区。为此,有一个下拉菜单,他们可以从中选择正确的时区。因此,来自中国的用户可能会选择 (UTC+08:00) 北京、重庆、香港、乌鲁木齐,来自洛杉矶的另一个用户将选择 (UTC-07:00) 山区时间(美国和加拿大)(我假设)和来自巴黎的人将选择 (UTC+01:00) 布鲁塞尔、哥本哈根、马德里、巴黎。

Web 应用程序在美国的服务器上运行,具有特定的时区......

现在......所有这些用户都希望在下周五的 19:00 收到他们当地时区的电子邮件通知!!!

在这里,我迷路了...对于所有这些用户来说,下周五 19:00 肯定不是同一时间...

如何映射他们的个人资料时区,以便我网站上运行的服务将在下周五 19:00 用户本地时区发送电子邮件通知???

我目前正处于这个阶段......用所有时区填充下拉菜单,以便用户可以在他们的个人资料中选择他们当前的时区。

当页面加载比下拉列表填充时区时:

    protected void Page_Load(object sender, EventArgs e)
    {
        ddlUserTimeZone.DataSource = GetTimeZones();
        ddlUserTimeZone.DataTextField = "Name";
        ddlUserTimeZone.DataValueField = "ID";
        ddlUserTimeZone.DataBind(); 
    }

    public Collection<TimeZoneItem> GetTimeZones()
    {
        Collection<TimeZoneItem> timeZones = new Collection<TimeZoneItem>();
        foreach (var timeZoneInfo in TimeZoneInfo.GetSystemTimeZones())
        {
            timeZones.Add(new TimeZoneItem 
            { 
                TimeZoneName = timeZoneInfo.DisplayName, 
                TimeZoneID = timeZoneInfo.Id 
            });

        }

        return timeZones;
    }

    public struct TimeZoneItem
    {
        public string TimeZoneName { get; set; }
        public string TimeZoneID { get; set; }
    }

现在,你们能帮忙将个人资料时区与当前时间匹配,以便在正确的时间发送电子邮件吗?

提前致谢!

4

2 回答 2

2

Are you just setting up this service? If so, run your web servers and database servers on Universal Time Coordinated (UTC or Zulu) time, not a local time zone. Everything is far easier to manage if you do that. I learned this the hard way.

UTC used to be called Greenwich Mean Time; it is timezone +00:00. It doesn't change for daylight savings time like US and European local time does.

This time zone thing is a pain, and worth getting right. Some countries have half-hour time zones.

At any rate, once you know each user's preferred time zone, and the time she wants her notification, you can then convert the time from local to UTC and store it.

Try something like this to get the user's hour and minute into UTC. (Time zone conversions need a date, because they depend on daylight savings time rules. There's one more complication. On the day when a time zone switches from daylight to standard, or vice versa, the UTC time for a notification will change. Most people handle this by recomputing the UTC date and time of the next notification (tomorrow's notification) as they send each notification. Consider this code.

TimeZoneInfo userLocal =            ;//user's time zone
int hour =                          ;//whatever the user wants
int minute =                        ;//whatever the user wants

DateTime tomorrow = DateTime.Now.AddDays(1);
int year = tomorrow.Year;
int month = tomorrow.Month;
int day = tomorrow.Day;
DateTime notificationTime = new DateTime(year,month,day,
                                         hour,minute,0, 
                                         DateTimeKind.Unspecified);
DateTime tomorrowNotificationTime = TimeZoneInfo.ConvertTimeToUtc(
                                         notificationTime,userLocal);

This should get you the UTC time you need to deliver this user's notification tomorrow, in the correct time zone for tomorrow's date.

于 2012-06-30T23:41:42.363 回答
0

理想情况下,日期时间应以 UTC 格式存储在服务器上。

您的服务器上有以下数据

  • 用户的时区信息。
  • 用户需要通知的时间。
  • 您服务器上的当前本地时间。

    // Convert current local server time to UTC.
    var serverUtc = DateTime.UtcNow;
    
    // Convert UTC time to users local time. This gives you the date and time as per the user.
    var userTimeZone = TimeZoneInfo.GetSystemTimeZones()[0]; // just an example. Replace with actual value.
    var userCurrentTime = TimeZoneInfo.ConvertTime(serverUtc, userTimeZone);
    
    /*
    add a day to userCurrentTime till its Friday. Add/subtract minutes till its 7:00PM.
    */
    
    var expectedNotificationTimeUtc = TimeZoneInfo.ConvertTimeToUtc(userCurrentTime, userTimeZone);
    /*
    1. store the expectedNotificationTimeUtc as the time you want to send the email. 
    2. your service can check for users their expectedNotificationTimeUtc and 
         if the UtcNow is within an acceptable range of the that time, send the email.
    */
    
于 2012-07-01T01:04:35.937 回答