tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Unification of common date/time macros



Hello,
 
I need to use common macros (defines) for a year of Epoch (1970), seconds per 
day, seconds per year etc.

For user-space there is already /usr/include/tzfile.h, that contains i.a.:
#define SECSPERMIN 60
#define MINSPERHOUR 60
#define HOURSPERDAY 24
#define DAYSPERWEEK 7
#define DAYSPERNYEAR 365
#define DAYSPERLYEAR 366
#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
#define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
#define MONSPERYEAR 12
[...]
#define TM_YEAR_BASE 1900

#define EPOCH_YEAR 1970
#define EPOCH_WDAY TM_THURSDAY
[...]
#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)

For kernel-space these defines are spread across different files, drivers, 
architectures.
Examples:
- arch/hp300/stand/common/clock.c:
#define FEBRUARY 2
#define STARTOFTIME 1970
#define SECDAY (60L * 60L * 24L)
#define SECYR (SECDAY * 365)
[...]
#define leapyear(year) ((year) % 4 == 0)
[...]
#define days_in_year(a) (leapyear(a) ? 366 : 365)
#define days_in_month(a) (month_days[(a) - 1])
[...]

- arch/mvmeppc/stand/libsa/clock.c:
#define SECDAY (24 * 60 * 60)
#define SECYR (SECDAY * 365)
#define LEAPYEAR(y) (((y) & 3) == 0)
#define YEAR0 68
[...]

- arch/amiga/dev/rtc.h
#define STARTOFTIME 1970

- arch/atari/dev/clockreg.h
#define is_leap(x) (!(x % 4) && ((x % 100) || !(x % 1000)))
[...]
#define SECS_DAY 86400L
#define SECS_HOUR 3600L
[...]
#define BSDSTARTOFTIME 1970

- arch/luna68k/dev/timekeeper.c
#define YEAR0 1970 /* year offset */

When these numbers seem to be used to be hard-coded into algorithms, like:
- arch/vax/vax/clock.c
int
numtoyear(int num)
{
int y = 1970, j;
while(num >= (j = SECPERYEAR(y))) {
y++;
num -= j;
}
return y;
}

Well, why am I raising this topic? My code will need a set of similar macros 
mentioned above and I don't want to copy-paste the same piece of code, but 
reuse something central and well-tested. Why well tested? It's worth to see 
that these definitions may differ, like missing (U)L-ong modifiers for large 
numbers (it may overflow 16-bit integers, a standard minimum for int).

For Tru64 there was a central file /usr/sys/include/sys/machine/clock.h with 
relevant defines.

I'm a volunteer for a patch, but please discuss it. Where it ought to be 
placed? File per architecture or uniform for all of them?


Home | Main Index | Thread Index | Old Index