本文发表在 rolia.net/zh 相约加拿大网上社区枫下论坛
Here is a C function showing an algorithm to convert Gregorian calendar date to Julian day number Jday.
Jday(unsigned month, unsigned day, unsigned year)
* The following two autos are just for clearity
unsigned long century;
unsigned long years;
* The code for argument validation is ignored here.
if (month > 2)
month -= 3;
month += 9;
century = (unsigned long) year / 100;
years = (unsigned long) year % 100;
return ((146097UL * century) >> 2) + ((1461UL * years) >> 2)
+ (unsigned long) ((153 * month + 2) / 5 + day) + 1721119UL;
Porting this function to Shell Script we can get days between almost any two given dates. For exampple:
Jday(6, 4, 2002) - Jday(6, 4, 1989)
It is said that the algorithm is from Communications of the ACM, Volume 6, No. 8, (Aug. 1963), p. 444. I never read it.
I am not sure the exact range of the valid date. Because Gregorian calendar started on September 14, 1752, I think the range of valid date in this algorithm should be at least from September 14, 1752 to the date which is limited by the maximum value of the number used in the Shell Script.
更多精彩文章及讨论，请光临枫下论坛. 网址: rolia.net/zh