Linux下时间相关接口封装

本文详细介绍了Linux下与时间相关的系统调用,包括time、gmtime和localtime函数的使用,以及struct tm结构体成员的解释。同时,讨论了时间接口的封装,如获取当前本地和UTC时间的结构体及字符串表示,以及获取当前时区的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、相关系统调用
1、time函数
函数原型:time_t time(time_t *time)
头文件:#include <time.h>
功能:获得系统当前时间
返回值(长整型):成功返回从公元1970-1-1,00:00:00到当前的秒数,失败返回-1
参数(传出参数):同返回值

2、gmtime函数
函数原型:struct tm *gmtime(const time_t *time)
头文件:#include <time.h>
功能:将从公元1970-1-1,00:00:00到当前的秒数时间转换成日常使用的年月日形式
返回值:日常生活中所使用的时间表示,用结构体struct tm表示,utc时间,不加时区
参数(传入参数):从公元1970-1-1,00:00:00到当前的秒数时间,长整型

3、localtime函数
函数原型:struct tm *localtime(const time_t time)
头文件:#include <time.h>
功能:将utc秒时间转换为年月日时分秒的形式,并加时区,表征当地时间
返回值:struct tm
,当地时间
参数(传入参数):从公元1970-1-1,00:00:00到当前的秒数时间,长整型

4、struct tm结构体成员
struct tm {
int tm_sec; /* 秒 – 取值区间为[0,59] /
int tm_min; /
分 - 取值区间为[0,59] /
int tm_hour; /
时 - 取值区间为[0,23] /
int tm_mday; /
一个月中的日期 - 取值区间为[1,31] /
int tm_mon; /
月份(从一月开始,0代表一月) - 取值区间为[0,11] /
int tm_year; /
年份,其值等于实际年份减去1900 /
int tm_wday; /
星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一 /
int tm_yday; /
从每年1月1日开始的天数– 取值区间[0,365],其中0代表1月1日 */
int tm_isdst; // 夏令时标识符,夏令时tm_isdst为正;不实行夏令时tm_isdst为0
};

二、常用接口封装
1、获取当前本地时间,日常表示形式,使用结构体描述

int get_time_local_common(service_time_t *cur_time)
{
    time_t t;
    struct tm *tp;
    if((0 < time(&t)) && (NULL != (tp = localtime(&t))))
    {
        cur_time->year = tp->tm_year + 1900;
        cur_time->mouth = tp->tm_mon + 1;
        cur_time->week = tp->tm_wday;
        cur_time->day = tp->tm_mday;
        cur_time->hour = tp->tm_hour;
        cur_time->minute = tp->tm_min;
        cur_time->second = tp->tm_sec;
    }
    else
    {
        return -1;
    }
    return 0;
}

2、获取当前本地时间,日常表示形式,使用字符串描述

int get_time_local_string(char *time_str, size_t buf_len)
{
    time_t t;
    struct tm *tp;
    if((0 < time(&t)) && (NULL != (tp = localtime(&t))))
    {
        snprintf(time_str, buf_len, "%d/%02d/%02d %02d:%02d:%02d", 
            tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
    }
    else
    {
        return -1;
    }
    return 0;
}

3、获取当前utc时间,日常表示形式,使用结构体描述

int get_time_utc_common(service_time_t *utc_time)
{
    time_t t;
    struct tm *tp;
    if((0 < time(&t)) && (NULL != (tp = gmtime(&t))))
    {
        utc_time->year = tp->tm_year + 1900;
        utc_time->mouth = tp->tm_mon + 1;
        utc_time->week = tp->tm_wday;
        utc_time->day = tp->tm_mday;
        utc_time->hour = tp->tm_hour;
        utc_time->minute = tp->tm_min;
        utc_time->second = tp->tm_sec;
    }
    else
    {
        return -1;
    }
    return 0;
}

4、获取当前utc时间,日常表示形式,使用字符串描述

int get_time_utc_string(char *time_str, size_t buf_len)
{
    time_t t;
    struct tm *tp;
    if((0 < time(&t)) && (NULL != (tp = gmtime(&t))))
    {
         snprintf(time_str, buf_len, "%d/%02d/%02d %02d:%02d:%02d", 
            tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
    }
    else
    {
        return -1;
    }
    return 0;
}

5、获取当前时区

int g_timezone = -100;
int get_timezone()
{
    if(-100 == g_timezone)
    {
        time_t t;
        struct tm *tp;
        if(0 < time(&t))
        {
            int second_utc = t % (24 * 60 * 60);
            if(NULL != (tp = localtime(&t)))
            {
                int second_local = tp->tm_hour * 3600 + tp->tm_min * 60 + tp->tm_sec;
                g_timezone = (second_local - second_utc) / (60 * 60);
                if(12 < g_timezone)
                {
                    g_timezone = 24 - timezone;
                }
            }
        }

    }
    return g_timezone; 
}

6、时间表示结构体

typedef struct 
{
    int year;
    int mouth;
    int day;
    int week;
    int hour;
    int minute;
    int second;
}service_time_t;

7、测试主函数

int main()
{
    service_time_t time;
    printf("timezone = %d\n", get_timezone());

    if(0 <= get_time_local_common(&time))
    {
        printf("local time : %d/%d/%d week:%d  %d:%d:%d\n", time.year, time.mouth, time.day,
            time.week, time.hour, time.minute, time.second);
    }
    if(0 <= get_time_utc_common(&time))
    {
        printf("  utc time : %d/%d/%d week:%d  %d:%d:%d\n", time.year, time.mouth, time.day,
            time.week, time.hour, time.minute, time.second);
    }

    char time_str[30];
    if(0 <= get_time_local_string(time_str, sizeof(time_str)))
    {
        printf("local time string : %s\n", time_str);
    }

    if(0 <= get_time_utc_string(time_str, sizeof(time_str)))
    {
        printf("  utc time string : %s\n", time_str);
    }
    
    return 0;
}

8、测试程序输出

timezone = 8
local time : 2020/9/29 week:2  21:40:25
  utc time : 2020/9/29 week:2  13:40:25
local time string : 2020/09/29 21:40:25
  utc time string : 2020/09/29 13:40:25

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值