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
注意,两种方法输出时间不一样