【C/C++時間系列】通過time()函式獲取時間戳
阿新 • • 發佈:2019-01-05
【時間戳】Unix時間戳(Unix timestamp),或稱Unix時間(Unix time)、POSIX時間(POSIX time),是一種時間表示方式,定義為從格林威治時間1970年01月01日00時00分00秒起至現在的總秒數
##############
【time()】獲取當前時間
函式模型在time.h中
/* Return the current time and put it in *TIMER if TIMER is not NULL. */
extern time_t time (time_t *__timer) __THROW;
從備註瞭解到 有2中方式可以獲取到時間,一種是函式返回值 ,另一種是*__timer指標。
程式碼實現如下:
#include <iostream> #include <time.h> using namespace std; int main() { time_t myt=time(NULL); cout<<"sizeof(time_t) is: "<<sizeof(time_t)<<endl; cout<<"myt is :"<<myt<<endl; time_t t; time(&t); cout<<"t is:"<<t<<endl; }
編譯執行如下:
$gcc -lstdc++ l_time_t.cpp
$./a.out
sizeof(time_t) is: 8
myt is :1532958324
t is:1532958324
$
1、可以通過指定NULL引數來獲得返回的時間
2、可以通過&t方式傳遞地址給time函式,並把結果存放到t中
3、time_t在該系統的sizeof 為8
4、返回結果為1970年01月01日00時00分00秒起至現在的總秒數