StudyNPractice
StudyNPractice

Reputation: 75

How to get the sub seconds of current time

const boost::posix_time::ptime now;
boost::posix_time::second_clock::local_time();

now.date().year();
now.date().month();
now.date().day();

now.time_of_day().hours(); 
now.time_of_day().minutes();
now.time_of_day().seconds();

I figured out how to get years, months, days, hours, minutes, and seconds using boost::posix_time::ptime.

But I can't get sub seconds of ptime. Is there any way to get milliseconds, microseconds, or nanoseconds?

Thanks.

Upvotes: 0

Views: 190

Answers (1)

rafix07
rafix07

Reputation: 20969

You can do it by substracting scaled total number of seconds from total_milliseconds/microseconds:

To get ms use:

time_duration td;
td.total_milliseconds() - (td.total_seconds()*1000)

To get us use:

time_duration td;
td.total_microseconds() - (td.total_seconds()*1000000)

You need to change type of clock when calling local_time from second_clock (which is based on seconds precision) to microsec_clock to see ms/us different from 0.

DEMO

Upvotes: 1

Related Questions