OT: Zjisteni licheho/sudeho dne
Karel Zak
zakkr na zf.jcu.cz
Středa Červenec 26 11:25:32 CEST 2000
On Wed, 26 Jul 2000, Michal Krause wrote:
> Zdravim.
>
> Omlouvam se za OT, ale nevite nekdo, jakym zpusobem spolehlive zjistit
> zda je den lichy nebo sudy (je jedno, jestli treba od roku 1970 nebo od
> jineho datumu).
To, ale musis rict co je to sudy/lichy den :-) Tedy mysleno od ceho to
pocitas - od zacatku roku, mesice, pocatku vesmiru apod...
To jak ti tu nekdo radil s julianskym kalendarem je opravdu asi
nejlepsi. Tady to mas v C obousmernou konverzi:
/*
* Calendar time to Julian date conversions.
* Julian date is commonly used in astronomical applications,
* since it is numerically accurate and computationally simple.
* The algorithms here will accurately convert between Julian day
* and calendar date for all non-negative Julian days
* (i.e. from Nov 23, -4713 on).
*
* Ref: Explanatory Supplement to the Astronomical Almanac, 1992.
* University Science Books, 20 Edgehill Rd. Mill Valley CA 94941.
*
* Use the algorithm by Henry Fliegel, a former NASA/JPL colleague
* now at Aerospace Corp. (hi, Henry!)
*
* These routines will be used by other date/time packages - tgl 97/02/25
*/
int
date2j(int y, int m, int d)
{
int m12 = (m - 14) / 12;
return ((1461 * (y + 4800 + m12)) / 4 + (367 * (m - 2 - 12 * (m12))) / 12
- (3 * ((y + 4900 + m12) / 100)) / 4 + d - 32075);
} /* date2j() */
void
j2date(int jd, int *year, int *month, int *day)
{
int j,
y,
m,
d;
int i,
l,
n;
l = jd + 68569;
n = (4 * l) / 146097;
l -= (146097 * n + 3) / 4;
i = (4000 * (l + 1)) / 1461001;
l += 31 - (1461 * i) / 4;
j = (80 * l) / 2447;
d = l - (2447 * j) / 80;
l = j / 11;
m = (j + 2) - (12 * l);
y = 100 * (n - 49) + i + l;
*year = y;
*month = m;
*day = d;
return;
} /* j2date() */
---
Snad se na tebe NASA & PostgreSQL nebude zlobit :-)
---
Pouziti:
~~~~~~~
int j = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
if ((((unsigned) j) | 1) == j)
printf("Jsem lichej!")
else
printf("Jsem sudej!");
... welcome to date/time alchemy ...
Karel
Další informace o konferenci Linux