首页 > 代码库 > Linux系统编程_5_获取系统时间
Linux系统编程_5_获取系统时间
Linux环境中时间编程函数:
比较常用的是ctime与localtime
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
Linux环境中时间编程结构体:
struct tm { int tm_sec; /* seconds */ int tm_min; /* minutes */ int tm_hour; /* hours */ int tm_mday; /* day of the month */ int tm_mon; /* month */ int tm_year; /* year */ int tm_wday; /* day of the week */ int tm_yday; /* day in the year */ int tm_isdst; /* daylight saving time */ };
Linux系统中获取系统当前时间的两种方法比较:
#include <stdio.h> #include <time.h> int main() { time_t nTime; struct tm *stTime; char *str; str = ctime(&nTime); printf("Time: %s\n", str); time(&nTime); stTime = localtime(&nTime); printf("Current Time: %d:%d:%d,%d:%d:%d\n", stTime->tm_year+1900, stTime->tm_mon, stTime->tm_mday, stTime->tm_hour, stTime->tm_min, stTime->tm_sec); return 0; }
打印:
Time: Sun Apr 7 04:47:40 1974
Current Time: 2014:10:15,15:39:44
注意,两种方法输出时间不一样
Linux系统编程_5_获取系统时间
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。