• C语言time()函数的用法


    time()函数

    1. time()函数的用途

    time_t time(time_t *t);

    函数说明:此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数(即格林尼治时间1970年1月1日00:00:00到当前时刻的时长,时长单位是秒)。如果t并非空指针的话,此函数也会将返回值存在t指针所指的内存。
    返回值:成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于error中。

    从声明中可以看出,time()函数返回值的数据类型是time_t。传递给time()函数的参数是指向time_t数据类型的指针。

    2. time()函数的头文件

    要使用time(),必须在程序中包含 ##include 头文件。

    3. time()函数返回的数据类型

    下面是从文件中找到的函数声明:

    time_t time(time_t *t)
    time(time_t *t)
    
    • 1
    • 2

    从声明中可以看出,time()函数返回值的数据类型是 time_t 。传递给time()函数的参数是指向time_t数据类型的指针。

    4. time()函数使用示例

    time()函数有两种使用方式:

    (1) t1=time(NULL)或t1=time(0)

    将空指针传递给time()函数,并将time()返回值赋给变量t1

    (2) time(&t2);

    将变量t2的地址作为实参传递给time()函数,函数自动把结果传递给t2,不需要额外的赋值语句。
    示例代码:

        #include 
        #include 
         
        int main()
        {
            time_t t1,t2; //分别声明两种使用方式的赋值对象
        	
            t1=time(0);   //第一种使用方式
            time(&t2);    //第二种使用方式
        	
            printf("t1=%ld\n",t1);
            printf("t2=%ld",t2);
        	
            return 0;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    localtime函数

    struct tm* localtime(const time_t *timep);

    函数说明:localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。结构tm的定义可以参考gmtime()。此函数返回的时间日期已经转换成当地时区。
    返回值:返回结构tm代表目前的当地时间。


    例子1

    #include 
    #include 
    #include 
    
    #define BUF_SIZE 200
    
    typedef struct {
        int year;
        int month;
        int day;
        int hour;
        int minute;
        int second;
    }Time_YMD_HMS;
    
    char* getNowTime()
    {
        Time_YMD_HMS *curDate =(Time_YMD_HMS *)malloc(sizeof(Time_YMD_HMS));
        char *timeBuf =(char *)malloc(BUF_SIZE);
        //bzero(timeBuf,BUF_SIZE);
        //bzero(curDate,sizeof(Time_YMD_HMS));
        memset(timeBuf,0,BUF_SIZE);
        memset(curDate,0,sizeof(Time_YMD_HMS));
        time_t now;
        struct tm *timeNow;
        time(&now);
        timeNow = localtime(&now);
        curDate->year=timeNow->tm_year+1900;
        curDate->month=timeNow->tm_mon+1;
        curDate->day=timeNow->tm_mday;
        curDate->hour=timeNow->tm_hour;
        curDate->minute=timeNow->tm_min;
        curDate->second=timeNow->tm_sec;
        // yyyy-MM-dd HH:mm:ss
        if(curDate->second < 10)
            sprintf(timeBuf, "%d-%d-%d %d:%d:0%d",curDate->year,curDate->month,curDate->day,
                curDate->hour,curDate->minute,curDate->second);
        else
            sprintf(timeBuf, "%d-%d-%d %d:%d:%d",curDate->year,curDate->month,curDate->day,
                curDate->hour,curDate->minute,curDate->second);
        free(curDate);
        return timeBuf;
    }
    
    int main()
    {
        while (1)
        {
            printf("%s \n",getNowTime());
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    例子2

    #include 
    #include 
    #include 
    #include 
    
    void gettime(char *cur_time) {
            char Year[6] = {0};
            char Month[4] = {0};
            char Day[4] = {0};
            char Hour[4] = {0};
            char Min[4] = {0};
            char Sec[4] = {0};
    
            time_t current_time;
            struct tm* now_time;
            time(&current_time);
            now_time = localtime(&current_time);
    
            strftime(Year, sizeof(Year), "%Y-", now_time);
            strftime(Month, sizeof(Month), "%m-", now_time);
            strftime(Day, sizeof(Day), "%d ", now_time);
            strftime(Hour, sizeof(Hour), "%H:", now_time);
            strftime(Min, sizeof(Min), "%M:", now_time);
            strftime(Sec, sizeof(Sec), "%S", now_time);
    
            strncat(cur_time, Year, 5);
            strncat(cur_time, Month, 3);
            strncat(cur_time, Day, 3);
            strncat(cur_time, Hour, 3);
            strncat(cur_time, Min, 3);
            strncat(cur_time, Sec, 3);
    }
    
    int main() {
            char *cur_time = (char *)malloc(21*sizeof(char));
            gettime(cur_time);
            printf("Current time: %s\n", cur_time);
            free(cur_time);
            cur_time = NULL;
            return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    内核的时间打印

    void sundp_get_time()
    {
        struct tm tm1;
    
        time_t local_time1;
        local_time1 = ktime_get_real_seconds();
        time64_to_tm(local_time1, -sys_tz.tz_minuteswest * 60, &tm1);
        tm1.tm_year += 1900;
        tm1.tm_mon += 1;
        tm1.tm_mday += 2;
    
        uvc_printk(KERN_INFO, "sundp_ %d_%d_%d %d:%d.%d   ",tm1.tm_year,tm1.tm_mon,tm1.tm_mday,tm1.tm_hour,tm1.tm_min,tm1.tm_sec);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    参考链接:

    获取当前时间,要用到time.h中的time()和localtime()函数,二者具体介绍与使用,参见:
    C中时间/环境/终端控制相关函数
    c语言time()函数用法,参见:
    C语言time()函数的用法
    c++时间结构体(time_t和tm)

  • 相关阅读:
    win安装vue并运行 vue-admin-template
    一零一九、岗位数据分析(Spark)
    OPPO手机如何添加日程桌面插件?
    Flutter笔记:光影动画按钮、滚动图标卡片组等
    如何使用element-ui相关组件如:el-select,el-table,el-switch,el-pagination,el-dialog
    Direct3D拾取
    流行语来源查询易语言代码
    GBase 8c V3.0.0数据类型——网络地址函数和操作符(macaddr函数)
    Golang sync.Map 原理(两个map实现 读写分离、适用读多写少场景)
    MATLIB从excel表中读取数据并画出函数图像
  • 原文地址:https://blog.csdn.net/weixin_42581177/article/details/127553855