在C++中获取时间戳的方法通常有以下几种:
#include <ctime>
#include <iostream>
int main() {
time_t timestamp = time(nullptr);
std::cout << "Current timestamp: " << timestamp << std::endl;
return 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;
}
#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;
}