Use boost to get and format the time

Source: Internet
Author: User

Http://blog.csdn.net/zhaodan19861107/article/details/7428757

Using boost to get the current time is convenient and quick, and you do not need to consider cross-platform issues.

1. Output yyyymmdd

[CPP]
View plaincopy

  1. # Include <boost/date_time/Gregorian. HPP>
  2. # Define boost_date_time_source
  3. STD: String strtime = boost: Gregorian: to_iso_string (\
  4. Boost: Gregorian: day_clock: local_day ());
  5. STD: cout <strtime. c_str () <STD: Endl;
[CPP]
View plaincopy

  1. # Include <boost/date_time/Gregorian. HPP>
  2. # Define boost_date_time_source
  3. STD: String strtime = boost: Gregorian: to_iso_string (\
  4. Boost: Gregorian: day_clock: local_day ());
  5. STD: cout <strtime. c_str () <STD: Endl;

2. YYYYMMDD-HH output: 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: posix_time: to_iso_string (\
  4. Boost: posix_time: second_clock: local_time ());
  5. // The time format in strtime is yyyymmddthhmmss. the date and time are separated by uppercase letters 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: posix_time: to_iso_string (\
  4. Boost: posix_time: second_clock: local_time ());
  5. // The time format in strtime is yyyymmddthhmmss. the date and time are separated by uppercase letters 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 interval. There are many powerful functions for calculating the time interval in boost. I only list what I currently use.

[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: posix_time: ptime time_now, time_now1;
  5. Boost: posix_time: millisec_posix_time_system_config: time_duration_type time_elapse;
  6. // Here, the unit is microsecond. Here, you can replace microsec_clock with second_clock in seconds;
  7. Time_now = boost: posix_time: microsec_clock: universal_time ();
  8. // Sleep 100 ms;
  9. Boost: this_thread: Sleep (boost: posix_time: millisec (100 ));
  10. Time_now1 = boost: posix_time: microsec_clock: universal_time ();
  11. Time_elapse = time_now1-time_now;
  12. // Similar to gettickcount, only the difference between the two ticket values is obtained here, in microseconds;
  13. Int ticks = time_elapse.ticks ();
  14. // Obtain the number of seconds between the two 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: posix_time: ptime time_now, time_now1;
  5. Boost: posix_time: millisec_posix_time_system_config: time_duration_type time_elapse;
  6. // Here, the unit is microsecond. Here, you can replace microsec_clock with second_clock in seconds;
  7. Time_now = boost: posix_time: microsec_clock: universal_time ();
  8. // Sleep 100 ms;
  9. Boost: this_thread: Sleep (boost: posix_time: millisec (100 ));
  10. Time_now1 = boost: posix_time: microsec_clock: universal_time ();
  11. Time_elapse = time_now1-time_now;
  12. // Similar to gettickcount, only the difference between the two ticket values is obtained here, in microseconds;
  13. Int ticks = time_elapse.ticks ();
  14. // Obtain the number of seconds between the two intervals;
  15. Int sec = time_elapse.total_seconds ();
 

 

From: http://blog.csdn.net/morebread/article/details/6742957

Using boost to get the current time is convenient and quick, and you do not need to consider cross-platform issues.

1. Output yyyymmdd

[CPP]
View plaincopy

  1. # Include <boost/date_time/Gregorian. HPP>
  2. # Define boost_date_time_source
  3. STD: String strtime = boost: Gregorian: to_iso_string (\
  4. Boost: Gregorian: day_clock: local_day ());
  5. STD: cout <strtime. c_str () <STD: Endl;
[CPP]
View plaincopy

  1. # Include <boost/date_time/Gregorian. HPP>
  2. # Define boost_date_time_source
  3. STD: String strtime = boost: Gregorian: to_iso_string (\
  4. Boost: Gregorian: day_clock: local_day ());
  5. STD: cout <strtime. c_str () <STD: Endl;

2. YYYYMMDD-HH output: 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: posix_time: to_iso_string (\
  4. Boost: posix_time: second_clock: local_time ());
  5. // The time format in strtime is yyyymmddthhmmss. the date and time are separated by uppercase letters 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: posix_time: to_iso_string (\
  4. Boost: posix_time: second_clock: local_time ());
  5. // The time format in strtime is yyyymmddthhmmss. the date and time are separated by uppercase letters 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 interval. There are many powerful functions for calculating the time interval in boost. I only list what I currently use.

[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: posix_time: ptime time_now, time_now1;
  5. Boost: posix_time: millisec_posix_time_system_config: time_duration_type time_elapse;
  6. // Here, the unit is microsecond. Here, you can replace microsec_clock with second_clock in seconds;
  7. Time_now = boost: posix_time: microsec_clock: universal_time ();
  8. // Sleep 100 ms;
  9. Boost: this_thread: Sleep (boost: posix_time: millisec (100 ));
  10. Time_now1 = boost: posix_time: microsec_clock: universal_time ();
  11. Time_elapse = time_now1-time_now;
  12. // Similar to gettickcount, only the difference between the two ticket values is obtained here, in microseconds;
  13. Int ticks = time_elapse.ticks ();
  14. // Obtain the number of seconds between the two 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: posix_time: ptime time_now, time_now1;
  5. Boost: posix_time: millisec_posix_time_system_config: time_duration_type time_elapse;
  6. // Here, the unit is microsecond. Here, you can replace microsec_clock with second_clock in seconds;
  7. Time_now = boost: posix_time: microsec_clock: universal_time ();
  8. // Sleep 100 ms;
  9. Boost: this_thread: Sleep (boost: posix_time: millisec (100 ));
  10. Time_now1 = boost: posix_time: microsec_clock: universal_time ();
  11. Time_elapse = time_now1-time_now;
  12. // Similar to gettickcount, only the difference between the two ticket values is obtained here, in microseconds;
  13. Int ticks = time_elapse.ticks ();
  14. // Obtain the number of seconds between the two intervals;
  15. Int sec = time_elapse.total_seconds ();
 

 

From: http://blog.csdn.net/morebread/article/details/6742957

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.