c++获取时间戳的方法有哪些


在C++中获取时间戳的方法通常有以下几种:

    使用标准库中的time()函数来获取当前系统时间的时间戳,返回的是从1970年1月1日0时0分0秒开始到现在的秒数。
#include <ctime>#include <iostream>int main() {time_t timestamp = time(nullptr);std::cout << "Current timestamp: " << timestamp << std::endl;return 0;}
    使用标准库中的system_clock::now()函数来获取当前系统时间的时间戳,返回的是从1970年1月1日0时0分0秒开始到现在的毫秒数。
#include <chrono>#include <iostream>int main() {auto timestamp = std::chrono::system_clock::now().time_since_epoch().count();std::cout << "Current timestamp: " << timestamp << std::endl;return 0;}
    使用操作系统提供的API来获取时间戳,例如Windows系统可以使用GetSystemTime()函数,Linux系统可以使用gettimeofday()函数等。
#include <iostream>#include <windows.h>int main() {SYSTEMTIME st;GetSystemTime(&st);FILETIME ft;SystemTimeToFileTime(&st, &ft);ULARGE_INTEGER ul;ul.LowPart = ft.dwLowDateTime;ul.HighPart = ft.dwHighDateTime;time_t timestamp = ul.QuadPart / 10000000ULL - 11644473600ULL;std::cout << "Current timestamp: " << timestamp << std::endl;return 0;}


上一篇:django怎么执行python脚本

下一篇:SpringBoot中怎么集成Swagger文档


c++
Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器