【Orangepi Zero2】localtime、asctime函数
- localtime、asctime
localtime、asctime
#include <time.h>
struct tm *localtime(const time_t *timep);
char *asctime(const struct tm *tm);
localtime()
是 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间,而 gmtime
函数转换后的时间没有经过时区变换,是UTC时间 。
asctime();
把timeptr指向的tm结构体中储存的时间转换为字符串,返回的字符串格式为:Www Mmm dd hh:mm:ss yyyy。其中Www为星期;Mmm为月份;dd为日;hh为时;mm为分;ss为秒;yyyy为年份。
Broken-down time is stored in the structure tm, which is defined in
<time.h> as follows:
struct tm {
int tm_sec; /* Seconds (0-60) */
int tm_min; /* Minutes (0-59) */
int tm_hour; /* Hours (0-23) */
int tm_mday; /* Day of the month (1-31) */
int tm_mon; /* Month (0-11) */
int tm_year; /* Year - 1900 */
int tm_wday; /* Day of the week (0-6, Sunday = 0) */
int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
int tm_isdst; /* Daylight saving time */
};
#include <time.h>
#include <stdio.h>
int main()
{
time_t t;
struct tm *my_tm;
my_tm = localtime(&t);
printf("tm_sec : %d\n",my_tm->tm_sec);
printf("tm_min : %d\n",my_tm->tm_min);
printf("tm_hour : %d\n",my_tm->tm_hour);
printf("tm_mday : %d\n",my_tm->tm_mday);
printf("tm_mon : %d\n",my_tm->tm_mon);
printf("tm_year : %d\n",my_tm->tm_year);
printf("tm_wday : %d\n",my_tm->tm_wday);
printf("tm_yday : %d\n",my_tm->tm_yday);
printf("tm_isdst : %d\n",my_tm->tm_isdst);
char *buf = asctime(localtime(&t));
printf("asctime : %s\n", buf);
return 0;
}