6.【Orangepi Zero2】localtime、asctime函数

【Orangepi Zero2】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;
}

在这里插入图片描述

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值