Skip to content

DateTimeOffset gets converted to local (while DateTime doesn't) #105

Open
@NightProgramming

Description

@NightProgramming

I think it would make more sense if both DateTimeOffset and DateTime don't get converted to local time.

Here is my test code:

var myInstance = new MyClass()
{
    DateTimeOffsetProperty = DateTimeOffset.UtcNow,
    DateTimeProperty = DateTime.UtcNow
};

using (var store = new DataStore("data.json"))
{
    IDocumentCollection<MyClass> collection = store.GetCollection<MyClass>();
    collection.DeleteMany(_ => true); // Some cleaning

    collection.InsertOne(myInstance); // Serializes DateTimeOffset with local offset and time (incorrect!)
    // Serializes DateTime as Zulu/UTC and UTC time (correct)
}

using (var store = new DataStore("data.json"))
{
    IDocumentCollection<MyClass> collection = store.GetCollection<MyClass>();
    MyClass myInstanceDeserialized = collection.Find(_ => true).Single(); // DateTimeOffset offset and time are local (incorrect!)
    // DateTime Kind property and the time are UTC (correct)
}

string serialized = JsonConvert.SerializeObject(myInstance); // JSON.NET serializes DateTimeOffset with offset 0 and UTC time (correct)
MyClass myInstanceDeserializedByJsonDotNet = JsonConvert.DeserializeObject<MyClass>(serialized); // JSON.NET deserializes DateTimeOffset to offset 0 and UTC time (correct)

When I manually change data.json to have UTC time and offset for the DateTimeOffset then it still gets deserialized to a local DateTimeOffset.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions