Freigeben über


WebServices and Timezone

Have you ever had to deal with consumer and provider of webservices residing in different time zones and one of the exchanges between them has to do with DateTime? If you have, then you probably know the output of the following piece of code. If not, I think its a good thing to know.

Let's say we have a server in NY hosting a web service exposing the following web methods:

    [WebMethod]
    public DateTime GetServerTime()
    {
        return DateTime.Now;
    }

    [WebMethod]
    public DateTime GetClientTime(DateTime clientTime)
    {
        return clientTime;
    }

    [WebMethod]
    public string ReturnMyTime(DateTime clientTime)
    {
        return clientTime.ToString();
    }

 

The consumer of this Web service is in LA. We have a WSManager class which takes care of the communication with the web service. The consumer invokes the web services in the following way:

string serverTime = WSManager.GetServerTime().ToString();

string clientTime = WSManager.GetClientTime(DateTime.Now).ToString();

string clientTime = WSManager.ReturnMyTime(DateTime.Now);

Will there be any differences between the three strings? If yes, what will it be?

 

[Update]: Raymond talks about differences between Win32 and .NET's way of dealing with TimeZones and Day light savings

[Update]: On a relevant note do you see any difference between the following two lines of code, where the attempt is to have a DateTime (dt1, dt2) in universal time following the statements:

DateTime dt1 = DateTime.UtcNow.AddDays(184)

DateTime dt2 =DateTime.Now.AddDays(184).ToUniversalTime().ToString()

[Update]: BCL Team's blog on how the DateTime issues have been addressed in .NET 3.5 - A Brief History of DateTime and A Brief History of DateTime Follow-up

Comments

  • Anonymous
    March 30, 2006
    The comment has been removed
  • Anonymous
    March 30, 2006
    You are right. The key here is that the web services try to do the basic translation between the time zones provided you talk in DateTime. Once you start transliterating then you have to do plumbing work on either side of it.

    In short, I prefer webmethods 1 and 2 but not 3.

    Another thing I try to keep in mind while building applications which have DateTime as a parameter is make them talk UTC. That way you are in clear. It is kind of what XML is to distributed applications residing on diverse platforms.
  • Anonymous
    August 14, 2007
    Jeff, The culture info problem you describe can be overcome by flowing the client culture to the server ... see http://www.w3.org/TR/2005/WD-ws-i18n-20050914/