Using boost to get the current time is quick and easy, and you don't have to think about cross-platform issues.
1. Output YYYYMMDD
[CPP]View Plaincopy
- #include <boost/date_time/gregorian/gregorian.hpp>
- #define Boost_date_time_source
- std::string strtime = boost::gregorian::to_iso_string (\
- Boost::gregorian::d ay_clock::local_day ());
- Std::cout << strtime.c_str () << Std::endl;
[CPP]View Plaincopy
- #include <boost/date_time/gregorian/gregorian.hpp>
- #define Boost_date_time_source
- std::string strtime = boost::gregorian::to_iso_string (\
- Boost::gregorian::d ay_clock::local_day ());
- Std::cout << strtime.c_str () << Std::endl;
2. Output Yyyymmdd-hh:mm:ss
[CPP]View Plaincopy
- #include <boost/date_time/posix_time/posix_time.hpp>
- #define Boost_date_time_source
- std::string strtime = boost::p osix_time::to_iso_string (\
- Boost::p osix_time::second_clock::local_time ());
- At this time, the format of the strtime is YYYYMMDDTHHMMSS, and the date and time are separated by the capital letter T.
- int pos = strtime.find (' T ');
- Strtime.replace (pos,1,std::string ("-"));
- Strtime.replace (pos + 3,0,std::string (":"));
- Strtime.replace (pos + 6,0,std::string (":"));
- Std::cout << strtime.c_str () << Std::endl;
[CPP]View Plaincopy
- #include <boost/date_time/posix_time/posix_time.hpp>
- #define Boost_date_time_source
- std::string strtime = boost::p osix_time::to_iso_string (\
- Boost::p osix_time::second_clock::local_time ());
- At this time, the format of the strtime is YYYYMMDDTHHMMSS, and the date and time are separated by the capital letter T.
- int pos = strtime.find (' T ');
- Strtime.replace (pos,1,std::string ("-"));
- Strtime.replace (pos + 3,0,std::string (":"));
- Strtime.replace (pos + 6,0,std::string (":"));
- Std::cout << strtime.c_str () << Std::endl;
3. Calculate the time interval. The ability to calculate time intervals in boost is a lot more powerful, and I'm just listing what I'm using right now.
[CPP]View Plaincopy
- #include <boost/date_time/posix_time/posix_time.hpp>
- #include <boost/thread.hpp>
- #define Boost_date_time_source
- Boost::p osix_time::p time Time_now,time_now1;
- Boost::p Osix_time::millisec_posix_time_system_config::time_duration_type time_elapse;
- This is in microseconds, where microsec_clock can be replaced by second_clock in seconds;
- Time_now = boost::p osix_time::microsec_clock::universal_time ();
- Sleep 100 milliseconds;
- Boost::this_thread::sleep (boost::p osix_time::millisec (100));
- Time_now1 = boost::p osix_time::microsec_clock::universal_time ();
- Time_elapse = Time_now1-time_now;
- Similar to the GetTickCount, only this side gets the difference of the ticket value of 2 time, in microseconds units;
- int ticks = Time_elapse.ticks ();
- The number of seconds to get two time intervals;
- int sec = Time_elapse.total_seconds ();
[CPP]View Plaincopy
- #include <boost/date_time/posix_time/posix_time.hpp>
- #include <boost/thread.hpp>
- #define Boost_date_time_source
- Boost::p osix_time::p time Time_now,time_now1;
- Boost::p Osix_time::millisec_posix_time_system_config::time_duration_type time_elapse;
- This is in microseconds, where microsec_clock can be replaced by second_clock in seconds;
- Time_now = boost::p osix_time::microsec_clock::universal_time ();
- Sleep 100 milliseconds;
- Boost::this_thread::sleep (boost::p osix_time::millisec (100));
- Time_now1 = boost::p osix_time::microsec_clock::universal_time ();
- Time_elapse = Time_now1-time_now;
- Similar to the GetTickCount, only this side gets the difference of the ticket value of 2 time, in microseconds units;
- int ticks = Time_elapse.ticks ();
- Get two seconds in a time interval
Use boost to get time and format