c 时间 学习

linux

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

int main(int argc,char **argv)
{
    //两种时间的获取方法
    struct timeval tv;
    gettimeofday(&tv,NULL);
    time_t t=time(0);
    printf("%u---%u+++%u
",tv.tv_sec,tv.tv_usec,t);
    //时间格式转换
    char tmp[64];
    strftime(tmp,sizeof(tmp),"%Y%m%d%H%M%S
",localtime(&t));
    printf("%s",tmp);
    return 0;
}

windows

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

int main(int argc, char* argv[])
{
    //windows没有timeval结构,当然也没有gettimeofday函数
    //struct timeval tv;
    //gettimeofday(&tv,NULL);
    
    time_t t=time(0);
    char tmp[64];
    strftime(tmp,sizeof(tmp),"%Y%m%d-%H%M%S",localtime(&t));
    printf("%s",tmp);
    
    return 0;
}
原文地址:https://www.cnblogs.com/bloodofhero/p/4148079.html