日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux下获取系统时间的方法

發布時間:2024/9/5 linux 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux下获取系统时间的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
可以用 localtime 函數分別獲取年月日時分秒的數值。

  Linux下獲得系統時間的C語言的實現方法:

  1. 可以用 localtime 函數分別獲取年月日時分秒的數值。

  #include<time.h>???? //C語言的頭文件

  #include<stdio.h>???? //C語言的I/O

  void?? main()

  {

  time_t?? now;???????? //實例化time_t結構

  struct?? tm???? *timenow;???????? //實例化tm結構指針

  time(&now);

  //time函數讀取現在的時間(國際標準時間非北京時間),然后傳值給now

  timenow?? =?? localtime(&now);

  //localtime函數把從time取得的時間now換算成你電腦中的時間(就是你設置的地區)

  printf("Local?? time?? is?? %s\n",asctime(timenow));

  //上句中asctime函數把時間轉換成字符,通過printf()函數輸出

  }

  注釋:time_t是一個在time.h中定義好的結構體。而tm結構體的原形如下:

  struct?? tm

  {

  int?? tm_sec;//seconds?? 0-61

  int?? tm_min;//minutes?? 1-59

  int?? tm_hour;//hours?? 0-23

  int?? tm_mday;//day?? of?? the?? month?? 1-31

  int?? tm_mon;//months?? since?? jan?? 0-11

  int?? tm_year;//years?? from?? 1900

  int?? tm_wday;//days?? since?? Sunday,?? 0-6

  int?? tm_yday;//days?? since?? Jan?? 1,?? 0-365

  int?? tm_isdst;//Daylight?? Saving?? time?? indicator

  };

2. 對某些需要較高精準度的需求,Linux提供了gettimeofday()。

  #include?? <stdio.h>

  #include?? <stdlib.h>

  #include?? <sys/time.h>

  int? main(int argc,?? char **argv)

  {

  struct?? tim?? start,stop,diff;

  gettimeofday(&start,0);

  //做你要做的事...

  gettimeofday(&stop,0);

  tim_subtract(&diff,&start,&stop);

  printf("總計用時:%d毫秒\n",diff.tv_usec);

  }

  int tim_subtract(struct tim *result, struct tim *x, struct tim *y)

  {

  int nsec;

  if ( x->tv_sec > y->tv_sec )

  return?? -1;

  if ((x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec))

  return?? -1;

  result->tv_sec = ( y->tv_sec-x->tv_sec );

  result->tv_usec = ( y->tv_usec-x->tv_usec );

  if (result->tv_usec<0)

  {

  result->tv_sec--;

  result->tv_usec+=1000000;

  }

  return?? 0;

  }


總結

以上是生活随笔為你收集整理的linux下获取系统时间的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。