Discussion:
[mongodb-user] Converting from std::chrono::system_clock::time_point to b_date and back
Ofer Reichman
2015-09-25 08:08:36 UTC
Permalink
I've spent some time trying to figure out how to store a *time_point* using
the C++ driver.
I'm posting my code for review and to help anyone who might need it (until
the driver supports it <https://jira.mongodb.org/browse/CXX-626>).

Storing:
using std::chrono::system_clock;
using std::chrono::duration_cast;
using std::chrono::milliseconds;

system_clock::time_point tp = system_clock::now(); // for example

document << "someTimePoint" << b_date{duration_cast<milliseconds>(tp.
time_since_epoch()).count()};

Retrieving:
system_clock::time_point tp(duration_cast<system_clock::duration>(
milliseconds{doc->view()["someTimePoint"].get_date().value}));

Note: Different clocks have different epochs, so this code may not work
correctly if the machine that stores the time_point is different than the
one that retrieves it.

Hope this is helpful.
Any reviews are welcome!

Ofer
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/ae1dedb3-7e91-40d6-a6fb-409440492446%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Adam Midvidy
2015-09-25 14:33:02 UTC
Permalink
Hi Ofer,

Thanks for sharing this. If you'd like to add a constructor to b_date that
takes a std::chrono::system_clock::time_point, we'd be happy to take a pull
request for it. We have an outstanding ticket for
this: https://jira.mongodb.org/browse/CXX-626.

Thanks,
Adam
Post by Ofer Reichman
I've spent some time trying to figure out how to store a *time_point*
using the C++ driver.
I'm posting my code for review and to help anyone who might need it (until
the driver supports it <https://jira.mongodb.org/browse/CXX-626>).
using std::chrono::system_clock;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
system_clock::time_point tp = system_clock::now(); // for example
document << "someTimePoint" << b_date{duration_cast<milliseconds>(tp.
time_since_epoch()).count()};
system_clock::time_point tp(duration_cast<system_clock::duration>(
milliseconds{doc->view()["someTimePoint"].get_date().value}));
Note: Different clocks have different epochs, so this code may not work
correctly if the machine that stores the time_point is different than the
one that retrieves it.
Hope this is helpful.
Any reviews are welcome!
Ofer
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/496b41b8-6f2d-4492-b20e-2237706923da%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ofer Reichman
2015-09-29 12:03:18 UTC
Permalink
Would be happy to do so, once I get some reviews. I'm not sure I'm doing it
right.
According to the manual
<http://docs.mongodb.org/manual/reference/bson-types/#document-bson-type-date>,
"BSON Date is a 64-bit integer that represents the number of milliseconds
since the Unix epoch (Jan 1, 1970)." So I think I got that right.
Is using the type *system_clock::time_point* correct?
Is the C++ code optimal? (especially the 2nd one - retrieval)
Post by Adam Midvidy
Hi Ofer,
Thanks for sharing this. If you'd like to add a constructor to b_date that
takes a std::chrono::system_clock::time_point, we'd be happy to take a pull
https://jira.mongodb.org/browse/CXX-626.
Thanks,
Adam
Post by Ofer Reichman
I've spent some time trying to figure out how to store a *time_point*
using the C++ driver.
I'm posting my code for review and to help anyone who might need it
(until the driver supports it <https://jira.mongodb.org/browse/CXX-626>).
using std::chrono::system_clock;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
system_clock::time_point tp = system_clock::now(); // for example
document << "someTimePoint" << b_date{duration_cast<milliseconds>(tp.
time_since_epoch()).count()};
system_clock::time_point tp(duration_cast<system_clock::duration>(
milliseconds{doc->view()["someTimePoint"].get_date().value}));
Note: Different clocks have different epochs, so this code may not work
correctly if the machine that stores the time_point is different than the
one that retrieves it.
Hope this is helpful.
Any reviews are welcome!
Ofer
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/023acd22-b3dd-4b75-89d0-882bc1f3a52f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ofer Reichman
2015-10-08 16:37:21 UTC
Permalink
done. added constructor and casting operator to b_date.
Post by Ofer Reichman
Would be happy to do so, once I get some reviews. I'm not sure I'm doing
it right.
According to the manual
<http://docs.mongodb.org/manual/reference/bson-types/#document-bson-type-date>,
"BSON Date is a 64-bit integer that represents the number of milliseconds
since the Unix epoch (Jan 1, 1970)." So I think I got that right.
Is using the type *system_clock::time_point* correct?
Is the C++ code optimal? (especially the 2nd one - retrieval)
Post by Adam Midvidy
Hi Ofer,
Thanks for sharing this. If you'd like to add a constructor to b_date
that takes a std::chrono::system_clock::time_point, we'd be happy to take a
https://jira.mongodb.org/browse/CXX-626.
Thanks,
Adam
Post by Ofer Reichman
I've spent some time trying to figure out how to store a *time_point*
using the C++ driver.
I'm posting my code for review and to help anyone who might need it
(until the driver supports it <https://jira.mongodb.org/browse/CXX-626>
).
using std::chrono::system_clock;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
system_clock::time_point tp = system_clock::now(); // for example
document << "someTimePoint" << b_date{duration_cast<milliseconds>(tp.
time_since_epoch()).count()};
system_clock::time_point tp(duration_cast<system_clock::duration>(
milliseconds{doc->view()["someTimePoint"].get_date().value}));
Note: Different clocks have different epochs, so this code may not work
correctly if the machine that stores the time_point is different than the
one that retrieves it.
Hope this is helpful.
Any reviews are welcome!
Ofer
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/cf2dd165-68f6-43c3-87ec-7435a7fc4ccd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...