C++获取当前时间戳
时间: 2023-11-26 09:47:04 浏览: 151
以下是两种C++获取当前时间戳的方法:
1.使用gettimeofday函数获取时间戳
```cpp
#include <sys/time.h>
#include <iostream>
int main() {
double timestamp;
struct timeval time_v;
gettimeofday(&time_v, NULL);
timestamp = time_v.tv_sec * 1000 + time_v.tv_usec / 1000;
std::cout << "now time: " << timestamp << std::endl;
return 0;
}
```
2.封装gettimeofday函数获取时间戳
```cpp
#include <sys/time.h>
#include <iostream>
double GetTimeStamp(void) {
double timestamp;
struct timeval time_v;
gettimeofday(&time_v, NULL);
timestamp = (time_v.tv_sec) + (time_v.tv_usec) / 1000000.0;
return timestamp;
}
int main() {
double timestamp = GetTimeStamp();
std::cout << "now time: " << timestamp << std::endl;
return 0;
}
```
阅读全文