The client ignores the time zone for date fields

When sending data from Android (Kotlin client) is not transferred to the time zone:

ServiceStack:

JsConfig.DateHandler = DateHandler.ISO8601;
    
//POCO and data model
public class TestDataModel {
        public DateTime Date  { get; set; }
        public string DateStringByTimeZone { get; set; }
        public string TimeZone { get; set; }
     }
    
col.InsertOne(new TestDataModel {
        Date = request.Date,
        DateStringByTimeZone = request.DateStringByTimeZone,
        TimeZone = request.TimeZone,
    });

Kotlin:

val date = Calendar.getInstance(TimeZone.getDefault()).time
val df = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")

val request = TestDataModelRequest()
request.date = date
request.dateStringByTimeZone = df.format(date)

val client = AndroidServiceClient(host);
client.post(request)

result:

As you can see in the picture, changing the time zone on the client does not affect the date, the data on the client is correct, but when sending this data is lost.
When the date in the request is filled in as follows everything is processed correctly, so I assume the problem is in the client:

{ "Date": "2001-07-04T12:08:56+0300" }

I ask to prompt how correctly to transfer date, with time zone?
Thanks.

C#'s DateTime doesn’t have Time Zones and are sent over as UTC, you’ll need to use a Type like DateTimeOffset which supports TimeZones or a different property to capture the TimeZone.

I understand, but if it would be possible to configure the serialization of date for AndroidServiceClient or JsonServiceClient that would solve the problem, it’s like something you can do?

for example:

JsConfig<DateTime>.SerializeFn = time => new DateTime(time.Ticks).ToString("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

You’re trying to send a TimeZone in a DateTime which you can’t do as it doesn’t support TimeZones so there’s no serialization you can configure on the client to send TimeZone’s in a DateTime, it doesn’t support it, use a different property.

You can send it like a string in your example then deserialize it your Service into a Date Type that supports TimeZone’s like DateTimeOffset or a Noda Time Date Type.

1 Like