Jeremy
Jeremy

Reputation: 46340

datetime time zone conversion in .net web service

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?

Upvotes: 2

Views: 9988

Answers (3)

Cheeso
Cheeso

Reputation: 192457

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.

Upvotes: 1

jro
jro

Reputation: 7480

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.

Upvotes: 1

Andrew Barrett
Andrew Barrett

Reputation: 19911

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

Upvotes: 0

Related Questions