歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux系統如何辨別gmtime和localtime

Linux系統如何辨別gmtime和localtime

日期:2017/3/2 11:42:09   编辑:Linux技術

  gmtime和localtime是兩種不同的函數,不少人在使用的時候容易將兩者混淆,下面小編就教你Linux下如何辨別gmtime和localtime的使用,以便你下次能夠正確使用。

  區別:

  #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;

  }

  運行結果如下:

 Linux系統如何辨別gmtime和localtime

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

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

  上面就是Linux區別gmtime和localtime函數的相關介紹了,從上面的代碼運行結果中可以看出,gmtime和localtime的用法是有些區別的。

Copyright © Linux教程網 All Rights Reserved