国产宅男网站在线|亚洲A级性爱免费视频|亚洲中精品级在线|午夜福利AA毛

  • <dd id="gf5jf"><th id="gf5jf"></th></dd>

    <cite id="gf5jf"><label id="gf5jf"></label></cite>
  • <div id="gf5jf"><listing id="gf5jf"></listing></div>
    學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 操作系統(tǒng) > Linux教程 >

    Linux下用C獲取當(dāng)前時(shí)間命令是什么

    時(shí)間: 加城1195 分享

      Linux繼承了Unix以網(wǎng)絡(luò)為核心的設(shè)計(jì)思想,是一個(gè)性能穩(wěn)定的多用戶網(wǎng)絡(luò)操作系統(tǒng)。本篇文章主要介紹了Linux獲取當(dāng)前時(shí)間的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

      Linux下用C獲取當(dāng)前時(shí)間,具體如下:

      代碼(可以把clock_gettime換成time(NULL))

      void getNowTime()

      {

      timespec time;

      clock_gettime(CLOCK_REALTIME, &time); //獲取相對(duì)于1970到現(xiàn)在的秒數(shù)

      tm nowTime;

      localtime_r(&time.tv_sec, &nowtime);

      char current[1024];

      sprintf(current, "%04d%02d%02d%02d:%02d:%02d", nowTime.tm_year + 1900, nowTime.tm_mon, nowTime.tm_mday,

      nowTime.tm_hour, nowTime.tm_min, nowTime.tm_sec);

      }

      分析:

      clock_gettime()

      函數(shù)"clock_gettime"是基于Linux C語(yǔ)言的時(shí)間函數(shù),他可以用于計(jì)算精度和納秒。

      語(yǔ)法:

      #include

      int clock_gettime(clockid_t clk_id,struct timespec *tp);

      參數(shù):

      clk_id : 檢索和設(shè)置的clk_id指定的時(shí)鐘時(shí)間。

      CLOCK_REALTIME:系統(tǒng)實(shí)時(shí)時(shí)間,隨系統(tǒng)實(shí)時(shí)時(shí)間改變而改變,即從UTC1970-1-1 0:0:0開始計(jì)時(shí),中間時(shí)刻如果系統(tǒng)時(shí)間被用戶改成其他,則對(duì)應(yīng)的時(shí)間相應(yīng)改變

      CLOCK_MONOTONIC:從系統(tǒng)啟動(dòng)這一刻起開始計(jì)時(shí),不受系統(tǒng)時(shí)間被用戶改變的影響

      CLOCK_PROCESS_CPUTIME_ID:本進(jìn)程到當(dāng)前代碼系統(tǒng)CPU花費(fèi)的時(shí)間

      CLOCK_THREAD_CPUTIME_ID:本線程到當(dāng)前代碼系統(tǒng)CPU花費(fèi)的時(shí)間

      struct timespec

      {

      time_t tv_sec; /* 秒*/

      long tv_nsec; /* 納秒*/

      };

      localtime()

      localtime是 把從1970-1-1零點(diǎn)零分到當(dāng)前時(shí)間系統(tǒng)所偏移的秒數(shù)時(shí)間轉(zhuǎn)換為本地時(shí)間.

      語(yǔ)法

      說明:此函數(shù)獲得的tm結(jié)構(gòu)體的時(shí)間是日歷時(shí)間。

      用 法: struct tm *localtime(const time_t *clock);

      返回值:返回指向tm 結(jié)構(gòu)體的指針.tm結(jié)構(gòu)體是time.h中定義的用于分別存儲(chǔ)時(shí)間的各個(gè)量(年月日等)的結(jié)構(gòu)體.

      例1:

      #include

      #include

      #include

      int main(void)

      {

      time_t timer;//time_t就是long int 類型

      struct tm *tblock;

      timer = time(NULL);

      tblock = localtime(&timer);

      printf("Local time is: %s\n", asctime(tblock));

      return 0;

      }

      執(zhí)行結(jié)果:

      Local time is: Mon Feb 16 11:29:26 2009

      例2:

      上面的例子用了asctime函數(shù),下面這個(gè)例子不使用這個(gè)函數(shù)一樣能獲取系統(tǒng)當(dāng)前時(shí)間。需要注意的是年份加上1900,月份加上1。

      #include

      #include

      int main()

      {

      struct tm *t;

      time_t tt;

      time(&tt);

      t = localtime(&tt);

      printf("%4d年%02d月%02d日 %02d:%02d:%02d\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);

      return 0;

      }

      localtime()與localtime_r()的區(qū)別

      localtime():

      #include

      #include

      #include

      #include

      using namespace std;

      int main(int argc, char *argv[])

      {

      time_t tNow =time(NULL);

      time_t tEnd = tNow + 1800;

      //注意下面兩行的區(qū)別

      struct tm* ptm = localtime(&tNow);

      struct tm* ptmEnd = localtime(&tEnd);

      char szTmp[50] = {0};

      strftime(szTmp,50,"%H:%M:%S",ptm);

      char szEnd[50] = {0};

      strftime(szEnd,50,"%H:%M:%S",ptmEnd);

      printf("%s /n",szTmp);

      printf("%s /n",szEnd);

      system("PAUSE");

      return EXIT_SUCCESS;

      }

      最后出來的結(jié)果是:

      21:18:39

      21:18:39

      和最初想法不一致。

      查閱localtime的文檔,發(fā)現(xiàn)這段話:

      This structure is statically allocated and shared by the functions gmtime and localtime. Each time either one of these functions is called the content of this structure is overwritten.

      也就是說每次只能同時(shí)使用localtime()函數(shù)一次,要不就會(huì)被重寫!

      The localtime() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe.

      因此localtime()不是可重入的。同時(shí)libc里提供了一個(gè)可重入版的函數(shù)localtime_r();

      Unlike localtime(), the reentrant version is not required to set tzname。

      修改程序:(localtime_r())

      #include

      #include

      #include

      #include

      using namespace std;

      int main(int argc, char *argv[])

      {

      time_t tNow =time(NULL);

      time_t tEnd = tNow + 1800;

      //在這里修改程序

      //struct tm* ptm = localtime(&tNow);

      //struct tm* ptmEnd = localtime(&tEnd);

      struct tm ptm = { 0 };

      struct tm ptmEnd = { 0 };

      localtime_r(&tNow, &ptm);

      localtime_r(&tEnd, &ptmEnd);

      char szTmp[50] = {0};

      strftime(szTmp,50,"%H:%M:%S",&ptm);

      char szEnd[50] = {0};

      strftime(szEnd,50,"%H:%M:%S",&ptmEnd);

      printf("%s /n",szTmp);

      printf("%s /n",szEnd);

      system("PAUSE");

      return EXIT_SUCCESS;

      }

      最后出來的結(jié)果是:

      10:29:06

      10:59:06

      tm

      struct tm {

      int tm_sec; /* 秒 – 取值區(qū)間為[0,59] */

      int tm_min; /* 分 - 取值區(qū)間為[0,59] */

      int tm_hour; /* 時(shí) - 取值區(qū)間為[0,23] */

      int tm_mday; /* 一個(gè)月中的日期 - 取值區(qū)間為[1,31] */

      int tm_mon; /* 月份(從一月開始,0代表一月) - 取值區(qū)間為[0,11] */

      int tm_year; /* 年份,其值等于實(shí)際年份減去1900 */

      int tm_wday; /* 星期 – 取值區(qū)間為[0,6],其中0代表星期天,1代表星期一 */

      int tm_yday; /* 從每年1月1日開始的天數(shù)– 取值區(qū)間[0,365],其中0代表1月1日 */

      int tm_isdst; /* 夏令時(shí)標(biāo)識(shí)符,夏令時(shí)tm_isdst為正;不實(shí)行夏令時(shí)tm_isdst為0 */

      };

      time 函數(shù)

      返回:1970-1-1, 00:00:00以來經(jīng)過的秒數(shù)

      原型: time_t time(time_t *calptr)

      結(jié)果可以通過返回值,也可以通過參數(shù)得到,見實(shí)例

      頭文件

      返回值:

      成功:秒數(shù),從1970-1-1,00:00:00 可以當(dāng)成整型輸出或用于其它函數(shù)

      失?。?1

      time_t now;

      time(&now);// 等同于now = time(NULL)

      printf("now time is %d\n", now);

      補(bǔ)充:Linux基本命令

      1.ls命令:

      格式::ls [選項(xiàng)] [目錄或文件]

      功能:對(duì)于目錄,列出該目錄下的所有子目錄與文件;對(duì)于文件,列出文件名以及其他信息。

      常用選項(xiàng):

      -a :列出目錄下的所有文件,包括以 . 開頭的隱含文件。

      -d :將目錄像文件一樣顯示,而不是顯示其他文件。

      -i :輸出文件的i節(jié)點(diǎn)的索引信息。

      -k :以k字節(jié)的形式表示文件的大小。

      -l :列出文件的詳細(xì)信息。

      -n :用數(shù)字的UID,GID代替名稱。

      -F : 在每個(gè)文件名后面附上一個(gè)字符以說明該文件的類型,“*”表示可執(zhí)行的普通文 件;“/”表示目錄;“@”表示符號(hào)鏈接;“l”表示FIFOS;“=”表示套接字。

      2.cd命令

      格式:cd [目錄名稱]

      常用選項(xiàng):

      cd .. 返回上一級(jí)目錄。

      cd ../.. 將當(dāng)前目錄向上移動(dòng)兩級(jí)。

      cd - 返回最近訪問目錄。

      3.pwd命令

      格式: pwd

      功能:顯示出當(dāng)前工作目錄的絕對(duì)路徑。

      相關(guān)閱讀:Linux主要特性

      完全兼容POSIX1.0標(biāo)準(zhǔn)

      這使得可以在Linux下通過相應(yīng)的模擬器運(yùn)行常見的DOS、Windows的程序。這為用戶從Windows轉(zhuǎn)到Linux奠定了基礎(chǔ)。許多用戶在考慮使用Linux時(shí),就想到以前在Windows下常見的程序是否能正常運(yùn)行,這一點(diǎn)就消除了他們的疑慮。

      多用戶、多任務(wù)

      Linux支持多用戶,各個(gè)用戶對(duì)于自己的文件設(shè)備有自己特殊的權(quán)利,保證了各用戶之間互不影響。多任務(wù)則是現(xiàn)在電腦最主要的一個(gè)特點(diǎn),Linux可以使多個(gè)程序同時(shí)并獨(dú)立地運(yùn)行。

      良好的界面

      Linux同時(shí)具有字符界面和圖形界面。在字符界面用戶可以通過鍵盤輸入相應(yīng)的指令來進(jìn)行操作。它同時(shí)也提供了類似Windows圖形界面的X-Window系統(tǒng),用戶可以使用鼠標(biāo)對(duì)其進(jìn)行操作。在X-Window環(huán)境中就和在Windows中相似,可以說是一個(gè)Linux版的Windows。

      支持多種平臺(tái)

      Linux可以運(yùn)行在多種硬件平臺(tái)上,如具有x86、680x0、SPARC、Alpha等處理器的平臺(tái)。此外Linux還是一種嵌入式操作系統(tǒng),可以運(yùn)行在掌上電腦、機(jī)頂盒或游戲機(jī)上。2001年1月份發(fā)布的Linux 2.4版內(nèi)核已經(jīng)能夠完全支持Intel 64位芯片架構(gòu)。同時(shí)Linux也支持多處理器技術(shù)。多個(gè)處理器同時(shí)工作,使系統(tǒng)性能大大提高。


    Linux下用C獲取當(dāng)前時(shí)間相關(guān)文章:

    1.linux shell獲取當(dāng)前時(shí)間命令

    2.Linux C語(yǔ)言時(shí)間函數(shù)

    3.linux查看java進(jìn)程命令

    4.Linux下如何編寫第一個(gè)C語(yǔ)言hello world程序

    5.Linux C語(yǔ)言怎樣使用文本編輯器

    4018918