输出当前时间-linux-C

环境:linuc
编译器:GCC
输出当前时间

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>    //uintptr_t
#include <stdarg.h>    //va_start....
#include <unistd.h>    //STDERR_FILENO等
#include <sys/time.h>  //gettimeofday
#include <time.h>      //localtime_r
#include <fcntl.h>     //open
#include <errno.h>     //errno
int main()
{
    struct timeval   tv;
    struct tm        tm;
    memset(&tv,0,sizeof(struct timeval));
    memset(&tm,0,sizeof(struct tm));
    gettimeofday(&tv,NULL);

    time_t sec =tv.tv_sec;
    localtime_r(&sec,&tm);
    tm.tm_mon++;                 //月份要调整下正常
    tm.tm_year += 1900;          //年份要调整下才正常
    printf("time =  %4d/%02d/%02d %02d:%02d:%02d \n",     //格式是 年/月/日 时:分:秒
                    tm.tm_year, tm.tm_mon,
                    tm.tm_mday, tm.tm_hour,
                    tm.tm_min, tm.tm_sec);
    return 0;
}

编译和和运行

image

效果
image

posted @ 2021-05-12 14:34  appearAndLeave  阅读(170)  评论(0)    收藏  举报