zoukankan      html  css  js  c++  java
  • linux 中 gmtime和 localtime区别

    gmtime和localtime

    struct tm *gmtime(const time_t *timep);
    struct tm *gmtime_r(const time_t *timep, struct tm *result);

    struct tm *localtime(const time_t *timep);
    struct tm *localtime_r(const time_t *timep, struct tm *result);

    参数都是time_t,the number of seconds elapsed since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).

    但是gmtime 返回的 tm时间是UTC时区的时间。

    localtime 返回得时间是系统设置的时区的时间。

    有段代码可以看一下

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

    int main()
    {

    time_t time;
    struct tm tm;
    struct tm *tm_ret;

    tm.tm_year = 2020-1900;
    tm.tm_mon = 9-1;
    tm.tm_mday = 10;
    tm.tm_hour = 16;
    tm.tm_min = 30;
    tm.tm_sec = 0;

    time= mktime(&tm);

    printf("time:%lld ", time);

    tm_ret = gmtime(&time);
    printf("%d/%02d/%02d %02d:%02d:%02d ", tm_ret->tm_year +1900, tm_ret->tm_mon +1, tm_ret->tm_mday, tm_ret->tm_hour, tm_ret->tm_min, tm_ret->tm_sec);

    tm_ret = localtime(&time);
    printf("%d/%02d/%02d %02d:%02d:%02d ", tm_ret->tm_year +1900, tm_ret->tm_mon +1, tm_ret->tm_mday, tm_ret->tm_hour, tm_ret->tm_min, tm_ret->tm_sec);
    return 0;
    }

    结果是:

    time:1599726600
    2020/09/10 08:30:00
    2020/09/10 16:30:00



  • 相关阅读:
    ES5-Array的新增方法
    ES5-Object扩展方法
    JS利用HTML5的Web Worker实现多线程
    git合并分支到master上面
    JS线程及回调函数执行
    JS实现继承
    蓝桥杯 高精度加法
    蓝桥杯 阶乘计算
    【题集】k倍区间(抽屉原理)
    代码填空:全排列
  • 原文地址:https://www.cnblogs.com/blueyunchao0618/p/13646539.html
Copyright © 2011-2022 走看看