开发者

Time zone problem with the user time and server time being different

开发者 https://www.devze.com 2023-02-22 05:48 出处:网络
Basically i have a silverlight .net web application where by the user does his scheduling for his tasks etc. So he chooses a time and date for the task to run, and he could be anywhere in the world, t

Basically i have a silverlight .net web application where by the user does his scheduling for his tasks etc. So he chooses a time and date for the task to run, and he could be anywhere in the world, then this time date gets stored in the database however the server that picks up and runs this scheduled tasks will be somewhere else (lets say malta etc) so the time zone will be different. Whats the best possible way without aski开发者_运维百科ng the user for the time zone, to convert the users time to the servers time and servers time to user time vice versa in order to process the tasks?

Thanks in advance, Jon


I think you want to store everything on the server in UTC time and then convert it back to the users time when you go to display it in the client application. When the user schedules a task, convert that time to UTC time on the client and pass that to the server. The server then always passes back UTC time to the client.

Here is a quick code example to get the UTC time from the client and convert it to the local time.

//Get UTC time
var utcTime = DateTime.UtcNow;
Console.WriteLine("{0} {1}", utcTime, utcTime.Kind);

//Convert UTC time to local time
var localTime = utcTime.ToLocalTime();
Console.WriteLine("{0} {1}", localTime, localTime.Kind);

The Kind property will tell you if you are dealing with local or UTC time. This SO question also has some good information about converting from UTC to local time.


>Whats the best possible way without asking the user for the time zone

You are going then to have to assume the clients OS timezone is correctly set (this is often not the case) and use this timezone to convert to/from your servers time which should be in UTC

Here are a couple of extension methods that allow you to do this.

Any dates sent to the client should call ToDisplayTime() before being rendered on the client UI.

Likewise, before the client sends a date to the server it should call FromDisplayTime() to convert it to UTC. thus the server sends and receives times in UTC only

/// <summary>
    /// Converts the specified DateTime (which should be the UTC time) into the users timezone.
    /// </summary>
    /// <param name="utcDateTime"></param>
    /// <returns></returns>
    public static DateTime ToDisplayTime(this DateTime utcDateTime)
    {
        var result = TimeZoneInfo.ConvertTime(utcDateTime, TimeZoneInfo.Local);
        return result;
    }

    /// <summary>
    /// Converts the specified DateTime from local time to UTC
    /// </summary>
    /// <param name="locatDateTime"></param>
    /// <returns></returns>
    public static DateTime FromDisplayTime(this DateTime locatDateTime)
    {
        var result = TimeZoneInfo.ConvertTime(locatDateTime, TimeZoneInfo.Local, TimeZoneInfo.FindSystemTimeZoneById("UTC"));
        return result;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号