Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cosmos DateTime Serialization #35189

Closed
cliedeman opened this issue Nov 23, 2024 · 2 comments
Closed

Cosmos DateTime Serialization #35189

cliedeman opened this issue Nov 23, 2024 · 2 comments
Labels
area-cosmos closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@cliedeman
Copy link

cliedeman commented Nov 23, 2024

File a bug

Cosmos Provider DateTime Serialization does not serialize in the expected format: yyyy-MM-ddTHH:mm:ss.fffffffZ

Include your code

Create any model that contains a DateTime and save it to cosmos

class Document {
  public Guid Id { get; set; }

  public DateTime Timestamp {get;set;}
}
{
    "id": "cd081158-d474-43aa-b115-150f1d162e1d",
    "_ts": 1732385644,
    "$type": "Document",
    "_etag": "d401e28f-58dd-43df-895f-cf02b9cc2e18",
    "_self": "dbs/cosmosdb/colls/Document/docs/cd081158-d474-43aa-b115-150f1d162e1d/",
    "Timestamp": "2021-01-01T02:00:00+02:00",
}

The serialized timestamp does not follow the cosmos recommendations:

https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/query/working-with-dates#storing-datetimes.

Related to Azure/azure-cosmos-dotnet-v3#4904 but I dont believe the ef core cosmos provider uses Json Serialization

Include provider and version information

EF Core version:
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 6.0)
Operating system:
IDE: (e.g. Visual Studio 2022 17.4)

@roji
Copy link
Member

roji commented Nov 23, 2024

In what way is the serialization not in the expected format? Are you referring to the lack of the .fffffff component? If so, then AFAIK ISO9601 allows omitting it when it's zero; try setting your DateTime to a timestamp which contains a sub-millisecond component, and you should see it serialized.

Otherwise, if I haven't understood what you're saying, please provide more details/context.

@cliedeman
Copy link
Author

Hi @roji I figured out my issue.

The DateTime kind was local and not UTC. After changing it to UTC it was saved in the expected format.

The other issue is that the date in question is not valid according to the dotnet date format

DateTimeOffset.ParseExact(
            "2021-01-01T02:00:00+02:00",
            "yyyy-MM-ddTHH:mm:ssZ",
            CultureInfo.InvariantCulture);

fails with

String '2021-01-01T02:00:00+02:00' was not recognized as a valid DateTime.

where as

DateTimeOffset.ParseExact(
            "2021-01-01T02:00:00Z",
            "yyyy-MM-ddTHH:mm:ssZ",
            CultureInfo.InvariantCulture);

passes.

Atleast to my understanding if the Z is present in the format only Z will be accepted and not an offset

@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Nov 24, 2024
@roji roji added the closed-no-further-action The issue is closed and no further action is planned. label Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-cosmos closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants