Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local time with milliseconds

Tags:

c++

time

boost

how can I get current time with library boost. I can do this:

ptime now = boost::posix_timesecond_clock::local_time();
tm d_tm = to_tm(now);

But the last time unit of tm structure is second and I need in millisecond. Can I get current time with milliseconds?

like image 329
Max Frai Avatar asked Jun 28 '10 17:06

Max Frai


People also ask

How do you convert UTC time to milliseconds?

The getUTCMilliseconds() method is used to get the milliseconds from a given date according to universal time (UTC). The value return by getUTCMilliseconds() method is a number between 0 and 999.

How do you convert epoch time to milliseconds?

Convert from human-readable date to epoch long epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds.

What is current time Millis?

currentTimeMillis() method returns the current time in milliseconds. The unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.


1 Answers

look at boost::posix_time::microsec_clock::local_time()

#include <boost/date_time/posix_time/posix_time_types.hpp>

#include <iostream>

int
main()
{
    boost::posix_time::ptime time = boost::posix_time::microsec_clock::local_time();
    boost::posix_time::time_duration duration( time.time_of_day() );
    std::cout << duration.total_milliseconds() << std::endl;

    return 0;
}
like image 56
Sam Miller Avatar answered Oct 14 '22 16:10

Sam Miller