Pepito Fernandez
Pepito Fernandez

Reputation: 2440

EF Core Cosmos DB persists DateTime as Local instead of UTC

I have an object, let's call it MyMainAggregate for simplicity. This object has a record called TimeSlot which has two members, one DateTime called Start and one TimeSpan called Duration.

The DateTime for the TimeSlot object is set to Utc, and my tests confirm that the .Kind is Utc.

However, when I check CosmosDb document, it displays the DateTime as the Local version, however, it has the Z attached to it. For example, "Start": "2022-12-09T11:00:00Z". 11:00:00 is Local. The correct time should've looked like 16:00:00Z.

I can't figure out what the heck is going on and testing DbContext is a royal nightmare. I am thinking, as the last line of defense, to create an Interceptor and double check again that the date is correct, but it seems like an overkill when I am already setting the Date to Utc.

Has anyone ran into this same issue?

[UPDATE]

I ran the Cosmos Emulator locally and it is working as expected, Local times are properly being saved as Utc by using a Converter<DateTime, string>. However, when I pushed the code to the dev environment, I still see Local time with an attached 'Z'.

Upvotes: 0

Views: 261

Answers (1)

Pepito Fernandez
Pepito Fernandez

Reputation: 2440

Welp! The issue was pretty straightforward. This is why you should never assume anything.

The code is working as expected. What I never took in consideration is that, effectively, my pods (k8s) were using UTC as the local time. So, rightfully so, 11:00:00Z was correct, because I was setting an appointment to 11AM, at UTC.

Always check what's the Local time of your services/pods.

Upvotes: 2

Related Questions