Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8078535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:49:33+00:00 2026-06-05T15:49:33+00:00

Is there a succinct way, or a generally accepted way to convert from a

  • 0

Is there a succinct way, or a generally accepted way to convert from a boost::posix_time::ptime to a mongo::Date_t and back again?

Mongo to Boost

The Boost documentation seems incomplete or incorrect. It documents a function date_from_tm which constructs a date struct from a tm. However, the following example is given:

tm pt_tm;
/* snip */
ptime pt = ptime_from_tm(pt_tm);

But there is no documented function ptime_from_tm. However this header file does include that function.

So, I can at least go from mongo::Date_t to boost::posix_time::ptime:

mongo::Date_t d = ...;
std::tm t;
d.toTm(&t);
boost::posix_time::ptime pt = ptime_from_tm(t);

Boost to Mongo

I’m kind of stuck when it comes to going in the opposite direction. The MongoDB documentation is rather incomplete, and the associated header file doesn’t have many helpful comments. Basically, the Date_t struct is constructed from an unsigned long long millisecond count. I can only assume from the 1970-1-1:00:00.00 epoch.

So my current solution to go from a boost::posix_time::ptime to a mongo::Date_t is:

boost::posix_time::ptime pt = ...;
std::tm pt_tm = boost::posix_time::to_tm(pt);
std::time_t t = mktime(pt_tm);
mongo::Date_t d(t);

Of course, I can probably collapse that into one line, but it seems that the whole round trip from one date/time representation to the other and back again is becoming convoluted and messy.

Finally

Is there a better way? Does anybody with a better knowledge of both libraries and a good understanding of date/time programming know a succinct, simple way to achieve what I’m trying to achieve?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:49:35+00:00Added an answer on June 5, 2026 at 3:49 pm

    Basically, the Date_t struct is constructed from an unsigned long long millisecond count. I can only assume from the 1970-1-1:00:00.00 epoch.

    You are correct.

    I think your code is about as good as you’re going to get. You have to somehow get to a time_t-like seconds-since-the epoch, which seems to be a little complex with ptime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a succinct way to retrieve a random record from a sql server
Bottom Line Up Front Is there a succinct way that I can constrain the
Is there a more succinct way to write the below code? $myQuery = SELECT
Is there a nice succinct way to get random rows using query over? Something
Is there a succinct way (i.e. not a for loop) to create a string
Is there are more succinct or Rubyesque way of writing this: if ( variable
I was wondering if there is a cleaner (more succinct) way to do what
Is there a succinct way to represent a bounded numeric value in .NET 3.5?
Is there a succinct way to implement IEnumerable<'T> in a type whose base type
Is there a more succinct way to define a class in a namespace than

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.