Unix时间
unix时间戳是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数
获取方法
gettimeofday函数
函数原型
#include<sys/time.h>
int gettimeofday(struct timeval*tv,struct timezone *tz )
结构体说明
struct timeval{
long tv_sec;/*秒*/
long tv_usec;/*微妙*/
};
struct timezone{
int tz_minuteswest;/*和greenwich时间差*/
int tz_dsttime;
}
使用方法
int64_t GetCurrentTime()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
time函数
#include <time.h>
time_t time(time_t *t);
time_t其实就是long int类型,只不过被typedef重命名了。
调用time时,需要定一个long int变量(缓存)来存放总秒数。
功能
返回从1970.1.1经过的秒数
使用方法
//通过返回值获取, 不使用参数时,参数指定为NULL
time_t tim = time(NULL);
//通过参数获取
long int tim = 0;
time(&tim);
printf("tim = %ld\n",tim);
ctime函数
#include <time.h>
char *ctime(const time_t *timep);
功能
将time返回的总秒数,转为固定的格式时间,不过这个时间是国际时间,并不是本地时间(我们的本地时间是北京时间)
使用方法
long int tim = 0;
time(&tim);
char * ctim = NULL;
ctim = ctime(&tim);
printf("%s\n",ctim); //Mon Feb 17 11:00:01 2020
gtime函数
#include <time.h>
struct tm *gmtime(const time_t *timep);
功能
将time返回的总秒数,转为国际时间的年 月 日 时 分 秒。
然后开辟一个struct tm结构体变量,将年月日时分秒放到struct tm结构体变量中
//time.h
struct tm {
int tm_sec; /* 秒 – 取值区间为[0,59] */
int tm_min; /* 分 - 取值区间为[0,59] */
int tm_hour; /* 时 - 取值区间为[0,23] */
int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11]*/
int tm_year; /* 年份,其值等于实际年份减去1900 */
int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
};
使用方法
long int tim = 0;
time(&tim);
struct tm * gmt = NULL;
gmt = gmtime(&tim);
printf("year : %d month : %d day = %d hour : %d min : %d second %d\n",gmt->tm_year-1900, gmt->tm_mon+1 ,gmt->tm_mday,gmt->tm_hour,gmt->tm_min,gmt->tm_sec);
return 0;
locatime函数
#include <time.h>
struct tm *localtime(const time_t *timep);
功能
与gmtime完全一样,只不过是转为本地时间的年月日时分秒,我们的本地时间是北京时间。
使用方法
long int tim = 0;
time(&tim);
struct tm * gmt = NULL;
gmt = localtime(&tim);
printf("year : %d month : %d day = %d hour : %d min : %d second %d\n",gmt->tm_year,gmt->tm_mon,gmt->tm_mday,gmt->tm_hour,gmt->tm_min,gmt->tm_sec);
return 0;
mktime函数
#include <time.h>
time_t mktime(struct tm *tm);
功能
将struct tm变量中的年月日时分秒,反过来转为总秒数。
使用方法
long int tim = 0;
time(&tim);
struct tm * gmt = NULL;
gmt = localtime(&tim);
printf("year : %d month : %d day = %d hour : %d min : %d second %d\n",gmt->tm_year,gmt->tm_mon,gmt->tm_mday,gmt->tm_hour,gmt->tm_min,gmt->tm_sec);
long int mkt = mktime(gmt);
printf("总秒数为:%ld\n",mkt);
return 0;
asctime函数
#include <time.h>
char *asctime(const struct tm *tm);
功能
负责将struct tm中的年月日时分秒,组合为固定格式的时间。
使用方法
long int tim = 0;
time(&tim);
struct tm * gmt = NULL;
gmt = localtime(&tim);
char *asc = asctime(gmt);
printf("%s\n",asc);//Mon Feb 17 11:00:01 2020
return 0;
strftime函数
#include <time.h>
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
###功能
与asctime功能一样,只不过strftime能够组合为我们自己指定的时间格式。
为了组合为我们自定义的时间格式,我们需要为函数其指定格式
使用方法
参数
s:缓存地址,这个缓存用于存放转换后的字符串。
max:缓存的大小
tm:放有年月日时分秒的结构体变量的地址。
format:自定义时间格式与printf("%d %s", a, buf);指定打印格式的操作方式是一样的
long int tim = 0;
time(&tim);
struct tm * gmt = NULL;
gmt = localtime(&tim);
char buf[100] = {0};
strftime(buf, sizeof(buf),"%Y.%m.%d %H:%M:%S\n", gmt);
printf("buf = %s\n",buf); //2020.11.23 18:00:00
return 0;
参考链接
原文链接: https://www.cnblogs.com/Daniel-Hu/p/14025986.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/205324
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!