Linux下用户态mktime实现参考

1、背景

继上周《Linux下mktime耗时测试》,本次通过参考glibc、内核源码进行实验

2、接口说明

时间转换接口,主要用于所指向的结构转换为自 1970 年1月1日以来持续时间的秒数,发生错误时返回-1。

SYNOPSIS         top
       #include <time.h>

       time_t mktime(struct tm *timeptr);
DESCRIPTION
       The functionality described on this reference page is aligned
       with the ISO C standard. Any conflict between the requirements
       described here and the ISO C standard is unintentional. This
       volume of POSIX.12017 defers to the ISO C standard.

       The mktime() function shall convert the broken-down time,
       expressed as local time, in the structure pointed to by timeptr,
       into a time since the Epoch value with the same encoding as that
       of the values returned by time().  The original values of the
       tm_wday and tm_yday components of the structure shall be ignored,
       and the original values of the other components shall not be
       restricted to the ranges described in <time.h>.
---
       Broken-down time is stored in the structure tm, which is defined in <time.h> as follows:

           struct tm {
   
               int tm_sec;    /* Seconds (0-60) */
               int tm_min;    /* Minutes (0-59) */
               int tm_hour;   /* Hours (0-23) */
               int tm_mday;   /* Day of the month (1-31) */
               int tm_mon;    /* Month (0-11) */
               int tm_year;   /* Year - 1900 */
               int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
               int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
               int tm_isdst;  /* Daylight saving time */
           };

3、实现

3.1 参考内核实现(最简单)

核心接口实现,几行代码就搞定


/* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
 *
 * [For the Julian calendar (which was used in Russia before 1917,
 * Britain & colonies before 1752, anywhere else before 1582,
 * and is still in use by some communities) leave out the
 * -year/100+year/400 terms, and add 10.]
 *
 * This algorithm was first published by Gauss (I think).
 *
 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
 * machines where long is 32-bit! (However, as time_t is signed, we
 * will already get problems at other places on 2038-01-19 03:14:08)
 */
unsigned long __mktime_kernel(const unsigned int year0, const unsigned int mon0,
                              const unsigned int day,   const unsigned int hour,
                              const unsigned int min,   const unsigned int sec)
{
   
    unsigned int mon = mon0, year = year0;

    /* 1..12 -> 11,12,1..10 */
    if (0 >= (int)(mon -= 2)) {
   
        mon += 12;  /* Puts Feb last since it has leap day */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值