3

I have read that Date related types are not supported in DocumentDB and are considered as string. So i have saved one of my properties like 2017-01-13T07:30:00+05:30 ,when i try to read it locally it is working,but once i host my service to azure, it is getting converted to UTC (2017-01-13T02:30:00+00:00). What could be the reason, i thought we can save and read them as string. Is the DateTimeOffset serialization be the issue?

UPDATE

I observed one thing, when i changed the local system timezone and ran it the time zone is getting converted to the new timezone. So i have a doubt when we are querying data from DocumentDB it is converting time to the format where the code is being run

2 Answers 2

1

Well well well. This appears to be an issue with how DocumentDb uses JsonConvert to deserialize DateTimeOffset strings in standard ISO 8601 format.

Basically they'll convert the time zone to the local system time zone, and ignore the offset information in the string. Thanks. That's kind of the point of DateTimeOffset's. For an Azure worker, that's going to be UTC. Of course testing locally its using your local time zone and works fine (if the original data had the same offset as you).

Setting the JsonConvert.DefaultSettings also has no effect, so they must be setting the settings explicitly when deserializing. Nothing from MS, I've raised a ticket on GITHUB here.

What baffles me is why no one else is really complaining? This is a huge, massive bug with documentDb. We use it to store about 80 million events; date is rather important, especially when you have daylight saving or cross time zone. Completely maddening, but typical Microsoft. And I'm a fan boi!

Using settings like this should fix it. Since we can only do this via DefaultSettings (which are overridden by explicit settings in their SDK) there's not much we can do. You might have luck using a JsonConverter, which I'm also investigating.

new JsonSerializerSettings()
        {
            DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind,
            DateParseHandling = DateParseHandling.DateTimeOffset
        }
2
  • i raised an issue for Microsoft in Git and they admitted it is a bug . So they recommended me to save in string and convert to DateTimeOffset when necessarry. But i saved my datetime and offset separately as two properties and concatenated them before sending data to client. Git ticket Apr 5, 2017 at 17:53
  • Thanks. I'm a bit surprised this configuration wasn't available easily in the documentation, I was kinda baffled the values I got from CosmosDB had an 1 hour offset...
    – ken2k
    Nov 6, 2018 at 10:16
0

With specific regard to DateTime, the best advice I can give is to use

CultureInfo.InvariantCulture

for ParseExact and ToString when you have an exact format in mind.

var d = DateTime.Now;
var s1 = d.ToString(CultureInfo.InvariantCulture);   // "05/21/2014 22:09:28"
var s2 = d.ToString(new CultureInfo("en-US"));       // "5/21/2014 10:09:28 PM"
6
  • I am receiving data from app with offset which i save to document db. The issue comes when i try to query data from document db Jan 16, 2017 at 6:18
  • @ArjunMenon refer here stackoverflow.com/questions/31973710/…
    – MMM
    Jan 16, 2017 at 6:21
  • 1
    I observed one thing, when i changed the local system timezone and ran it the time zone is getting converted to the new timezone Jan 16, 2017 at 8:46
  • 1
    This seems to be a bug In the SDK and I'll investigate. Can you specify the API you are using it to read? You can store the offset as a separate field as a workaround. I believe it's stored correctly in DocumentDB but there is an issue while deserializing it as a response. Jan 16, 2017 at 23:03
  • We are using.NET DocumentClient object to fetch and do DocumentDB operations Jan 18, 2017 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.