歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux下用C獲取當前系統時間

Linux下用C獲取當前系統時間

日期:2017/3/2 9:59:53   编辑:關於Linux

#include <time.h>
time_t time(time_t calptr);
返回的是日歷時間,即國際標准時間公元1970年1月1日00 : 00 : 00以來經過的秒數。然後再調用
char *ctime(const time_t calptr) ;
轉化為字符串表示

#include <stdio.h>
#include <time.h>
int main ()
{
time_t timep;
time (&timep);
printf( "%s ",ctime(&timep));
}

用localtime可直接分解出年月日時分秒:
struct tm *ptm;
long ts;
int y,m,d,h,n,s;

ts = time(NULL);
ptm = localtime(&ts);

y = ptm-> tm_year+1900; //年
m = ptm-> tm_mon+1; //月
d = ptm-> tm_mday; //日
h = ptm-> tm_hour; //時
n = ptm-> tm_min; //分
s = ptm-> tm_sec; //秒

Copyright © Linux教程網 All Rights Reserved