1. time_t:类型变量,相当于long
2.tm:结构体类型
struct tm
{
int tm_sec; /* Seconds. [0-60] (1 leap second) */
int tm_min; /* Minutes. [0-59] */
int tm_hour; /* Hours. [0-23] */
int tm_mday; /* Day. [1-31] */
int tm_mon; /* Month. [0-11] */
int tm_year; /* Year - 1900. */
int tm_wday; /* Day of week. [0-6] */
int tm_yday; /* Days in year.[0-365] */
int tm_isdst; /* DST. [-1/0/1]*/
}
3.gettimeifday函数:获取本地时间转换为时间戳
timeval cur_time;
char buf[128];
gettimeofday(&cur_time,NULL);
printf("----------cur_time:%d.%d\n",cur_time.tv_sec,cur_time.tv_usec);
函数输出为:
4.gmtime_r函数:将日历时间timep转换为用UTC时间表示的时间
timeval cur_time;
char buf[128];
gettimeofday(&cur_time,NULL);
printf("----------cur_time:%d.%d\n",cur_time.tv_sec,cur_time.tv_usec);
int time = cur_time.tv_sec;
struct tm t;
time_t epoch_time = (time_t)time;
gmtime_r(&epoch_time, &t);
printf("year:%d-min:%d-day:%d-hour:%d-min:%d-sec:%d\n",t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min,t.tm_sec);
函数输出为:
函数输出为从1900开始的年月日,为获得标准输出需要加上1900:
printf("year:%d-min:%d-day:%d-hour:%d-min:%d-sec:%d\n",t.tm_year+1900, t.tm_mon+1, t.tm_mday+1, t.tm_hour, t.tm_min,t.tm_sec);
输出为:
5.strftime 将时间戳按格式输出
timeval cur_time;
char buf[128];
gettimeofday(&cur_time,NULL);
printf("----------cur_time:%d.%d\n",cur_time.tv_sec,cur_time.tv_usec);
int time = cur_time.tv_sec;
struct tm t;
time_t epoch_time = (time_t)time;
gmtime_r(&epoch_time, &t);
printf("year:%d-mon:%d-day:%d-hour:%d-min:%d-sec:%d\n",t.tm_year+1900, t.tm_mon+1, t.tm_mday+1, t.tm_hour, t.tm_min,t.tm_sec);
strftime(buf, 32,"%Y %m %d %H %M %S",&t);
printf("---------------times:%s\n",buf);
string cmd = "date;timedatectl";
system(cmd.c_str());
函数按格式输出时间,其中timedatectl可以看出系统时区,输出为UTC时间
6. localtime函数:将日历时间timep转换为用户指定的时区的时间
原型:struct tm *localtime(const time_t *calkptr)
timeval cur_time;
char buf[128];
gettimeofday(&cur_time,NULL);
printf("----------cur_time:%d.%d\n",cur_time.tv_sec,cur_time.tv_usec);
int time = cur_time.tv_sec;
struct tm t;
time_t epoch_time = (time_t)time;
gmtime_r(&epoch_time, &t);
printf("year:%d-mon:%d-day:%d-hour:%d-min:%d-sec:%d\n",t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min,t.tm_sec);
strftime(buf, 32,"%Y %m %d %H %M %S",&t);
printf("---------------times:%s\n",buf);
struct tm * timenow = localtime(&epoch_time);
strftime(buf, 128,"%Y %m %d %H %M %S", timenow);
printf("---------------time:%s\n",buf);
string cmd = "date;timedatectl";
system(cmd.c_str());
经过localtime转换后输出为本地时区时间
7.mktime:将tm结构转换为时间戳
timeval cur_time;
char buf[128];
gettimeofday(&cur_time,NULL);
printf("----------cur_time:%d.%d\n",cur_time.tv_sec,cur_time.tv_usec);
int time = cur_time.tv_sec;
struct tm t;
time_t epoch_time = (time_t)time;
gmtime_r(&epoch_time, &t);
printf("year:%d-mon:%d-day:%d-hour:%d-min:%d-sec:%d\n",t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min,t.tm_sec);
strftime(buf, 32,"%Y %m %d %H %M %S",&t);
printf("---------------times:%s\n",buf);
struct tm * timenow = localtime(&epoch_time);
strftime(buf, 128,"%Y %m %d %H %M %S", timenow);
printf("---------------time:%s\n",buf);
time_t tmp = mktime(timenow);
printf("---------------mktime t:%d\n",tmp);
string cmd = "date;timedatectl";
system(cmd.c_str());
8.ctime:将time_t输出为字符串,结合以上代码
std::cout << ctime(&epoch_time) << std::endl;
9.difftime:计算两个time_t的时间差
time_t t2 = 1656259624;
printf("t1:%d, t2:%d, diff:%f\n",time,t2,difftime(time,t2));
printf("---------diff:%d\n",time-t2);