Boost logo

Boost Users :

Subject: Re: [Boost-users] [date_time] Creating a ptime from a double
From: Ryan McConnehey (mccorywork_at_[hidden])
Date: 2009-11-03 11:23:38


Igor R wrote:
A lot of the constructors of ptime deals with getting the current system time or time structures.  I have a double with the seconds and fractional microseconds from midnight January 1, 1970.  What is the best way to create a ptime object from this double?
    


#include <boost/date_time/posix_time/posix_time.hpp>
#include <math.h>
namespace pt = boost::posix_time;

int main()
{
	double d = 1000.1;
	double sec;
	double mksec = std::modf(d, &sec);
	pt::ptime t = pt::from_time_t(static_cast<int>(sec)) +
pt::microseconds(static_cast<int>(mksec));
}
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
The documentation for modf states that d == sec + mksec.  This means that mksec is a fractional number and not a whole number.  The pt::microseconds expects whole numbers of microseconds.  If I static_cast the fractional number I would get at most 1 when it rounds.  Isn't this correct?

Ryan

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net