This is the mail archive of the mailing list for the glibc project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: advise on future of glibc (mktime issue)

Jakub Jelinek <> wrote:
>>  I need to know the weekday for millions of dates, I guess I could use
>> a hash (I would need to find a library for that) for it, but everything
>> was fine until I started using RHEL4 with the patched glibc. Any
>> suggestions ?
> I might be missing something obvious, but if all you need is the weekday
> for dates stored in the year*10000+month*100+day format and you need
> to do it that many times, then surely writing your own function
> will be uncomparably faster than doing expensive mktime and localtime.
> I haven't put any effort into optimizing this and you can get away
> even without mktime calls or just one, not just caching tm_wday
> of Jan, 1st for each year.

Perhaps, you have the Zeller's formula in mind?

int real_get_dayofweek(int date)
 // extract day/yr/mn
 int d = date % 100;
 int m = (date % 10000) / 100;
 int y = date / 10000;

 // Zeller's formula to compute Gregorian day-of-week
 if (m < 3) { m = m + 12 ; y = y - 1;}
 return ((2 + d + (13*m-2)/5 + y + y/4 - y/100 + y/400) % 7); // Sun=0, Sat=6

Sorry for a little OT.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]