linux基礎之C語言獲取時間


/////////////////////////////////////////////////////
//獲取系統當前時間,並轉換為當地時間顯示
/////////////////////////////////////////////////////

#include <stdio.h>
#include <time.h>

int main (void)
{
    time_t now;
    struct tm *ptm;
    //time() returns the time as the number of seconds since the Epoch,
    //1970-01-01 00:00:00 +0000 (UTC)
    //這里只能得到當前距離某個時間的總秒數,需要進一步轉換
    time (&now);
    //獲取當地日期和時間
    ptm = localtime (&now);
    //將轉換后的時間以字符串形式顯示
    printf ("now: %s", asctime(ptm));

    return 0;
}

結果

linux@ubuntu:~/c_file$ ./get_time 
now: Sun Dec  8 11:56:59 2019


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM