1. time相关的函数在 time.h 中可以查看原型。如下命令可以找出time.h的路径:
whereis time.h
2. 先看代码,要包含time.h
===================================
void main() {
struct timeval val;
gettimeofday(&val, NULL);
printf("sec=%ld, usec=%ld
", val.tv_sec, val.tv_usec);
time_t rawtime; // long type
time ( &rawtime );
printf("rastime = %ld
", rawtime);
struct tm * timeinfo;
timeinfo = localtime ( &rawtime );
printf("%d-%d-%d %d:%d:%d
", timeinfo->tm_year+1900, timeinfo->tm_mon+1,
timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
printf ( "The current date/time is: %s", asctime (timeinfo) );
}
======================================
说明:
1> time_t实际上是长整数类型,定义为:typedef long time_t;
{
__time_t tv_sec;
__suseconds_t tv_usec;
};
2>
localtime 函数原型:struct tm *localtime(const time_t *timer)
3>
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
4>
功 能: 转换日期和时间为相应的字符串(英文简写形式,形如:Mon Feb 16 11:29:26 2009)
参考博客:
https://www.cnblogs.com/wainiwann/archive/2012/11/28/2792133.html