时间获取相关函数mktime()、gmtime()
mktime()
函数原型:time_t mktimestruct tm *timeptr);
函数功能:将时间转换为自1970年1月1日以来逝去时间的秒数,发生错误时返回-1。也就是是把获取的时间转换成时间戳。
gmtime()
函数原型:struct tm *gmtimeconst time_t *timeptr)
函数功能:返回tm结构的格林尼治时间(GMT)
其中的tm结构体定义如下:
头文件:time.h
struct tm {int tm_sec; /* seconds after the minute, 0 to 600 - 60 allows for the occasional leap second) */int tm_min; /* minutes after the hour, 0 to 59 */int tm_hour; /* hours since midnight, 0 to 23 */int tm_mday; /* day of the month, 1 to 31 */int tm_mon; /* months since January, 0 to 11 */int tm_year; /* years since 1900 */int tm_wday; /* days since Sunday, 0 to 6 */int tm_yday; /* days since January 1, 0 to 365 */int tm_isdst; /* Daylight Savings Time flag */union { /* ABI-required extra fields, in a variety of types */struct {int __extra_1, __extra_2;};struct {long __extra_1_long, __extra_2_long;};struct {char *__extra_1_cptr, *__extra_2_cptr;};struct {void *__extra_1_vptr, *__extra_2_vptr;};};
};
话不多说,直接上才艺。
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#include <string.h>typedef struct _SystemTime
{uint8_t yearH;uint8_t yearL;uint8_t month;uint8_t day;uint8_t hour;uint8_t minute;uint8_t second;
} T_SystemTime, *PT_SystemTime;time_t CalEscapedTimeT_SystemTime DateTime);
void GetDateTimeFromEscapedtime_t lSec, T_SystemTime *tTime);int mainint argc,char **argv)
{ time_t tDataTimer = 0;T_SystemTime *TIMER;T_SystemTime SystemTime = {20, 20, 11, 26, 17, 52, 23};//定义的时间,也可以用time)获取当前时间tDataTimer = CalEscapedTimeSystemTime);GetDateTimeFromEscapedtDataTimer,TIMER);return 0;
}
/* 将时间转成时间戳 */
time_t CalEscapedTimeT_SystemTime DateTime)
{struct tm StmTime;time_t ThisTime = 0;memset&StmTime, 0, sizeofstruct tm));/* 获取时间差 */StmTime.tm_year = DateTime.yearH*100 + DateTime.yearL) - 1900;StmTime.tm_mon = DateTime.month - 1;StmTime.tm_mday = DateTime.day;StmTime.tm_hour = DateTime.hour;StmTime.tm_min = DateTime.minute;StmTime.tm_sec = DateTime.second;StmTime.tm_isdst = 0;ThisTime = mktime&StmTime);printf"ThisTime:%lld\n",ThisTime);return ThisTime;
}
/* 将时间戳转成日期时间 */
void GetDateTimeFromEscapedtime_t lSec, T_SystemTime *tTime)
{struct tm *StmTime;char *wday[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};StmTime = gmtime&lSec); /* 获取时间 */printf"%d年%02d月%02d日",1900+StmTime->tm_year),1+StmTime->tm_mon),StmTime->tm_mday);printf"%s %02d:%02d:%02d\n",wday[StmTime->tm_wday],StmTime->tm_hour),StmTime->tm_min,StmTime->tm_sec);//StmTime->tm_hour一般的时间戳转换是获取的世界时间,结果会加上时区的8小时,我这里不加是因为直接获取的当前网络时间tTime->yearH = 1900+StmTime->tm_year ) /100;//>> 8;tTime->yearL = 1900+StmTime->tm_year ) %100;//& 0xFF;tTime->month = 1 + StmTime->tm_mon;tTime->day = StmTime->tm_mday;tTime->hour = StmTime->tm_hour;tTime->minute = StmTime->tm_min;tTime->second = StmTime->tm_sec; printf"%d%d年%02d月%02d日",tTime->yearH,tTime->yearL,tTime->month,tTime->day);printf"%02d:%02d:%02d\n",tTime->hour,tTime->minute,tTime->second);}
运行结果:
2020.11.27 周五 更新
后续发现,gmtime)在MDK中计算的时间老是不对,调用该函数就有问题。
改用了另一个时间相关函数:
localtime)
struct tm *localtimeconst time_t *timep);
StmTime = localtime&lSec);