0

Suppose I have this controller action:

public HttpResponseMessage AddUser([FromBody] UserInfo info){...}

And the class UserInfo has 2 properties:

Public string UserName { get; set;}
Public DateTimeOffset AddedOn { get; set;}

And the following request comes in which has a DateTime string:

POST http://webapiserver:1234/users/add HTTP/1.1
Content-Type: application/json
{
  "UserName": "user1",
  "AddedOn": "2014-04-02T13:00:00"
}

The value for AddedOn is deserialized to a DateTimeOffset object of "4/2/0001 1:00:00 PM -05:00". Note that the offset "-05:00" was added - this is the time zone of the web API server.

The question: is there anyway I can cause the deserialization of DateTimeOffset for a zone-unspecified time string to use a different time zone offset instead of the default value which is the web server's time zone offset?

Note: you may ask: why use DateTimeOffset? I have to do so in order to honor requests which include time zone offset. However, we have to handle requests that have unspecified time as well.

You may also ask: what's wrong with the current default offset, which is the web API server's time zone offset? This is because we want to handle the case where the database and the web server are in different time zones. So I need the deserialization to use the database's time zone, not the web server's time zone.

I know it is an option to use a custom media type formatter. But I'm hoping that there's an easier way since all I need is for the serializer to use a specific time zone offset when deserializing DateTimeOffset data. Any suggestion would be greatly appreciated.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Zoomzoom
  • 1,042
  • 2
  • 13
  • 32
  • If the date is UTC then just send it up with a `0` hours offset `2014-04-02T13:00:00+00:00` – James Apr 03 '14 at 15:30
  • But we don't control the format of the time that comes in, so the offset information isn't guaranteed to be there. This question is pertaining to the specific case where the offset is *not* included. – Zoomzoom Apr 03 '14 at 15:37
  • 1
    The answer to this question can be derived from the answer by Matt Johnson to another question: http://stackoverflow.com/questions/22846319/is-it-possible-to-specify-an-offset-for-json-net-deserialization-of-datetimeoffs – Zoomzoom Apr 03 '14 at 22:12

0 Answers0