Linux获取当前时间日期asctime, asctime_r, gmtime, gmtime_r, localtime, localtime_r, mktime

本篇记录Linux环境下获取当前时间日期的函数asctime, asctime_r, gmtime, gmtime_r, localtime, localtime_r, mktime的基本使用。

开发环境Ubuntu1804

查看系统帮助文档

struct tm结果如下:

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 */
           };

1.gmtime函数

函数名gmtime
相关函数gmtime_r, asctime, asctime_r, localtime, localtime_r, mktime
表头文件#include<time.h>
函数定义struct tm *gmtime(const time_t *timep);
函数说明根据timep时间戳,转换成tm结构日期
返回值正常返回tm结果的指针,错误返回nullptr

示例:

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

using namespace std;


int main()
{
    time_t aa = time(nullptr);
    printf("aa==========================%ld\n", aa);
    timespec time1;
    int ret1 = clock_gettime(CLOCK_REALTIME, &time1);
    printf("ret1========%d tv_sec1=======%ld\n", ret1, time1.tv_sec);
    //char buf1[100];
    struct tm *tm1 = gmtime(&time1.tv_sec);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm1->tm_year + 1900, tm1->tm_mon + 1,
           tm1->tm_mday, tm1->tm_hour, tm1->tm_min, tm1->tm_sec);
    timespec time2;
    time2.tv_sec = time1.tv_sec + 10;
    //char buf2[100];
    struct tm *tm2 = gmtime(&time2.tv_sec);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm2->tm_year + 1900, tm2->tm_mon + 1,
           tm2->tm_mday, tm2->tm_hour, tm2->tm_min, tm2->tm_sec);
    cout << "Hello World!" << endl;
    return 0;
}


运行结果:

2.gmtime_r函数

函数名gmtime_r
相关函数gmtime, asctime, asctime_r, localtime, localtime_r, mktime
表头文件#include<time.h>
函数定义struct tm *gmtime_r(const time_t *timep, struct tm *result);
函数说明根据timep时间戳,转换成tm结构日期,成功保存在result中
返回值正常返回tm结果的指针,错误返回nullptr

示例:

#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>

using namespace std;


int main()
{
    time_t aa = time(nullptr);
    printf("aa==========================%ld\n", aa);
    timespec time1;
    int ret1 = clock_gettime(CLOCK_REALTIME, &time1);
    printf("ret1========%d tv_sec1=======%ld\n", ret1, time1.tv_sec);
    //char buf1[100];
    struct tm tm11;
    struct tm *tm1 = gmtime_r(&time1.tv_sec, &tm11);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm1->tm_year + 1900, tm1->tm_mon + 1,
           tm1->tm_mday, tm1->tm_hour, tm1->tm_min, tm1->tm_sec);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm11.tm_year + 1900, tm11.tm_mon + 1,
           tm11.tm_mday, tm11.tm_hour, tm11.tm_min, tm11.tm_sec);
    printf("tm1==============================%p\n", tm1);
    printf("tm11=============================%p\n\n", &tm11);

    timespec time2;
    time2.tv_sec = time1.tv_sec + 10;
    struct tm tm22;
    struct tm *tm2 = gmtime_r(&time2.tv_sec, &tm22);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm2->tm_year + 1900, tm2->tm_mon + 1,
           tm2->tm_mday, tm2->tm_hour, tm2->tm_min, tm2->tm_sec);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm22.tm_year + 1900, tm22.tm_mon + 1,
           tm22.tm_mday, tm22.tm_hour, tm22.tm_min, tm22.tm_sec);
    printf("tm2==============================%p\n", tm2);
    printf("tm22=============================%p\n\n", &tm22);
    cout << "Hello World!" << endl;
    return 0;
}

运行结果:

3.asctime函数

函数名asctime
相关函数gmtime, gmtime_r, asctime_r, localtime, localtime_r, mktime
表头文件#include<time.h>
函数定义char *asctime(const struct tm *tm);
函数说明根据tm时间,转换UTC字符串日期
返回值正常返回UTC日期字符串的指针,错误返回nullptr

示例:

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


using namespace std;


int main()
{
    time_t aa = time(nullptr);
    printf("aa==========================%ld\n", aa);
    timespec time1;
    int ret1 = clock_gettime(CLOCK_REALTIME, &time1);
    printf("ret1========%d tv_sec1=======%ld\n", ret1, time1.tv_sec);
    struct tm *tm1 = gmtime(&time1.tv_sec);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm1->tm_year + 1900, tm1->tm_mon + 1,
           tm1->tm_mday, tm1->tm_hour, tm1->tm_min, tm1->tm_sec);
    char *chtm1 = asctime(tm1);
    printf("chtm1===========%s\n", chtm1);
    timespec time2;
    time2.tv_sec = time1.tv_sec + 10;
    struct tm *tm2 = gmtime(&time2.tv_sec);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm2->tm_year + 1900, tm2->tm_mon + 1,
           tm2->tm_mday, tm2->tm_hour, tm2->tm_min, tm2->tm_sec);
    char *chtm2 = asctime(tm2);
    printf("chtm2===========%s\n", chtm2);

    cout << "Hello World!" << endl;
    return 0;
}

运行结果:

4.asctime_r函数

函数名asctime_r
相关函数gmtime, gmtime_r, asctime, localtime, localtime_r, mktime
表头文件#include<time.h>
函数定义char *asctime_r(const struct tm *tm, char *buf);
函数说明根据tm时间,转换UTC字符串日期,结果存在buf中
返回值正常返回UTC日期字符串的指针,错误返回nullptr

示例:

#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>

using namespace std;


int main()
{
    time_t aa = time(nullptr);
    printf("aa==========================%ld\n", aa);
    timespec time1;
    int ret1 = clock_gettime(CLOCK_REALTIME, &time1);
    printf("ret1========%d tv_sec1=======%ld\n", ret1, time1.tv_sec);
    struct tm *tm1 = gmtime(&time1.tv_sec);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm1->tm_year + 1900, tm1->tm_mon + 1,
           tm1->tm_mday, tm1->tm_hour, tm1->tm_min, tm1->tm_sec);
    char chbuf1[100];
    char *chtm1 = asctime_r(tm1, chbuf1);
    printf("chtm1===========%s\n", chtm1);
    printf("chbuf1==========%s\n", chbuf1);
    printf("chtm1===========%p\n", chtm1);
    printf("chbuf1==========%p\n", chbuf1);
    timespec time2;
    time2.tv_sec = time1.tv_sec + 10;
    struct tm *tm2 = gmtime(&time2.tv_sec);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm2->tm_year + 1900, tm2->tm_mon + 1,
           tm2->tm_mday, tm2->tm_hour, tm2->tm_min, tm2->tm_sec);
     char chbuf2[100];
    char *chtm2 = asctime_r(tm2, chbuf2);
    printf("chtm2===========%s\n", chtm2);
    printf("chbuf2==========%s\n", chbuf2);
    printf("chtm2===========%p\n", chtm2);
    printf("chbuf2==========%p\n", chbuf2);

    cout << "Hello World!" << endl;
    return 0;
}

运行结果:

5.localtime函数

函数名localtime
相关函数gmtime, gmtime_r, asctime, asctime_r, localtime_r, mktime
表头文件#include<time.h>
函数定义struct tm *localtime(const time_t *timep);
函数说明根据时间戳,转换本地时间tm结构体
返回值正常返回本地日期tm的指针,错误返回nullptr

示例:

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


using namespace std;



int main()
{
    time_t aa = time(nullptr);
    printf("aa==========================%ld\n", aa);
    timespec time1;
    int ret1 = clock_gettime(CLOCK_REALTIME, &time1);
    printf("ret1========%d tv_sec1=======%ld\n", ret1, time1.tv_sec);
    struct tm *tm1 = gmtime(&time1.tv_sec);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm1->tm_year + 1900, tm1->tm_mon + 1,
           tm1->tm_mday, tm1->tm_hour, tm1->tm_min, tm1->tm_sec);
    char *chtm1 = asctime(tm1);
    printf("chtm1===========%s\n", chtm1);
    struct tm *tm11 = localtime(&time1.tv_sec);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm11->tm_year + 1900, tm11->tm_mon + 1,
           tm11->tm_mday, tm11->tm_hour, tm11->tm_min, tm11->tm_sec);

    timespec time2;
    time2.tv_sec = time1.tv_sec + 10;
    struct tm *tm2 = gmtime(&time2.tv_sec);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm2->tm_year + 1900, tm2->tm_mon + 1,
           tm2->tm_mday, tm2->tm_hour, tm2->tm_min, tm2->tm_sec);
    char *chtm2 = asctime(tm2);
    printf("chtm2===========%s\n", chtm2);
    struct tm *tm22 = localtime(&time2.tv_sec);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm22->tm_year + 1900, tm22->tm_mon + 1,
           tm22->tm_mday, tm22->tm_hour, tm22->tm_min, tm22->tm_sec);


    cout << "Hello World!" << endl;
    return 0;
}

运行结果:

本地时间与UTC时间相差8小时

6.localtime_r函数

函数名localtime_r
相关函数gmtime, gmtime_r, asctime, asctime_r, localtime, mktime
表头文件#include<time.h>
函数定义struct tm *localtime_r(const time_t *timep, struct tm *result);
函数说明根据时间戳,转换本地时间tm结构体,结果存在buf中
返回值正常返回本地日期tm的指针,错误返回nullptr

示例:

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


using namespace std;

int main()
{
    time_t curtime = time(nullptr);
    printf("curtime==========================%ld\n", curtime);

    struct tm tm1;
    struct tm *tm11 = localtime_r(&curtime, &tm1);
    char *chtm1 = asctime(&tm1);
    printf("chtm1===========%s\n", chtm1);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm11->tm_year + 1900, tm11->tm_mon + 1,
           tm11->tm_mday, tm11->tm_hour, tm11->tm_min, tm11->tm_sec);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm1.tm_year + 1900, tm1.tm_mon + 1,
           tm1.tm_mday, tm1.tm_hour, tm1.tm_min, tm1.tm_sec);
    printf("tm1===========================%p\n", &tm1);
    printf("tm11==========================%p\n", tm11);

    time_t curtime2 = curtime + 10;
    struct tm tm2;
    struct tm *tm22 = localtime_r(&curtime2, &tm2);
    char *chtm2 = asctime(&tm2);
    printf("chtm2===========%s\n", chtm2);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm22->tm_year + 1900, tm22->tm_mon + 1,
           tm22->tm_mday, tm22->tm_hour, tm22->tm_min, tm22->tm_sec);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm2.tm_year + 1900, tm2.tm_mon + 1,
           tm2.tm_mday, tm2.tm_hour, tm2.tm_min, tm2.tm_sec);
    printf("tm2===========================%p\n", &tm2);
    printf("tm22==========================%p\n", tm22);


    cout << "Hello World!" << endl;
    return 0;
}

运行结果:

7.mktime函数

函数名mktime
相关函数gmtime, gmtime_r, asctime, asctime_r, localtime, localtime_r
表头文件#include<time.h>
函数定义time_t mktime(struct tm *tm);
函数说明根据tm时间转时间戳
返回值正常时间戳

示例:

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

using namespace std;

int main()
{
    time_t curtime = time(nullptr);
    struct tm *tm11 = localtime(&curtime);
    printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", tm11->tm_year + 1900, tm11->tm_mon + 1,
           tm11->tm_mday, tm11->tm_hour, tm11->tm_min, tm11->tm_sec);
    time_t ttim = mktime(tm11);
    printf("curtime==========================%ld\n", curtime);
    printf("ttim=============================%ld\n", ttim);

    cout << "Hello World!" << endl;
    return 0;
}

运行结果:

参考:

Linux获取当前时间 - Boblim - 博客园

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值