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
- # Include <boost/date_time/Gregorian. HPP>
- # Define boost_date_time_source
- STD: String strtime = boost: Gregorian: to_iso_string (\
- Boost: Gregorian: day_clock: local_day ());
- STD: cout <strtime. c_str () <STD: Endl;
[CPP]
View plaincopy
- # Include <boost/date_time/Gregorian. HPP>
- # Define boost_date_time_source
- STD: String strtime = boost: Gregorian: to_iso_string (\
- Boost: Gregorian: day_clock: local_day ());
- STD: cout <strtime. c_str () <STD: Endl;
2. YYYYMMDD-HH output: mm: SS
[CPP]
View plaincopy
- # Include <boost/date_time/posix_time/posix_time.hpp>
- # Define boost_date_time_source
- STD: String strtime = boost: posix_time: to_iso_string (\
- Boost: posix_time: second_clock: local_time ());
- // The time format in strtime is yyyymmddthhmmss. the date and time are separated by uppercase letters 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: posix_time: to_iso_string (\
- Boost: posix_time: second_clock: local_time ());
- // The time format in strtime is yyyymmddthhmmss. the date and time are separated by uppercase letters 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 interval. There are many powerful functions for calculating the time interval in boost. I only list what I currently use.
[CPP]
View plaincopy
- # Include <boost/date_time/posix_time/posix_time.hpp>
- # Include <boost/thread. HPP>
- # Define boost_date_time_source
- Boost: posix_time: ptime time_now, time_now1;
- Boost: posix_time: millisec_posix_time_system_config: time_duration_type time_elapse;
- // Here, the unit is microsecond. Here, you can replace microsec_clock with second_clock in seconds;
- Time_now = boost: posix_time: microsec_clock: universal_time ();
- // Sleep 100 ms;
- Boost: this_thread: Sleep (boost: posix_time: millisec (100 ));
- Time_now1 = boost: posix_time: microsec_clock: universal_time ();
- Time_elapse = time_now1-time_now;
- // Similar to gettickcount, only the difference between the two ticket values is obtained here, in microseconds;
- Int ticks = time_elapse.ticks ();
- // Obtain the number of seconds between the two 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: posix_time: ptime time_now, time_now1;
- Boost: posix_time: millisec_posix_time_system_config: time_duration_type time_elapse;
- // Here, the unit is microsecond. Here, you can replace microsec_clock with second_clock in seconds;
- Time_now = boost: posix_time: microsec_clock: universal_time ();
- // Sleep 100 ms;
- Boost: this_thread: Sleep (boost: posix_time: millisec (100 ));
- Time_now1 = boost: posix_time: microsec_clock: universal_time ();
- Time_elapse = time_now1-time_now;
- // Similar to gettickcount, only the difference between the two ticket values is obtained here, in microseconds;
- Int ticks = time_elapse.ticks ();
- // Obtain the number of seconds between the two intervals;
- 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
- # Include <boost/date_time/Gregorian. HPP>
- # Define boost_date_time_source
- STD: String strtime = boost: Gregorian: to_iso_string (\
- Boost: Gregorian: day_clock: local_day ());
- STD: cout <strtime. c_str () <STD: Endl;
[CPP]
View plaincopy
- # Include <boost/date_time/Gregorian. HPP>
- # Define boost_date_time_source
- STD: String strtime = boost: Gregorian: to_iso_string (\
- Boost: Gregorian: day_clock: local_day ());
- STD: cout <strtime. c_str () <STD: Endl;
2. YYYYMMDD-HH output: mm: SS
[CPP]
View plaincopy
- # Include <boost/date_time/posix_time/posix_time.hpp>
- # Define boost_date_time_source
- STD: String strtime = boost: posix_time: to_iso_string (\
- Boost: posix_time: second_clock: local_time ());
- // The time format in strtime is yyyymmddthhmmss. the date and time are separated by uppercase letters 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: posix_time: to_iso_string (\
- Boost: posix_time: second_clock: local_time ());
- // The time format in strtime is yyyymmddthhmmss. the date and time are separated by uppercase letters 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 interval. There are many powerful functions for calculating the time interval in boost. I only list what I currently use.
[CPP]
View plaincopy
- # Include <boost/date_time/posix_time/posix_time.hpp>
- # Include <boost/thread. HPP>
- # Define boost_date_time_source
- Boost: posix_time: ptime time_now, time_now1;
- Boost: posix_time: millisec_posix_time_system_config: time_duration_type time_elapse;
- // Here, the unit is microsecond. Here, you can replace microsec_clock with second_clock in seconds;
- Time_now = boost: posix_time: microsec_clock: universal_time ();
- // Sleep 100 ms;
- Boost: this_thread: Sleep (boost: posix_time: millisec (100 ));
- Time_now1 = boost: posix_time: microsec_clock: universal_time ();
- Time_elapse = time_now1-time_now;
- // Similar to gettickcount, only the difference between the two ticket values is obtained here, in microseconds;
- Int ticks = time_elapse.ticks ();
- // Obtain the number of seconds between the two 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: posix_time: ptime time_now, time_now1;
- Boost: posix_time: millisec_posix_time_system_config: time_duration_type time_elapse;
- // Here, the unit is microsecond. Here, you can replace microsec_clock with second_clock in seconds;
- Time_now = boost: posix_time: microsec_clock: universal_time ();
- // Sleep 100 ms;
- Boost: this_thread: Sleep (boost: posix_time: millisec (100 ));
- Time_now1 = boost: posix_time: microsec_clock: universal_time ();
- Time_elapse = time_now1-time_now;
- // Similar to gettickcount, only the difference between the two ticket values is obtained here, in microseconds;
- Int ticks = time_elapse.ticks ();
- // Obtain the number of seconds between the two intervals;
- Int sec = time_elapse.total_seconds ();
From: http://blog.csdn.net/morebread/article/details/6742957