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;}


上一篇:SpringBoot中怎么处理RESTful API请求

下一篇:MyBatis中TypeHandler的作用是什么


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