Use boost to get time and format

Source: Internet
Author: User

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
    1. #include <boost/date_time/gregorian/gregorian.hpp>
    2. #define Boost_date_time_source
    3. std::string strtime = boost::gregorian::to_iso_string (\
    4. Boost::gregorian::d ay_clock::local_day ());
    5. Std::cout << strtime.c_str () << Std::endl;
[CPP]View Plaincopy
    1. #include <boost/date_time/gregorian/gregorian.hpp>
    2. #define Boost_date_time_source
    3. std::string strtime = boost::gregorian::to_iso_string (\
    4. Boost::gregorian::d ay_clock::local_day ());
    5. Std::cout << strtime.c_str () << Std::endl;

2. Output Yyyymmdd-hh:mm:ss

[CPP]View Plaincopy
  1. #include <boost/date_time/posix_time/posix_time.hpp>
  2. #define Boost_date_time_source
  3. std::string strtime = boost::p osix_time::to_iso_string (\
  4. Boost::p osix_time::second_clock::local_time ());
  5. At this time, the format of the strtime is YYYYMMDDTHHMMSS, and the date and time are separated by the capital letter T.
  6. int pos = strtime.find (' T ');
  7. Strtime.replace (pos,1,std::string ("-"));
  8. Strtime.replace (pos + 3,0,std::string (":"));
  9. Strtime.replace (pos + 6,0,std::string (":"));
  10. Std::cout << strtime.c_str () << Std::endl;
[CPP]View Plaincopy
  1. #include <boost/date_time/posix_time/posix_time.hpp>
  2. #define Boost_date_time_source
  3. std::string strtime = boost::p osix_time::to_iso_string (\
  4. Boost::p osix_time::second_clock::local_time ());
  5. At this time, the format of the strtime is YYYYMMDDTHHMMSS, and the date and time are separated by the capital letter T.
  6. int pos = strtime.find (' T ');
  7. Strtime.replace (pos,1,std::string ("-"));
  8. Strtime.replace (pos + 3,0,std::string (":"));
  9. Strtime.replace (pos + 6,0,std::string (":"));
  10. 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
  1. #include <boost/date_time/posix_time/posix_time.hpp>
  2. #include <boost/thread.hpp>
  3. #define Boost_date_time_source
  4. Boost::p osix_time::p time Time_now,time_now1;
  5. Boost::p Osix_time::millisec_posix_time_system_config::time_duration_type time_elapse;
  6. This is in microseconds, where microsec_clock can be replaced by second_clock in seconds;
  7. Time_now = boost::p osix_time::microsec_clock::universal_time ();
  8. Sleep 100 milliseconds;
  9. Boost::this_thread::sleep (boost::p osix_time::millisec (100));
  10. Time_now1 = boost::p osix_time::microsec_clock::universal_time ();
  11. Time_elapse = Time_now1-time_now;
  12. Similar to the GetTickCount, only this side gets the difference of the ticket value of 2 time, in microseconds units;
  13. int ticks = Time_elapse.ticks ();
  14. The number of seconds to get two time intervals;
  15. int sec = Time_elapse.total_seconds ();
[CPP]View Plaincopy
  1. #include <boost/date_time/posix_time/posix_time.hpp>
  2. #include <boost/thread.hpp>
  3. #define Boost_date_time_source
  4. Boost::p osix_time::p time Time_now,time_now1;
  5. Boost::p Osix_time::millisec_posix_time_system_config::time_duration_type time_elapse;
  6. This is in microseconds, where microsec_clock can be replaced by second_clock in seconds;
  7. Time_now = boost::p osix_time::microsec_clock::universal_time ();
  8. Sleep 100 milliseconds;
  9. Boost::this_thread::sleep (boost::p osix_time::millisec (100));
  10. Time_now1 = boost::p osix_time::microsec_clock::universal_time ();
  11. Time_elapse = Time_now1-time_now;
  12. Similar to the GetTickCount, only this side gets the difference of the ticket value of 2 time, in microseconds units;
  13. int ticks = Time_elapse.ticks ();
  14. Get two seconds in a time interval

Use boost to get time and format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.