主要的时间类型:
1. time_t : long
2. struct tm{
int tm_sec; /* seconds 0 - 59*/ int tm_min; /* minutes 0 - 59*/ int tm_hour; /* hours 0 - 23*/ int tm_mday; /* day of the month 01 - 31*/ int tm_mon; /* month 0 - 11*/ int tm_year; /* year 距离1990年的数*/ int tm_wday; /* day of the week 0 - 6*/ int tm_yday; /* day in the year 0 - 355*/ int tm_isdst; /* daylight saving time */ };
3. struct timeval {
time_t tv_sec; /* seconds (秒)*/
suseconds_t tv_usec; /* microseconds(微秒) */
};
4. struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
};
1. time_t time(time_t * timer)
头文件:#include <time.h>
time_t : long
函数返回从1970-1-1 0:0:0开始到现在的秒数,如果参数timer不为空的话,返回的结果也会存在参数中。
2. struct tm *localtime(const time_t *timep)
头文件:#include <time.h>
函数功能:将timep的时间转换为tm类型的时间
3. struct tm *localtime_r(const time_t *timep, struct tm *result)
头文件:#include <time.h>
函数功能:同上
4. char *asctime(const struct tm *tm)
char *asctime_r(const struct tm *tm, char *buf)
头文件:#include <time.h>
函数功能:将time转为字符串形式的格式
效果: Sat Aug 3 23:13:19 2019
5.char *ctime(const time_t *timep)
char *ctime_r(const time_t *timep, char *buf);
头文件:#include <time.h>
函数功能:将time_t 类型的时间转换为现实世界使用的日期字符串
效果:Sat Aug 3 23:20:18 2019
6. time_t mktime(struct tm *tm)
头文件:#include <time.h>
函数功能:将时间结构体struct tm的值转化为经过的秒数
7. int gettimeofday(struct timeval *tv, struct timezone *tz)
头文件:#include <time.h>
函数功能:gettimeofday函数获取当前时间存于tv结构体中,相应的时区信息则存于tz结构体中 需要注意的是tz是依赖于系统,不同 的系统可能存在获取不到的可能,因此通常设置为NULL