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

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

Open
NightProgramming opened this issue Nov 19, 2024 · 0 comments
Open

Comments

@NightProgramming
Copy link

NightProgramming commented Nov 19, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant