question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can I override default Bson datetime reader to read datetime as utc?

See original GitHub issue

This solution doesn’t work for me.

            BsonMapper.Global.RegisterType<DateTime>(
                dateTime => new BsonValue(dateTime.ToMillisUnixTimestamp()),
                bsonValue => DateTimeExtensions.MillisUnixTimestampToDateTime(bsonValue.AsInt64)
            );

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mbdavidcommented, Oct 9, 2017

It´s better use Serialize/Deserialize method in this case:

Deserialize Func is Func<BsonValue, BsonMapper, object> Serialize Func is Func<BsonValue, BsonMapper, object>

BsonMapper.Global.ResolveMember((type, memberInfo, member) =>
{
    if(member.DataType == typeof(DateTime))
    {
        member.Deserialize = (v, m) => v.AsDateTime.ToMillisUnixTimestamp();
        member.Setter = (o, m) => DateTimeExtensions.MillisUnixTimestampToDateTime(Convert.ToInt64(o));
    } 
});

This will override all DateTime types to convert to Int64 before serialize into Bson.

1reaction
mbdavidcommented, Sep 29, 2017

Hi @Try4W, custom data type are not supported in basic already implemented internal types. But you can write your own mapper getter/setter function (implement ResolveMember method).

BsonMapper.Global.ResolveMember((type, memberInfo, member) =>
{
    if(member.DataType == typeof(DateTime))
    {
        member.Getter = ...
        member.Setter = ...
    } 
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Storing Utc and Local datetime in Mongo
I have a Mongo C# implementation that stores datetime as UTC. MongoDB.Bson.Serialization.Options.DateTimeSerializationOptions options = MongoDB.
Read more >
Datetimes and Timezones - PyMongo 4.4.1 documentation
Reading Time# ... As previously mentioned, by default all datetime.datetime objects returned by PyMongo will be naive but reflect UTC (i.e. the time...
Read more >
Mongodb date without timezone and Spring Data - Life in IDE
MongoDB stores by default all dates with time and timezone. ... When you read the date back in the same system (working in...
Read more >
Working with dates and times in MongoDB - Prisma
MongoDB will store date and time information using UTC internally, but can easily convert to other timezones at time of retrieval as needed....
Read more >
BSON Types
BSON is a binary serialization format used to store documents and make ... The official BSON specification refers to the BSON Date type...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found