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



  • 相关阅读:
    oracle 误删除表的几种恢复方法
    解决js在alert或者断点调试时才能赋值
    常用的Debug方式
    字节对齐
    CWnd::SetWindowPos的注意事项
    网络模块代码调试要点
    stub和mock
    全局变量的缺陷
    SVN切换地址
    C/C++如何得到int型最大值
  • 原文地址:https://www.cnblogs.com/blueyunchao0618/p/13646539.html
Copyright © 2011-2022 走看看