在C++中,获取当前时间的方法有很多种,以下是一些常见的方法:
1、使用<ctime>
库
<ctime>
库是C++标准库中的一个头文件,它提供了一些用于处理时间的函数。time()
函数可以获取当前的系统时间,返回一个表示从1970年1月1日午夜(UTC)到当前时间的秒数,要将其转换为更易读的格式,可以使用localtime()
函数将秒数转换为tm
结构体,然后使用strftime()
函数将tm
结构体格式化为字符串。
示例代码:
include <iostream> include <ctime> include <iomanip> int main() { std::time_t now = std::time(nullptr); std::tm* local_time = std::localtime(&now); char time_str[20]; std::strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", local_time); std::cout << "当前时间:" << time_str << std::endl; return 0; }
2、使用<chrono>
库
C++11引入了<chrono>
库,它提供了一种更现代、更灵活的方式来处理时间。std::chrono::system_clock::now()
函数可以获取当前的系统时间,返回一个表示从某个固定点(通常是系统启动时)到现在的时间点,要将其转换为更易读的格式,可以使用std::put_time()
函数将时间点格式化为字符串。
示例代码:
include <iostream> include <chrono> include <iomanip> include <sstream> int main() { auto now = std::chrono::system_clock::now(); auto duration = now.time_since_epoch(); auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration).count(); std::tm time_info; time_info.tm_year = (seconds / 60 / 60 / 24) 1970; // years since 1970 time_info.tm_mon = (seconds / 60 / 60 / 24) % 12 + 1; // months since January [0, 11] time_info.tm_mday = seconds / 60 / 60 % 28 + 1; // day of the month [1, 31] time_info.tm_hour = seconds / 60 % 60; // hours since midnight [0, 59] time_info.tm_min = seconds % 60; // minutes after the hour [0, 59] time_info.tm_sec = seconds % 60; // seconds after the minute [0, 59] std::stringstream ss; ss << std::put_time(&time_info, "%Y-%m-%d %H:%M:%S"); std::cout << "当前时间:" << ss.str() << std::endl; return 0; }
3、使用第三方库(如Boost.DateTime)
除了C++标准库和C++11引入的<chrono>
库之外,还可以使用第三方库来获取当前时间,Boost.DateTime库提供了一个名为boost::posix_time::ptime
的类,它可以表示一个时间点,并提供了一些用于操作时间的函数,要将其转换为更易读的格式,可以使用boost::posix_time::to_iso_extended_string()
函数将时间点格式化为字符串。
示例代码:
include <iostream> include <boost/date_time/posix_time/posix_time.hpp> include <boost/date_time/gregorian/gregorian.hpp> include <boost/date_time/formatting/iso_extended_format.hpp> include <boost/date_time/local_time/local_time.hpp> include <boost/date_time/zoned_time/zoned_time.hpp> include <boost/date_time/c_locale.hpp> include <boost/date_time/tz_database.hpp> include <boost/date_time/posix_time/posix_time.hpp> include <boost/date_time/gregorian/gregorian.hpp> include <boost/date_time/formatting/iso_extended_format.hpp> include <boost/date_time/local_time/local_time.hpp> include <boost/date_time/zoned_time/zoned_time.hpp> include <boost/date_time/c_locale.hpp> include <boost/date_time/tz_database.hpp> include <boost/date_time/posix_time/posix_time.hpp> include <boost/date_time/gregorian/gregorian.hpp> include <boost/date_time/formatting/iso_extended_format.hpp> include <boost/date_time/local_time/local_time.hpp> include <boost/date_time/zoned_time/zoned_time.hpp> include <boost/date_time/c_locale.hpp> include <boost/date_time/tz_database.hpp> include <boost/date_time/posix_time/posix_time.hpp> include <boost/date_time/gregorian/gregorian.hpp> include <boost/date_time/formatting/iso_extended_format.hpp> include <boost/date_time/local_time/local_time.hpp> include <boost/date_time/zoned_time/zoned_time.hpp> include <boost/date_time/c_locale.hpp> include <boost/date_time/tz_database.hpp> include <boost/date_time/posix_time/posix_time.hpp> include <boost/date_time/gregorian/gregorian.hpp> include <boost/date_time/formatting/iso_extended_format.hpp> include <boost/date_time/local_time/local
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/247848.html