C语言time()函数的用法

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

文章目录

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 <time.h> 头文件。

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

下面是从<time.h>文件中找到的函数声明

time_t time(time_t *t)
time(time_t *t)

从声明中可以看出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 <stdio.h>
    #include <time.h>
     
    int main()
    {
        time_t t1,t2; //分别声明两种使用方式的赋值对象
    	
        t1=time(0);   //第一种使用方式
        time(&t2);    //第二种使用方式
    	
        printf("t1=%ld\n",t1);
        printf("t2=%ld",t2);
    	
        return 0;
    }

localtime函数

struct tm* localtime(const time_t *timep);

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


例子1

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

#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;
}

例子2

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

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;
}

内核的时间打印

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);
}

参考链接

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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

“C语言time()函数的用法” 的相关文章