Timezone failure using WCF

I have a SaaS system with several clients around the world. However, each customer is in only one time zone. Communication between the client and my central server is done using WCF.

Now there is the planning aspect for the system, and the time should be stored in terms of local time. But I find that when I send a class marked with a DataContract attribute that has a DataMember property of type DateTime via the WCF interface, the system becomes too smart by half, and this translates the time into server time. OTOH, if I pass the DateTime value directly through the WCF interface as a parameter, time comes verbatim, like client time.

It causes me a lot of headaches. Is there something I can set up somewhere so that the WCF service does not translate time to local (server) time?

EDIT: Well, I don’t know what I did, but I was working on some related things, and the problem seemed to just go away! Therefore, I would like to try my answers, but in fact I can not reproduce my problems now ... If I had time for academic exercises, I would look deeper, but right now, if it did not break ...

+6
source share
3 answers

WCF is not able to automatically convert time values ​​to UTC. You can use and store UTC time for your application. Of course, you need to undo this conversion when you display these values ​​in the user interface.

+9
source

I know this is an old question, but if someone needs it, here it is:

I assume that you are running DateTime.Now on the client side. Instead of using this statement, use:

 DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified) 

It gets the local date for the client and will not change on the server.

+4
source

Try using DateTimeOffset objects - the time zone offset is also saved, and if the time is translated, you should be able to translate it back. You need .NET 3.5 or later.

You will have to change the code a bit, as DateTimeOffset and DateTime cannot be passed to each other (they can be converted).

You do not need to change the calls to the stored procedure, but if you find it necessary, DateTimeOffset also exists in SQL Server.

+2
source

Source: https://habr.com/ru/post/888426/


All Articles