參考:
這里的獲取時間戳和獲取當前日期時間是用的不一樣的方法
在linux下,頭文件<sys/time.h>已經定義好了結構體
struct timeval{ long tv_sec; //秒
long tv_usec; //微秒
};
demo(通過時間戳來生成一個唯一的名字,且按生成的時間順序排列):
#include <sys/time.h> #include <stdio.h> #include <unistd.h> #include <iostream>
using namespace std; int main() { int i; struct timeval tv; for(i=0; i<4; ++i) { gettimeofday(&tv, NULL); printf("%d\t%d\n",tv.tv_usec,tv.tv_sec); string image_n = std::to_string(tv.tv_sec) + "_" + std::to_string(tv.tv_usec); cout<< "iamge_name :"<<image_n <<endl; sleep(1); } return 0; }
關於sleep函數的注意點:
<unistd.h>頭文件介紹: