//获取时间
头文件:#include <time.h>
定义函数:struct tm *localtime(const time_t * timep);
函数说明:localtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm 返回。结构tm 的定义:
struct tm {
// int tm_sec; /* seconds after the minute - [0,59] */
// int tm_min; /* minutes after the hour - [0,59] */
// int tm_hour; /* hours since midnight - [0,23] */
// int tm_mday; /* day of the month - [1,31] */
// int tm_mon; /* months since January - [0,11] */
// int tm_year; /* years since 1900 */
// int tm_wday; /* days since Sunday - [0,6] */
// int tm_yday; /* days since January 1 - [0,365] */
// int tm_isdst; /* daylight savings time flag */
// };
。此函数返回的时间日期已经转换成当地时区。
返回值:返回结构tm 代表目前的当地时间。
范例
#include <time.h>
main(){
char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
time_t timep;
time(&timep);
auto p = localtime(&timep); //取得当地时间
printf ("%d%d%d ", (1900+p->tm_year), (l+p->tm_mon), p->tm_mday);
printf("%s%d:%d:%d ", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
#include <time.h> main(){ char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; time_t timep; struct tm *p; time(&timep); p = localtime(&timep); //取得当地时间 printf ("%d%d%d ", (1900+p->tm_year), (l+p->tm_mon), p->tm_mday); printf("%s%d:%d:%d ", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec); }