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

猜你喜欢

转载自www.cnblogs.com/risesource/p/12005185.html