4

We have a scheduler application. Using this application an user can schedule a task to run at 8 AM PT daily for the next 30 days. We convert the time to UTC and store it in the databse as we have users from various time zones. We use the following function to convert the time to UTC:

public static DateTime ToAccountLocalTime(this DateTime dt)
{
    return TimeZoneInfo.ConvertTimeFromUtc(dt, TimeZoneInfo.FindSystemTimeZoneById("user time zone"));
}

Now if the user scheduled a task to run at 8 AM PT daily for the next 30 days on 1st Nov 2012 then in the database 30 entries are made for each of the 30 days with time set to 4 PM UTC (as returned by the above method).

The problem occurred on Nov 4th when daylight saving ended. The task started running at 7 AM instead of 8 AM as the scheduler service runs all times based on UTC.

To solve this we need the above method to return 4 PM UTC for dates from 1st Nov to 3rd Nov and 3 PM UTC for dates after 4th Nov. What changes should I make to achieve this?

4

2 回答 2

0

您需要为 30 天中的每一天进行转换,因此请为每个, = ..调用ToUtc(或调用与您在问题中粘贴的例程相反的任何内容)并存储这些结果。localDate.AddDays(n)n130

如果ToUtc定义类似于:

return TimeZoneInfo.ConvertTimeToUtc(dt, TimeZoneInfo.FindSystemTimeZoneById("user time zone"));

它应该在指定的时间迎合 DST。(注意模棱两可或无效时间的例外情况。)

于 2012-11-13T00:59:01.553 回答
0

如果本地时间很重要,我会存储本地时间 + 时区,并在需要 UTC 时将其转换为 UTC。

然后您存储太平洋时间上午 8 点,它将根据当前是否为 DST 转换为下午 3 点或 4 点 UTC。

您将遇到的问题取决于您的数据库,它可能不支持时区。我可能会存储您传入的字符串FindSystemTimeZoneById。如果你想变得花哨,你可以制作一个表格,列出所有具有标识列的人并存储 Id。

于 2012-11-12T13:46:38.350 回答