Why any Time Zone is automatically converting to Local Time Zone when retrieving from Couchbase SDK

I facing an issue when retrieving date and time along with time zone in C# using couchbase SDK. Please find the details below.

This is the couchbase Insert Query which is having different country date and time along with time zone

Upsert Query
UPSERT INTO DtTest (KEY,VALUE)
VALUES ( “TZ:Dates”, {
“Tokyo” : “2020-06-18T15:26:00+09:00”,
“Beijing” : “2020-06-18T14:26:00+08:00”,
“Moscow” : “2020-06-18T09:26:00+03:00”,
“Paris” : “2020-06-18T08:26:00+02:00”,
“LosAngeles” : “2020-06-17T23:26:00-07:00”
})

This is couchbase Select Query, where we can check by using console

** Select Query**
select * from DtTest use keys “TZ:Dates”

Couchbase Select Query Result in console and we didn’t see any issue here
{
“Beijing”: “2020-06-18T14:26:00+08:00”,
“LosAngeles”: “2020-06-17T23:26:00-07:00”,
“Moscow”: “2020-06-18T09:26:00+03:00”,
“Paris”: “2020-06-18T08:26:00+02:00”,
“Tokyo”: “2020-06-18T15:26:00+09:00”
}

When we are trying to retrieving from Couchbase SDK in C#

** Couchbase SDK code in C#**
_bucket.Get<dynamic>("TZ:Dates");

Couchbase SDK Result in C# which is converting the timezone to my system local timezone
{
“Beijing”: “2020-06-18T11:56:00+05:30”,
“LosAngeles”: “2020-06-18T11:56:00+05:30”,
“Moscow”: “2020-06-18T11:56:00+05:30”,
“Paris”: “2020-06-18T11:56:00+05:30”,
“Tokyo”: “2020-06-18T11:56:00+05:30”
}

Note : Please help me on this to retrieve exact time zones how we are getting from Couchbase Console

This looks like issue with serialization.

@balakrishna_ghanta

It’s a known limitation of .NET and the DateTime type that it doesn’t handle time zones very well. It actually can’t represent anything but UTC or Local time, no other time zones.

My recommendation would be to use the NodaTime library, which has much richer support. https://www.nuget.org/packages/NodaTime/. This can be combined with https://www.nuget.org/packages/NodaTime.Serialization.JsonNet/ to help support serialization to/from JSON using Newtonsoft.Json (the default serializer used by the Couchbase SDK).

Hopefully that helps.

3 Likes

Also, to be fair you can also try using the System.DateTimeOffset type, which is better with time zones than System.DateTime. But still nowhere near as powerful or flexible as NodaTime.

3 Likes