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時區的。