类型:
time_t:其实是一个整型。
//时间结构体
<time.h>
struct tm
{
int tm_sec; //second:0-60
int tm_min; //minute:0-59
int tm_hour; //hour:0-23
int tm_mday; //month day:1-31
int tm_mon; //month:1-11
int tm_year; //year:1900-
int tm_wday; //week day:0-6
int tm_yday; //year day:0-365
int tm_isdat; //夏令时标志
};
//微秒结构体
<linux/time.h>
struct timeval
{
long tv_sec; //second
long tv_usec; //microseconds
};
//纳秒结构体
<linux/time.h>
struct timespce
{
time_t tv_sec; second
long tv_nsec; //nanoseconds
};
===========================================
标准C与日期函数
1.得到本地时间格式字符串
#include <time.h>
char *asctime(const struct tm *ptr);
2.得到程序开始运行的处理器时间
#include <time.h>
clock_t clock(void);
3.转换成本地格式时间
#include <time.h>
char *ctime(const time_t *time);
4.比较两个时间,返回秒数
#include <time.h>
double difftime(time_t time2,time_t time1);
5.time_t转成tm,格林时间
#include <time.h>
struct tm *gmtime(const time_t *time);
6.time_t转成tm,本地时间
#include <time.h>
struct tm *localtime(const time_t *time);
7.tm转成time_t
#include <time.h>
time_t mktime(const struct tm *time);
8.格式化时间
#inlcude <time.h>
size_t strftime(char *str,size_t maxsize,char *fmt,struct tm *time);
%Y%m%d %H%M%S
9.得到当前时间
#include <time.h>
time_t time(time_t *time);
10.得到微秒和时间
int gettimeofday(struct timeval *tv,struct timezone *tz);
==============================
代码:
#include <time.h>
int main()
{
time_t t1,t2;
struct tm *timeptr;
struct timeval tv1,tv2;
struct timezone tz;
char *timestr;
time(&t1);
gettimeofday(&tv1,&tz);
timeptr = localtime(&t1);
timeptr = gmtime(&t1);
ctime(&t1);
t2 = mktime(timeptr);
asctime(timeptr);
time(&t2)
gettimeofday(&tv2,&tz);
difftime(t2,t1);
strftime(timestr,(size_t)20,"%H-%m-%d %H%M%S",timeptr);
return 1;
}