0

I read many topics about the issue - here on stack overflow, msdn etc. However, I couldn't find the 'golden path'.

I have a classic issue here - There are clients from different timezones and a server from another one. The server should use the client timezone. Communication is done through WCF.

The most reasonable solution was to send the date from the client, as a Utc time (i.e.: DateTime.UtcNow (in order to sync the +/- on client) and on the server, use the ToUniversalTime (in order to sync the +/- timezone on server).

I think that this solution will solve my issue, but it will cause to many changes on current code.

Do you know any 'smarter' solutions for my issue ? Did I miss something here ?

Thanks, Joe.

leppie
  • 115,091
  • 17
  • 196
  • 297
  • Generally speaking, this is pretty much the resolution to the timezone problem you are experiencing where you would always store the UTC time on the server and then the client would either convert it to its format or you read the UTC from server and translate it to the client timezone over the wire. Required a lot of changes in code? Well - you need to then refactor the code better, such as create a static helper or extension method of some kind you can easily maintain dealing with timezones and conversion. – Ahmed ilyas Nov 05 '13 at 15:24
  • but it's mean i need to change all datetime properties. isn't there any elegant way ? – user1894419 Nov 05 '13 at 15:32
  • 1
    There is no "elegant way". The only real advice we can give you is ensuring that any future systems you design uses UTC time by default. There is absolutely no reason any new application should be built around local time. – Maxim Gershkovich Nov 05 '13 at 15:53

1 Answers1

2

Generally you want the server to work solely with UTC which should keep your information consistent across any time zone.

It's usually then on the client-side you want to gather some time zone information so you can allow clients to work with the dates/times in the format suited to them.

James
  • 80,725
  • 18
  • 167
  • 237
  • The thing is, it seems to me inelegant to change all datetime properties all over the code – user1894419 Nov 05 '13 at 15:34
  • 3
    @user1894419 - you should only need to make changes at points where the datetime values are being *shown* to the user (or where you're accepting user input). Everywhere else you *solely* work in UTC. I don't see how that results in you having to change properties all over the code. – Damien_The_Unbeliever Nov 05 '13 at 15:53
  • @user1894419 I got stung with this a few years back on a system I worked on - unfortunately there is no magic fix, you need to make those necessary changes. However, I had existing data in the DB (all in local time) so it was slightly trickier but I came up with a [solution](http://stackoverflow.com/questions/2700197/sql-server-convert-date-field-to-utc) to that problem. – James Nov 06 '13 at 09:44