关于时间的处理
1,常用的结构体
(1),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 */
};
//int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒
//int tm_min 代表目前分数,范围0-59
//int tm_hour 从午夜算起的时数,范围为0-23
//int tm_mday 目前月份的日数,范围01-31
//int tm_mon 代表目前月份,从一月算起,范围从0-11
//int tm_year 从1900 年算起至今的年数
//int tm_wday 一星期的日数,从星期一算起,范围为0-6
//int tm_yday 从今年1月1日算起至今的天数,范围为0-365
//int tm_isdst 日光节约时间的旗标
(2),struct timeval,struct timezone;
struct timeval {
time_t tv_sec; /* seconds (秒)*/
suseconds_t tv_usec; /* microseconds(微秒) */
};
struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
};
int tz_minuteswest; /* 格林威治时间往西方的时差 */
int tz_dsttime; /* 时间的修正方式*/
(3),struct timespec
struct timespec {
time_t tv_sec; // seconds (秒)
long tv_nsec; // and nanoseconds(纳秒)
};
(4), struct timeb
struct timeb {
time_t time;
unsigned short millitm;//毫秒
short timezone;
short dstflag;
};
2,时间函数介绍
(1),time()函数获取当前时间
#include <time.h>
time_t time(time_t *t);
//此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。
eg:
#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
time_t seconds;
seconds = time((time_t *)NULL);
printf("%d\n", seconds);
return 0;
}
(2),localtime_r() localtime()取得当地目前时间和日期
#include <time.h>
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
/*该函数将有time函数获取的值timep转换真实世界所使用的时间日期表示方法,然后将结果由结构tm返回*/
/**需要注意的是localtime函数可以将时间转换本地时间,但是localtime函数不是线程安全的。
多线程应用里面,应该用localtime_r函数替代localtime函数,因为localtime_r是线程安全的**/
eg:
#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
time_t timep;
struct tm *p;
time(&timep);
p = localtime(&timep);
printf("本地时间:%d-%d-%d-%d-%d-%d\n",ptr->tm_year + 1900,ptr->tm_mon + 1,ptr->tm_mday,ptr->tm_hour,ptr->tm_min,ptr->tm_sec);
return 0;
}
(3),asctime() asctime_r() 将时间和日期以字符串格式返回‘
#include <time.h>
struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
/**gmtime是把日期和时间转换为格林威治(GMT)时间的函数。将参数time 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回**/
/**asctime 将时间以换为字符串字符串格式返回 **/
eg:
#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
time_t timep;
time(&timep);
printf("%s\n", asctime(gmtime(&timep)));
;
}
(4),ctime(),ctime_r() 将时间和日期以字符串格式表示
#include <time.h>
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
/**ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回**/
eg:
#include <stdio.h>
#include <string.h>
#include <time.h>
int main(void)
{
time_t timep;
time(&timep);
printf("%s\n", ctime(&timep));
;
}
(5),mktime() 将时间结构体struct tm的值转化为经过的秒数
#include <time.h>
time_t mktime(struct tm *tm);
/**将时间结构体struct tm的值转化为经过的秒数**/
eg:
#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
time_t timep;
struct tm *p;
time(&timep);
p = localtime(&timep);
timep = mktime(p);
printf("%d\n", timep);
;
}
(6),gettimeofday() 获取当前时间
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
struct timeval {
time_t tv_sec; /* seconds (秒)*/
suseconds_t tv_usec; /* microseconds(微秒) */
};
struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */和格林威治时间差多少分钟
int tz_dsttime; /* type of DST correction */日光节约时间的状态
};
//gettimeofday函数获取当前时间存于tv结构体中,相应的时区信息则存于tz结构体中
//需要注意的是tz是依赖于系统,不同的系统可能存在获取不到的可能,因此通常设置为NULL
eg:
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
int main()
{
struct timeval tv;
gettimeofday(&tv, NULL);
printf("tv_sec: %d\n", tv.tv_sec);
printf("tv_usec: %d\n", tv.tv_usec);
return 0;
}
(7),clock_gettime
int clock_gettime(clockid_t clk_id, struct timespec* tp);
clk_id : 检索和设置的clk_id指定的时钟时间。
CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,
中间时刻如果系统时间被用户改成其他,则对应的时间相应改变
CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间
CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间
eg:
Link with -lrt.
int main()
{
struct timespec t;
clock_gettime(CLOCK_REALTIME,&t);
printf("tv_sec:%d\n",t.tv_sec);
printf("tv_nsec:%d\n",t.tv_nsec);
return 0;
}
(8),ftime
#include <sys/timeb.h>
int ftime(struct timeb *tp);
精确到毫秒
eg:
int main()
{
struct timeb tp;
ftime(&tp);
printf("time:%d\n", tp.time);
printf("millitm:%d\n",tp.millitm);
printf("timezone:%d\n",tp.timezone);
printf("dstflag:%d\n",tp.dstflag);
return 0;
}
函数 精确级别 数据类型
time 秒级(s) time_t
ftime 毫秒级(ms) struct timeb
gettimeofday 微妙级(us) struct timeval
clock_gettime 纳秒级(ns) struct timespec
ctime和asctime区别:都是时间字符串 参数不一样,前者是time_t 后者是struct tm
mktime和gmtime,localtime是一个逆过程。
mktime 输入是struct tm 输出是time_t
gmtime,localtime 输入是time_t 输出是struct tm