一、timeval结构体
需要头文件#include<sys/time.h>
man手册截图

1.通过man手册可以看到timeval结构体包含了time_t和suseconds_t俩个结构体
2.使用int gettimeofday(struct timeval *tv, struct timezone *tz);函数获取当前时间

3.gettimeofday——demon
#include<stdio.h>
#include<sys/time.h>
int main()
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
printf("%ld\t%ld\n", tv.tv_usec, tv.tv_sec);
printf("%d\t%d\n", tz.tz_minuteswest, tz.tz_dsttime);
return 0;
}
运行结果

二、tm结构体

需要头文件:#include<time.h>
1.localtime是 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间
2.将获取到的时间转换为标准格式
#include <stdio.h>
#include <time.h>
int main()
{
struct tm *_tm;
time_t _t;
time(&_t);//获取当前时间
_tm=localtime(&_t);
printf("%4d年%02d月%02d日 %02d:%02d:%02d\n",_tm->tm_year+1900,_tm->tm_mon+1,_tm->tm_mday,_tm->tm_hour,_tm->tm_min,_tm->tm_sec);
return 0;
}
运行结果:


936

被折叠的 条评论
为什么被折叠?



