Sujet : Re: "A diagram of C23 basic types"
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 17. Apr 2025, 00:31:08
Autres entêtes
Organisation : None to speak of
Message-ID : <87v7r3znsz.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
User-Agent : Gnus/5.13 (Gnus v5.13)
Richard Heathfield <
rjh@cpax.org.uk> writes:
[...]
Half a lifetime ago I found an algorithm on the Web and turned it into
these two functions:
>
long tojul(int yp,int mp,int dp) {long
a=(14-mp)/12,y=yp+4800-a,m=mp+12*a-3,jdn=dp+(153*m+2)/5+365*y+y/4-y/100+y/400-32045;
return jdn;}
void fromjul(int *yp,int *mp,int *dp,long jdn){long
y=4716,j=1401,m=2,n=12,r=4,p=1461,v=3,u=5,s=153,w=2,b=274277,c=-38,f=jdn+j+(((4*jdn+b)/146097)*3)/4+c,e=r*f+v,g=(e%p)/r,h=u*g+w;*dp=(h%s)/u+1;*mp=((h/s+m)%n)+1;*yp=e/p-y+(n+m-*mp)/n;}
>
long jd = tojul(2025, 4, 16); /* gives 2460782 */
>
fromjul(&y, &m, &d, 2460782); /* gives 2025, 4, 16) */
>
Day of week: take the Julian date % 7, then 0 is Monday, 1 is Tuesday
and so on:
>
$ expr `./juldate 16/4/2025` % 7
2
Be careful with terminology.
What you're computing is the "Julian day number", where day 0
is Monday, January 1, 4713 BCE in the proleptic Julian calendar,
or November 24, 4714 in the proleptic Gregorian calendar.
The term "Julian date" is ambigious. It can refer to the date
in the Julian calendar (leap year every 4 years, no exceptions)
or to the day of the Gregorian year (less ambiguously called the
ordinal date). There are a *lot* of variations.
https://en.wikipedia.org/wiki/Julian_day(It's widely believed that the "Julian" in "Julian day" and the
"Julian" in "Julian calendar" are unrelated, but it appears that
the latter was named after the former.)
It's not perfect, but its flaws have never affected me, so I've never
had to find the time to fix them. Here are some problems:
>
1) No account is taken of the 11-day shift in September 1752. If you
care, for 2/9/1752 and prior you should add 11 days to the Julian date
before using it in calculations.
The transition from the Julian to the Gregorian calendar took place at
different times in different places, from 1582 in Catholic Europe, to
1752 in the British Empire, to 1918 in Russia, to 1923 in Greece.
[...]
I have a few personal tools that deal with a Unix date number, where
1970-01-01 is 0 and today is 20194. Like the Julian day number, it's
useful for computing the number of days between dates. Unlike the
Julian day number, it increments at midnight rather than noon (the
latter is useful for astronomers).
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */