www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/01/09/02:00:11

Date: Tue, 9 Jan 2001 08:55:20 +0200 (EET)
From: Esa A E Peuha <peuha AT cc DOT helsinki DOT fi>
Sender: <peuha AT sirppi DOT helsinki DOT fi>
To: <djgpp AT delorie DOT com>
Subject: Re: strftime: Need Help with Time Offsets
In-Reply-To: <t21e5t4m6auc7k7vqlj78cv0c8qg1gngfh@4ax.com>
Message-ID: <Pine.OSF.4.30.0101090827480.1867-100000@sirppi.helsinki.fi>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On Sat, 6 Jan 2001, Jason Green wrote:

> Esa A E Peuha <peuha AT cc DOT helsinki DOT fi> wrote:

> > is_leap_year(year)
> > {
> >   return (year % 4 == 0) - (year % 100 == 0) + (year % 400 == 100);
> > }
>
> This is buggy (try it for year 2000),

For year 2000, the tm_year field would be 100, and the return value
would be 1 - 1 + 1 == 1, as it should.  However, for year 1600 the
argument would be -300, but -300 % 400 isn't necessarily 100 even under
C89, and C99 actually requires it to be -300 AFAIK.  So this is indeed
buggy.

> > The other is not (this could obviously be optimized, but this way it's
> > hopefully rather self-explanatory):
>
> Erm, sorry, this is not obvious to me.  Although that's probably more
> to do with damage suffered staring at date maths code. ;-)

No wonder. :-)  It's all pretty simple once you really understand it,
but it can be mind-boggling before that.  (Just be glad there's no need
to write code to compute the date of easter for a given year. ;-)

> I put your code into functions.  Unfortunately, it sometimes gets the
> week number wrong where week 52/53 overlaps into the new year.

So it does.  That's because I made a silly mistake (see below).

>       /* day belongs to last week of previous year */
>       return number_of_week (t->tm_wday,
>                              t->tm_yday +
>                              ((isleap(t->tm_year + TM_YEAR_BASE)) ?
>                               366:365));

This, of course, needs to add the number of days in the previous year,
so it should test for isleap(t->tm_year + TM_YEAR_BASE - 1). BTW, why
is there an extra set of parentheses around isleap(...)?

-- 
Esa Peuha
student of mathematics at the University of Helsinki
http://www.helsinki.fi/~peuha/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019