Julian Day

从项目webalizer抓取到一个有用的关于儒略日(Julian Day)的函数。

关于"儒略日数"介绍参考

http://blog.sina.com.cn/s/blog_53027c620100mtii.html

 

/*****************************************************************/
/**/
/* JDATE - Julian date calculator */
/**/
/* Calculates the number of days since Jan 1, 0000. */
/**/
/* Originally written by Bradford L. Barrett (03/17/1988) */
/* Returns an unsigned long value representing the number of */
/* days since January 1, 0000. */
/**/
/* Note: Due to the changes made by Pope Gregory XIII in the */
/* 16th Centyry (Feb 24, 1582), dates before 1583 will */
/* not return a truely accurate number (will be at least */
/* 10 days off). Somehow, I don't think this will */
/* present much of a problem for most situations :) */
/**/
/* Usage: days = jdate(day, month, year) */
/**/
/* The number returned is adjusted by 5 to facilitate day of */
/* week calculations. The mod of the returned value gives the */
/* day of the week the date is. (ie: dow = days % 7 ) where */
/* dow will return 0=Sunday, 1=Monday, 2=Tuesday, etc... */
/**/
/*****************************************************************/

u_int64_t jdate( int day, int month, int year )
{
u_int64_t days; /* value returned */
int mtable[] = {0,31,59,90,120,151,181,212,243,273,304,334};

/* First, calculate base number including leap and Centenial year stuff */

days=(((u_int64_t)year*365)+day+mtable[month-1]+
((year+4)/4) - ((year/100)-(year/400)));

/* now adjust for leap year before March 1st */

if ((year % 4 == 0) && !((year % 100 == 0) &&
(year % 400 != 0)) && (month < 3))
--days;

/* done, return with calculated value */

return(days+5);
}


如果调用得多的话,数组mtable声明为static是否要好些呢。

 

 

 

posted @ 2012-02-25 09:58  westfly  阅读(674)  评论(0编辑  收藏  举报