BSON DateTime Bug?

1,998 views
Skip to first unread message

SeveQ

unread,
Jan 30, 2012, 8:32:29 PM1/30/12
to mongodb-user
Hello MongoDB users,

I haven't been able to find a way to report a bug inside the BSON
serializer, yet. So I hope this is the right place or at least one of
the legit places to do so. Maybe someone could point me to the right
address if not.

Well, I've run into a problem with the serializer that seems like
being due to a wrong DateTime serialization. The serialized date is
always a day off from the original date (one day earlier -> would I
serialize date of today, it'd store yesterday in the record).

Does anyone else experience this problem?

Thanks a lot

Cheers,
Hendrik

Robert Stam

unread,
Jan 30, 2012, 9:01:05 PM1/30/12
to mongod...@googlegroups.com
What driver are you using?

Keep in mind that DateTime values in BSON are always stored in UTC, so there might be a conversion to UTC taking place.


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


SeveQ

unread,
Jan 31, 2012, 4:28:54 AM1/31/12
to mongodb-user
Hi,

I'm using the default C# driver provided by the MongoDB developers.
And it's definitely a UTC problem. I store a DateTime that only
contains a date, time is zero. The driver subtracts two hours from it
and hence, regarding only the date, it's a whole day earlier.
Unfortunately it doesn't add these subtracted two hours again when
deserializing. I suppose it should?

Thanks

Hendrik

On Jan 31, 3:01 am, Robert Stam <rob...@10gen.com> wrote:
> What driver are you using?
>
> Keep in mind that DateTime values in BSON are always stored in UTC, so
> there might be a conversion to UTC taking place.
>

Nat

unread,
Jan 31, 2012, 5:28:00 AM1/31/12
to mongod...@googlegroups.com

Robert Stam

unread,
Jan 31, 2012, 9:23:51 AM1/31/12
to mongod...@googlegroups.com
I recommend that you always use UTC values in your data model and only convert to and from LocalTime at the point of display to the user.

However, if you don't want to do it that way, you can annotate your class variables to let the driver know what you want. For example:

    public class Test
    {
        public ObjectId Id { get; set; }
        [BsonDateTimeOptions(DateOnly = true)]
        public DateTime DateOnly { get; set; }
        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime DateAndTime { get; set; }
    }

Since .NET doesn't have a Date class, adding the [BsonDateTimeOptions(DateOnly = true)] tells the driver that you are only using the Date portion of the DateTime value (the driver checks that the TimeOfDay component is zero). Using [BsonDateTimeOptions(Kind = DateTimeKind.Local)] tells the driver to convert the DateTime from UTC (what's stored in the database) back to LocalTime when you read the document back from the database. Note that it converts to the LocalTime of the current machine, which may or may not be the same as the LocalTime of the machine where the document was first created.

Keep in mind that server machines are often run on UTC time (a recommended practice), so on server machines configured that way LocalTime will be equal to UTC anyway.

SeveQ

unread,
Jan 31, 2012, 11:11:15 AM1/31/12
to mongodb-user
Ah, I see! You've opened my eyes! That'll help. Thanks a lot.
> >http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutor...
Reply all
Reply to author
Forward
0 new messages