1,time_t 类型。一个整数,表示从1970-01-01到现在的秒数。
获得其值的方法:
#include<time.h>
time_t time(time_t *t);
如果t不为空则把的得到的结果填入t所指的内存中去
2,struct timeval .表示从1970-01-01到现在经历的时间。可以精确到微秒
#include<time.h>
struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
struct timezone{
int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/
int tz_dsttime; /*日光节约时间的状态*/
};
获取方法:
#include <sys/time.h>
#include <unistd.h>
int gettimeofday ( struct timeval * tv , struct timezone * tz )
int settimeofday(const struct timeval *tv, const struct timezone *tz);
3,struct tm 详细的日期和时间
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 */
};
目前没有发现有直接过去该时间值的系统调用。
4,各个时间方式直接的转换
时间转string:
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
time_t与struct tm互转:
struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
time_t mktime(struct tm *tm);
其他转换:
size_t strftime(char *s, size_t max, const char *format,const struct tm *tm); //根据format和tm生成时间字符串
char *strptime(const char *s, const char *format, struct tm *tm); //根据s和format生成tm