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