Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #96068

Re: What about my last date functions? there are some error?

From Richard Heathfield <rjh@cpax.org.uk>
Newsgroups comp.lang.c
Subject Re: What about my last date functions? there are some error?
Date 2016-11-08 11:35 +0000
Organization Fix this later
Message-ID <nvsd9k$nd4$1@dont-email.me> (permalink)
References <gh932ctlqkebuct208fqbbc7rdak1b2c1o@4ax.com> <87d1i69r5w.fsf@bsb.me.uk>

Show all headers | View raw


On 08/11/16 10:55, Ben Bacarisse wrote:
> Rosario19 <Ros@invalid.invalid> writes:
>
>> What about my last date functions? there are some error?
>> just today i find someones...
>
> Great, a C question!

Aye. (Read on!)

<snip>
>>
>>     Ndays(y,m,g,ya,ma,ga)
>
> I'm surprised you don't have a date structure, at least as an
> alternative.

And that's one of the reasons I read comp.lang.c.

A few years ago, I stole a Julian date algorithm from the Web, and it 
never, ever occurred to me to use a struct to package up the day, month, 
and year parameters.

<snip>

>>     #define R return
>>     #define L(i) for(;i-->0;)
>>     #define I if
>>     #define N int
>>     #define C unsigned char
>>     #define F for
>>     #define G goto
>>     #define P printf
>>     #define E else
>
> Ah.  I'm not surprised there are bugs, but I'm not going to wade through
> this to try to fund any.

Will you fund my bugs instead?

But yes, of course you're right - such code renders itself useless 
because it's unreadable.

Even if there were /no/ bugs, it would still be useless *as code*, 
because it's unreadable. (That doesn't mean it couldn't be useful as a 
black box routine, right up until the point where a bug was found, after 
which it would revert to being useless again.)

My own Julian date routines (at least, the ones for converting to and 
from Julian date) are completely unreadable, because I stole the 
algorithm from the Web rather than devising it myself. I spent about an 
hour figuring out how it worked, neglected to write it down on that 
occasion, and have never since summoned up the energy to repeat the 
analysis.

It was so unreadable that I tried to make it worse! I wrote a 
'compressed' version of it (with minimal line count and minimal spaces) 
just for fun.

  The 'date of Easter' source is pretty hairy, too.

As source code, the library (at least the bits of it that I purloined) 
is quite useless. But until it breaks, I'll keep on using it as a black 
box. And it hasn't broken yet.

I suppose, after telling you all that, I have to show you. Here are the 
minimalist versions of tojul() and fromjul():

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;}

To calculate the day of the week from a Julian date, just % 7. Monday is 
0, Tuesday is 1, etc.

Once you have a Julian date, some date tasks become trivial:

difference in days = onejdate - anotherjdate
six weeks after a given date = jdate + 42

etc.

With a little fiddling, you can do things like "third Thursday of the 
month" or, more generally, nth Xday of the month; or a function 
is_bank_holiday() - which is fairly easy in the UK because we have 
fairly predictable bank holidays, and even easier in the USA because you 
don't have bank holidays at all!

But I never thought of a struct. So thanks for that.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

What about my last date functions? there are some error? Rosario19 <Ros@invalid.invalid> - 2016-11-08 11:28 +0100
  Re: What about my last date functions? there are some error? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-08 10:55 +0000
    Re: What about my last date functions? there are some error? "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-11-08 19:00 +0800
    Re: What about my last date functions? there are some error? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-08 11:04 +0000
      Re: What about my last date functions? there are some error? "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-11-08 19:22 +0800
    Re: What about my last date functions? there are some error? Richard Heathfield <rjh@cpax.org.uk> - 2016-11-08 11:35 +0000
      Re: What about my last date functions? there are some error? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-08 15:46 +0000
        Re: What about my last date functions? there are some error? Richard Heathfield <rjh@cpax.org.uk> - 2016-11-08 16:20 +0000
          Re: What about my last date functions? there are some error? Rosario19 <Ros@invalid.invalid> - 2016-11-08 17:44 +0100
            Re: What about my last date functions? there are some error? Richard Heathfield <rjh@cpax.org.uk> - 2016-11-08 16:51 +0000
              Re: What about my last date functions? there are some error? Jerry Stuckle <jstucklex@attglobal.net> - 2016-11-08 13:21 -0500
          Re: What about my last date functions? there are some error? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-08 18:06 +0000
            Re: What about my last date functions? there are some error? Richard Heathfield <rjh@cpax.org.uk> - 2016-11-08 18:56 +0000
              Re: What about my last date functions? there are some error? Keith Thompson <kst-u@mib.org> - 2016-11-08 12:05 -0800
              Re: What about my last date functions? there are some error? David Brown <david.brown@hesbynett.no> - 2016-11-08 21:10 +0100
                Re: What about my last date functions? there are some error? Richard Heathfield <rjh@cpax.org.uk> - 2016-11-08 20:16 +0000
          Re: What about my last date functions? there are some error? jadill33@gmail.com - 2016-11-08 11:16 -0800
            Re: What about my last date functions? there are some error? Richard Heathfield <rjh@cpax.org.uk> - 2016-11-08 20:08 +0000
            Re: What about my last date functions? there are some error? Keith Thompson <kst-u@mib.org> - 2016-11-08 12:11 -0800
              Re: What about my last date functions? there are some error? Keith Thompson <kst-u@mib.org> - 2016-11-08 12:29 -0800
      Re: What about my last date functions? there are some error? Rosario19 <Ros@invalid.invalid> - 2016-11-08 17:41 +0100
        Re: What about my last date functions? there are some error? asetofsymbols@gmail.com - 2016-11-08 11:26 -0800
          Re: What about my last date functions? there are some error? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-11-08 19:38 +0000
  Re: What about my last date functions? there are some error? Keith Thompson <kst-u@mib.org> - 2016-11-08 08:19 -0800

csiph-web