C++获取系统时间的三种方法

前言

在 C++ 编程中,获取系统时间是一项常见任务。无论是记录日志、计算程序运行时间,还是实现计时功能,都需要获取当前的系统时间。本文将介绍几种在 C++ 中获取系统时间的方法,并提供相应的代码示例。

1. 使用 C 标准库函数

C++ 兼容 C 标准库,因此可以使用 C 标准库中的 time.h 头文件来获取系统时间。

1.1 获取当前时间戳

#include <iostream>
#include <ctime>
 
int main() {
    // 获取当前时间戳
    time_t now = time(nullptr);
 
    // 将时间戳转换为本地时间
    tm* localTime = localtime(&now);
 
    // 输出当前时间
    std::cout << "当前时间: " << asctime(localTime);
 
    return 0;
}

代码解释:

  • time(nullptr) 返回当前的 Unix 时间戳,即从 1970 年 1 月 1 日 00:00:00 UTC 到现在的秒数。
  • localtime(&now) 将时间戳转换为本地时间结构体 tm。
  • asctime(localTime) 将 tm 结构体转换为可读的字符串格式。

1.2 获取特定时间格式

#include <iostream>
#include <ctime>
 
int main() {
    // 获取当前时间戳
    time_t now = time(nullptr);
 
    // 将时间戳转换为本地时间
    tm* localTime = localtime(&now);
 
    // 格式化输出时间
    char buffer[80];
    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localTime);
 
    std::cout << "当前时间: " << buffer << std::endl;
 
    return 0;
}

代码解释:

  • strftime 函数用于将 tm 结构体格式化为指定格式的字符串。
  • %Y-%m-%d %H:%M:%S 是格式化字符串,表示年-月-日 时:分:秒。

2. 使用 C++11 标准库

C++11 引入了 <chrono> 库,提供了更现代、更灵活的时间处理方式。

#include <iostream>
#include <chrono>
#include <ctime>
 
int main() {
    // 获取当前时间点
    auto now = std::chrono::system_clock::now();
 
    // 将时间点转换为时间戳
    std::time_t now_c = std::chrono::system_clock::to_time_t(now);
 
    // 将时间戳转换为本地时间
    tm* localTime = std::localtime(&now_c);
 
    // 输出当前时间
    std::cout << "当前时间: " << std::put_time(localTime, "%Y-%m-%d %H:%M:%S") << std::endl;
 
    return 0;
}

代码解释:

  • std::chrono::system_clock::now() 获取当前时间点。
  • std::chrono::system_clock::to_time_t(now) 将时间点转换为时间戳。
  • std::localtime(&now_c) 将时间戳转换为本地时间结构体 tm
  • std::put_time(localTime, "%Y-%m-%d %H:%M:%S") 将 tm 结构体格式化为指定格式的字符串。

2.1 计算时间差

#include <iostream>
#include <chrono>
 
int main() {
    // 获取开始时间点
    auto start = std::chrono::high_resolution_clock::now();
 
    // 模拟一些耗时操作
    std::this_thread::sleep_for(std::chrono::seconds(2));
 
    // 获取结束时间点
    auto end = std::chrono::high_resolution_clock::now();
 
    // 计算时间差
    std::chrono::duration<double> diff = end - start;
 
    // 输出时间差
    std::cout << "耗时: " << diff.count() << " 秒" << std::endl;
 
    return 0;
}

代码解释:

  • std::chrono::high_resolution_clock::now() 获取当前时间点。
  • std::this_thread::sleep_for(std::chrono::seconds(2)) 模拟 2 秒的耗时操作。
  • std::chrono::duration<double> diff = end – start 计算时间差。
  • diff.count() 返回时间差的秒数。

3. 使用 Boost 库

Boost 是一个功能强大的 C++ 库集合,提供了许多高级功能,包括时间处理。

#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
 
int main() {
    // 获取当前时间
    boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
 
    // 输出当前时间
    std::cout << "当前时间: " << now << std::endl;
 
    return 0;
}

代码解释:

  • boost::posix_time::second_clock::local_time() 获取当前本地时间。
  • now 是一个 boost::posix_time::ptime 对象,表示时间点。

总结

本文介绍了三种在 C++ 中获取系统时间的方法:

  • C 标准库函数:简单易用,但功能有限。
  • C++11 <chrono> 库:现代、灵活,适合复杂的时间处理需求。
  • Boost 库:功能强大,适合需要高级时间处理功能的场景。

到此这篇关于C++获取系统时间的三种方法的文章就介绍到这了,更多相关C++获取系统时间内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源链接:https://www.jb51.net/program/327313f2s.htm

© 版权声明
THE END
支持一下吧
点赞7 分享
评论 抢沙发
头像
请文明发言!
提交
头像

昵称

取消
昵称表情代码快捷回复

    暂无评论内容