Sujet : Re: "A diagram of C23 basic types"
De : rjh (at) *nospam* cpax.org.uk (Richard Heathfield)
Groupes : comp.lang.cDate : 16. Apr 2025, 23:13:58
Autres entêtes
Organisation : Fix this later
Message-ID : <vtpa38$369rc$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14
User-Agent : Mozilla Thunderbird
On 16/04/2025 22:10, BGB wrote:
<snip>
One shorthand is to assume a year is 365.25 days (31557600 seconds), and then base everything else off this (initially ignoring things like leap-years, etc, just assume that the number of days per year is fractional).
Then, say, 2629800 seconds per month, ...
For some other calculations, one can assume an integer number of days (365), just that each day is 0.07% longer.
For date/time calculations, one could then "guess" the date, and jitter it back/forth as needed until it was consistent with the calendar math.
Estimate and subtract the year, estimate and subtract the month, then the day. Then if we have landed on the wrong day, adjust until it fits.
Not really sure if there was a more standard way to do this.
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
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.
2) In the real world there was no year 0. Theoretically JD1 is 1 Jan 4713BC, but it's out by a year for year 0, and another 11 days for Sept 1752.
If you draw a line after the inception of the Calendar (New Style), it's fine. If you don't, it should be easy enough to wrap.
I have used these functions for over 25 years now and have always found them to be very reliable.
-- Richard HeathfieldEmail: rjh at cpax dot org dot uk"Usenet is a strange place" - dmr 29 July 1999Sig line 4 vacant - apply within