[date_time] Creating a ptime from a double

View: New views
5 Messages — Rating Filter:   Alert me  

[date_time] Creating a ptime from a double

by Ryan McConnehey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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?

Ryan
_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: [date_time] Creating a ptime from a double

by Igor R :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> 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@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: [date_time] Creating a ptime from a double

by Ryan McConnehey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...
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 mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: [date_time] Creating a ptime from a double

by Igor R :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Sorry, of course, you have to multiply the fractional part by some
factor (1000000?)
_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: [date_time] Creating a ptime from a double

by Ryan McConnehey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Igor R wrote:
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.
    

Sorry, of course, you have to multiply the fractional part by some
factor (1000000?)
  
That makes more sense.  Thanks.

Ryan

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users