Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?



[C#] DateTime changes when passed to a web service


5 replies to this topic - - - - -

#1 sathenzar

    Resident Fanatic

  • 871 posts
  • Joined: 12-June 06

Posted 05 April 2012 - 01:39

When I pass a date time object (via serialization) to the server web service, it converts it to have an offset that matches the servers time. Is there a way to force it to just keep the local time
as it's being converted. I don't care about the time zones, I just care about the clients local time and their time only. The client uploads a file and the database stores their local machine time with it,
not the servers. The servers machine time will NEVER matter in this solution. So if it's 7 PM EST I don't want it being sent then converted to 4 PM PST when the web service reads it.


#2 +scumdogmillionaire

    Neowinian Senior

  • 2,270 posts
  • Joined: 23-October 01
  • Location: Denver, CO
  • OS: Windows 8 Pro
  • Phone: Nokia Lumia 920

Posted 05 April 2012 - 01:40

DateTime.UtcNow

#3 OP sathenzar

    Resident Fanatic

  • 871 posts
  • Joined: 12-June 06

Posted 05 April 2012 - 01:55

If I call ToUniversalTime (I'm using the last write datetime on files in my computer) then I guess I'll have to take that value and convert it to local time to the client?

#4 +scumdogmillionaire

    Neowinian Senior

  • 2,270 posts
  • Joined: 23-October 01
  • Location: Denver, CO
  • OS: Windows 8 Pro
  • Phone: Nokia Lumia 920

Posted 05 April 2012 - 02:08

Nah, don't convert like that. Just use UtcNow, this will read it locally on the client, and locally on the server, if the client ever reads it back from the server it will read back as the client's local time.

#5 OP sathenzar

    Resident Fanatic

  • 871 posts
  • Joined: 12-June 06

Posted 05 April 2012 - 03:58

UtcNow just returns the current utc time though. That won't help me convert the existing db rows of datetime to utc, so how would that work?
Ex:
FileSvcDef fileDef = request.FileSvcDefLst[i]; // <------------------- this has a datetime property that I passed via a web service and once it hits the server changes the timezone 3 hours back.
lastSyncDate = (DateTime)mReader["file_last_sync"]; // This is the right time coming straight from the database.
FileSvcSyncDef fileSyncDef = new FileSvcSyncDef(fileDef.GameId, -1, fileDef.FileName, (DateTime)lastSyncDate, fileDef.DirPath, SyncLocation.Server);


#6 OP sathenzar

    Resident Fanatic

  • 871 posts
  • Joined: 12-June 06

Posted 05 April 2012 - 16:17

Another solution I found: Just re-create the date time and pass the kind as unspecified. Worked like a charm! Thanks for the replies everyone.