Anonymous
Anonymous

Reputation: 4767

C++ Boost: Get time with milliseconds

At the moment I am getting the date and time in the following way:

 std::string isoString = boost::posix_time::to_iso_string(boost::posix_time::second_clock::universal_time());
 std::string date = isoString.substr(0,8);
 std::string time = isoString.substr(9,16); 

Problem: The milliseconds are missing and I need this information. How can I obtain the time with milliseconds?

Upvotes: 1

Views: 3168

Answers (3)

Mikhail
Mikhail

Reputation: 8038

Why not C++11?

long long timestamp()
{
    return chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();
}

Upvotes: 1

fatihk
fatihk

Reputation: 7929

You can use boost::posix_time::microsec_clock :

 std::string isoString = boost::posix_time::to_iso_string(boost::posix_time::microsec_clock::universal_time());
 std::string date = isoString.substr(0,8);
 std::string time = isoString.substr(9,20); 

Upvotes: 2

ForEveR
ForEveR

Reputation: 55897

You should use boost::posix_time::microsec_clock

Upvotes: 1

Related Questions