mktime 返回指定时间的日历格式

头文件

#include <time.h>

语法

time_t mktime( struct tm *time );

功能

函数转换参数 time 类型的本地时间至日历时间,并返回结果。如果发生错误,返回 -1。

示例

#include <stdio.h>
#include <time.h>

int main () {
    int ret;
    struct tm info;
    char buffer[80];

    info.tm_year = 2021 - 1900;
    info.tm_mon = 7 - 1;
    info.tm_mday = 4;
    info.tm_hour = 0;
    info.tm_min = 0;
    info.tm_sec = 1;
    info.tm_isdst = -1;

    ret = mktime(&info);
    if( ret == -1 ) {
        printf("Error: unable to make time using mktime\n");
    } else {
        strftime(buffer, sizeof(buffer), "%c", &info );
        printf(buffer);
    }

    return(0);
}

输出结果:

07/04/21 00:00:01
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号