views:

2291

answers:

3

I have a web service (actually a silverlight enable wcf service, but for all intents and purposes, it's a web service) with a method taking a datetime parameter. The method gets called from a client, which could be in a different time zone. The service hosting the web service is in pacific standard time, and it appears the web service is converting the date time value to the servers time zone. The intent of the web service is to compare the datetime value against a value in the database, but the value in the database is based on the clients timezone, so because of the conversion, the comparison is not working.

Is there any way to turn the conversion off?

A: 

Check out this question, it has some discussion of what you want.

It looks like the easiest way is to pass the client timezone as well in the webmethod, or just pass the whole time as a string rather than a DateTime

Andrew Barrett
+1  A: 

and it appears the web service is converting the date time value to the servers time zone.

I'm trying to make sure I have this correct: your web service method takes a parameter of type DateTime (as in, System.DateTime) and is converting the passed parameter?

DateTime objects don't carry an implicit timezone, so any determination by the web service to infer local time would be a logic flaw (at least without supporting timezone criteria.)

If you can supply your server and client code snippets, it would make the scenario easier to diagnose.

UPDATE: Not sure if this scenario is accurate, so please clarify:

  • Client submits request with DateTime value of '12:00:00 00:00:00 PM CST'
  • Server in PST receives/interprets request at '10:00:00 00:00:00 PM PST'

If this is the case, can you convert all data to UTC time? If you're executing a query lookup, this implies converting the stored data to UTC as well.

jro
When a date time is serialized it's format includes timezone information.
Jeremy
Sorry, misunderstood the question -- it sounded as if the service were converting the value, not the WCF internals.
jro
I'm going to have to do this. Store the datetimes in the database as utc, and convert the client values to utc before calling the web service.
Jeremy
Since we work with UTC exclusively, I never saw the DateTime conversion in any method parameters for our service. Glad to help; something learned on my end as well. :-)
jro
+1  A: 

Not quite true that DateTime instances (structs) do not carry TimeZone info. There is a DateTimeKind property on each DateTime struct, as of .NET ... 3.0? I don't know the version. "Kind" is UTC, Local, or Unspecified. so while it is not a TimeZone per se, it is an inidication that the time is either expressed in the local zone, or in the universal time zone. Check the doc for more info.

To answer the original question - convert everyone to a single standard timezonee. UTC works just fine, and is easy because there are methods built-in to the BCL, but it doesn't really matter what timezone you choose. Just get everyone to agree on a standard.

Another thing you can do is transmit the time values as "TotalSeconds" values since... some specific time in the past. Like Jan 1 1970 (unix time_t), or Jan 1 1601 (windows FILETIME). But implicit in this approach is that all cooperating parties agree that this beginning point in time is UTC. So one million seconds since the beginning (the "epoch") refers to the same moment whether I am in El Paso or Nashua or Tokyo or London.

Cheeso