Linux源码中的mktime算法解析

Linux源码中的mktime算法解析

    我们知道,从CMOS中读出来的系统时间并不是time_t类型,而是类似于struct tm那样,年月日时分秒是分开存储的。

    那么,要把它转化为系统便于处理的time_t类型,就需要算法进行转换。

    我们都知道我们的公历还是比较复杂的,有大月小月,有闰年非闰年,处理起来会很麻烦。

    但是Linux的源代码仅仅用了短短的几行就完成了这个复杂的转换(Gauss算法),实在令人惊奇。话不多说,先看源代码:

include/linux/time.h

static inline unsigned long  mktime ( unsigned int  year, unsigned int  mon,
   
unsigned int  day, unsigned int  hour,
   
unsigned int  min, unsigned int  sec)
        
... {
    
if (0 >= (int) (mon -= 2)) ...{    /**//* 1..12 -> 11,12,1..10 */
         mon 
+= 12;      /**//* Puts Feb last since it has leap day */
         year 
-= 1;
    }


    
return (((
             (unsigned 
long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
             year
*365 - 719499
          )
*24 + hour /**//* now have hours */
       )
*60 + min /**//* now have minutes */
    )
*60 + sec; /**//* finally seconds */
}

    看上去令人眼花缭乱,毫无头绪。详情请阅读原文:http://blog.csdn.net/axx1611/article/details/1792827

 

应用:

/*功能: 返回YYYYMMDDHHMMSS格式的时间(sTime + sec);
*char[] sTime: YYYYMMDDHHMMSS
*int sec: 秒数 (sec 可以是负数)
*/

int StrTimeAddSec(char *sTime, int sec)
{
 struct tm *pTime = new struct tm;
 struct tm *pTime2;
 time_t th;

 pTime->tm_sec = atoi(sTime + 12) + sec;//秒
 sTime[12] = '\0';
 pTime->tm_min = atoi(sTime + 10);//分
 sTime[10] = '\0';
 pTime->tm_hour = atoi(sTime + 8);//小时
 sTime[8] = '\0';
 pTime->tm_mday = atoi(sTime + 6);//日
 sTime[6] = '\0';
 pTime->tm_mon = atoi(sTime + 4) - 1;//月份
 sTime[4] = '\0';
 pTime->tm_year = atoi(sTime) - 1900;//年

 //th = mktime(pTime) + sec;
 mktime(pTime);//自动调整时间
 //pTime2 = localtime(&th);
 //memset(sTime, 0, sizeof(sTime));

 sprintf(sTime, "%04d%02d%02d%02d%02d%02d", pTime->tm_year + 1900, pTime->tm_mon + 1, pTime->tm_mday, pTime->tm_hour, pTime->tm_min, pTime->tm_sec);

 //delete pTime2;//不用delete,因为localtime的返回值是个static的地址
 delete pTime;
 return 0;
}

 

static __time64_t __cdecl _make__time64_t ( struct tm *tb, int ultflag ) { __time64_t tmptm1, tmptm2, tmptm3; struct tm tbtemp; long dstbias = 0; long timezone = 0; _VALIDATE_RETURN( ( tb != NULL ), EINVAL, ( ( __time64_t )( -1 ) ) ) /* * First, make sure tm_year is reasonably close to being in range. */ if ( ((tmptm1 = tb->tm_year) _MAX_YEAR64 + 1) ) goto err_mktime; /* * Adjust month value so it is in the range 0 - 11. This is because * we don't know how many days are in months 12, 13, 14, etc. */ if ( (tb->tm_mon tm_mon > 11) ) { tmptm1 += (tb->tm_mon / 12); if ( (tb->tm_mon %= 12) tm_mon += 12; tmptm1--; } /* * Make sure year count is still in range. */ if ( (tmptm1 _MAX_YEAR64 + 1) ) goto err_mktime; } /***** HERE: tmptm1 holds number of elapsed years *****/ /* * Calculate days elapsed minus one, in the given year, to the given * month. Check for leap year and adjust if necessary. */ tmptm2 = _days[tb->tm_mon]; if ( _IS_LEAP_YEAR(tmptm1) && (tb->tm_mon > 1) ) tmptm2++; /* * Calculate elapsed days since base date (midnight, 1/1/70, UTC) * * * 365 days for each elapsed year since 1970, plus one more day for * each elapsed leap year. no danger of overflow because of the range * check (above) on tmptm1. */ tmptm3 = (tmptm1 - _BASE_YEAR) * 365 + _ELAPSED_LEAP_YEARS(tmptm1); /* * elapsed days to current month (still no possible overflow) */ tmptm3 += tmptm2; /* * elapsed days to current date. */ tmptm1 = tmptm3 + (tmptm2 = (__time64_t)(tb->tm_mday)); /***** HERE: tmptm1 holds number of elapsed days *****/ /* * Calculate elapsed hours since base date */ tmptm2 = tmptm1 * 24; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_hour); /***** HERE: tmptm1 holds number of elapsed hours *****/ /* * Calculate elapsed minutes since base date */ tmptm2 = tmptm1 * 60; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_min); /***** HERE: tmptm1 holds number of elapsed minutes *****/ /* * Calculate elapsed seconds since base date */ tmptm2 = tmptm1 * 60; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_sec); /***** HERE: tmptm1 holds number of elapsed seconds *****/ if ( ultflag ) { /* * Adjust for timezone. No need to check for overflow since * localtime() will check its arg value */ __tzset(); _ERRCHECK(_get_dstbias(&dstbias;)); _ERRCHECK(_get_timezone(&timezone;)); tmptm1 += timezone; /* * Convert this second count back into a time block structure. * If localtime returns NULL, return an error. */ if ( _localtime64_s(&tbtemp;, &tmptm1;) != 0 ) goto err_mktime; /* * Now must compensate for DST. The ANSI rules are to use the * passed-in tm_isdst flag if it is non-negative. Otherwise, * compute if DST applies. Recall that tbtemp has the time without * DST compensation, but has set tm_isdst correctly. */ if ( (tb->tm_isdst > 0) || ((tb->tm_isdst 0)) ) { tmptm1 += dstbias; if ( _localtime64_s(&tbtemp;, &tmptm1;) != 0 ) goto err_mktime; } } else { if ( _gmtime64_s(&tbtemp;, &tmptm1;) != 0) goto err_mktime; } /***** HERE: tmptm1 holds number of elapsed seconds, adjusted *****/ /***** for local time if requested *****/ *tb = tbtemp; return tmptm1; err_mktime: /* * All errors come to here */ errno = EINVAL; return (__time64_t)(-1); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值