Linux中gmtime和localtime的區別

來源:互聯網
上載者:User

Linux中gmtime和localtime的區別

前段時間用到,寫了段小測試代碼,個人覺得足夠清楚的表明了二者的區別了,還是不明白的話,就看看APUE裡面的章節吧。

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

int main(int argc, char **argv)
{
    time_t tmpcal_ptr = {0};
    struct tm *tmp_ptr = NULL;

    tmpcal_ptr = time(NULL);
    printf("tmpcal_ptr=%d\n", tmpcal_ptr);   

    tmp_ptr = gmtime(&tmpcal_ptr);
    printf("after gmtime, the time is:\n%d:%d:%d", tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec);

    tmp_ptr = localtime(&tmpcal_ptr);
    printf("after localtime, the time is:\n%d:%d:%d", tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec);

    return 0;
}

運行結果如下:


基本的意思是,gmtime轉出來的是0時區的標準時間

localtime是將時區考慮在內了,轉出的當前時區的時間。但是注意,有些嵌入式裝置上被裁減過的系統,時區沒有被設定好,導致二者轉出來的時間都是0時區的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.