How to serialize UTC ticks back to Localtime/UTC time using c#driver

196 views
Skip to first unread message

ksen

unread,
Dec 15, 2011, 12:58:25 AM12/15/11
to mongodb-user
Hi

Here is my code sample, i insert this doc into mongo,

BsonDocument doc = new BsonDocument();
docSet("CreatedDateTtime", BsonDateTime.Create(DateTime.Now));

I can see it in mongoshell it is stored in UTC

But when i read back, i still see it correctly in UTC time, but when i
use ToJson() it only returns the ticks.

How to set the options to return it UTC or Local instead of ticks

var existingDoc = bsonDocs.FindOne(query);
existingDoc .ToJson()

"CreatedDate" : { "$date" : 1323823226439 }

Thanks
ksen

Andreas Jung

unread,
Dec 15, 2011, 1:11:56 AM12/15/11
to mongod...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ksen wrote:

>
> But when i read back, i still see it correctly in UTC time, but when
> i use ToJson() it only returns the ticks.
>

JSON does not provide a representation to date time..so the
serialization to seconds or milliseconds is correct.

- -aj
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQGUBAEBAgAGBQJO6Y+sAAoJEADcfz7u4AZjimULv24xP7+4TzxNSSJ+TMcYYxe8
klA3/i6O37G/oDkf3oDP+ePaARl6LfEfWohTapQ3evWOUFpbmK7F+lh2JsMUQzQ7
rUBk0Zoao/XXY0m7jTEPlARTDo8dnhBD5NdhN6CdHiXlSYgBkECy8BK2uZc1cKjO
oxuoJUC5LVcimGELiQnsmE74wJ2qmLOY5IK4j0nAPWUK8IzjfR0Du5im8QKt5eID
C8meadYyd31gS8hApgQSqbMEefTCN58kkWcVEoDnlwJAquA6WGsIJiJwSywqVK+Q
Vu3tkFqNkFguuJvE5nNN/240M7BcV7oBLisQOIQuVBZU2NOXFGjEGajfWu8StEtm
Ya0YN2jJ81d+53KZk6btbRt3W8Motzb8G3W/Oel8UyMACN+3awlul+/s7mUFeAN0
ByTw+xMRDtP0skIlhx243IZw1U7zj/GjtiHlAH/3aLJiRcNxqO8gRIeGsl1Id6oL
DFk8OZrdq7q9mDhL8Cw0lXCmSm/gq7c=
=mZTS
-----END PGP SIGNATURE-----

lists.vcf

Robert Stam

unread,
Dec 15, 2011, 1:29:41 AM12/15/11
to mongod...@googlegroups.com
I'm guessing you are using an old version of the C# driver (the
current version is 1.3.1).

Using the latest driver and the following code:

collection.RemoveAll();

var document = new BsonDocument
{
{ "_id", 1 },
{ "CreatedDate", DateTime.Now }
};
collection.Insert(document);

document = collection.FindOne();
Console.WriteLine(document.ToJson());

Results in this output from the C# program:

{ "_id" : 1, "CreatedDate" : ISODate("2011-12-15T06:22:38.359Z") }

And the same output from the mongo shell:

> db.test.find()
{ "_id" : 1, "CreatedDate" : ISODate("2011-12-15T06:22:38.359Z") }
>

You can also easily convert the UTC time stored in the database back
to local time like this:

var localTime = document["CreatedDate"].AsDateTime.ToLocalTime();

> --
> 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.
>

Reply all
Reply to author
Forward
0 new messages