linux - How to print current time (with milliseconds) using C++ / C++11 -


currently use code

string now() {     time_t t = time(0);     char buffer[9] = {0};      strftime(buffer, 9, "%h:%m:%s", localtime(&t));     return string(buffer); } 

to format time. need add milliseconds, output has format: 16:56:12.321

you can use boost's posix time.

you can use boost::posix_time::microsec_clock::local_time() current time microseconds-resolution clock:

boost::posix_time::ptime = boost::posix_time::microsec_clock::local_time(); 

then can compute time offset in current day (since duration output in form <hours>:<minutes>:<seconds>.<milliseconds>, i'm assuming calculated current day offset; if not, feel free use starting point duration/time interval):

boost::posix_time::time_duration td = now.time_of_day(); 

then can use .hours(), .minutes(), .seconds() accessors corresponding values.
unfortunately, there doesn't seem .milliseconds() accessor, there .total_milliseconds() one; can little subtraction math remaining milliseconds formatted in string.

then can use sprintf() (or sprintf()_s if interested in non-portable vc++-only code) format fields raw char buffer, , safely wrap raw c string buffer robust convenient std::string instance.

see commented code below further details.

output in console like:

11:43:52.276


sample code:

///////////////////////////////////////////////////////////////////////////////  #include <stdio.h>      // sprintf()  #include <iostream>     // console output #include <string>       // std::string  #include <boost/date_time/posix_time/posix_time.hpp>   //----------------------------------------------------------------------------- // format current time (calculated offset in current day) in form: // //     "hh:mm:ss.sss" (where "sss" milliseconds) //----------------------------------------------------------------------------- std::string now_str() {     // current time clock, using microseconds resolution     const boost::posix_time::ptime =          boost::posix_time::microsec_clock::local_time();      // time offset in current day     const boost::posix_time::time_duration td = now.time_of_day();      //     // extract hours, minutes, seconds , milliseconds.     //     // since there no direct accessor ".milliseconds()",     // milliseconds computed _by difference_ between total milliseconds     // (for there accessor), , hours/minutes/seconds     // values fetched.     //     const long hours        = td.hours();     const long minutes      = td.minutes();     const long seconds      = td.seconds();     const long milliseconds = td.total_milliseconds() -                               ((hours * 3600 + minutes * 60 + seconds) * 1000);      //     // format this:     //     //      hh:mm:ss.sss     //     // e.g. 02:15:40:321     //     //      ^          ^     //      |          |     //      123456789*12     //      ---------10-     --> 12 chars + \0 --> 13 chars should suffice     //       //      char buf[40];     sprintf(buf, "%02ld:%02ld:%02ld.%03ld",          hours, minutes, seconds, milliseconds);      return buf; }  int main() {     std::cout << now_str() << '\n';     }  /////////////////////////////////////////////////////////////////////////////// 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -