ubuntu下c語言編程獲取系統時間

#include <stdio.h>  
#include <sys/time.h>  
#include <time.h>  
int gettimeofday(struct timeval *tv, struct timezone *tz);  
int main(int argc,char * argv[])  
{  
  struct timeval tv;  
struct tm *gmt, *area;
  while(1)       {     gettimeofday(&tv,0);  
        //打印文件名和此段程序行數,並顯示系統時間和微秒數;
  printf("%s:%d linetime %u:%u\n",__FILE__,__LINE__,tv.tv_sec,tv.tv_usec);  
    
area = localtime(&(tv.tv_sec));以本地時區顯示時間
printf("Local time is: %s", asctime(area));
gmt = gmtime(&(tv.tv_sec));以GMT時區顯示時間
printf("GMT is: %s", asctime(gmt));
  sleep(2);    }    return 0;  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章