Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!mit-eddie!uw-beaver!tektronix!psueea!psueea.uucp!kirkenda From: kirkenda@psueea.uucp (Steve Kirkendall) Newsgroups: comp.sys.ibm.pc Subject: Re: calendar code Message-ID: <1428@psueea.UUCP> Date: 30 Jun 89 17:18:31 GMT References: <82362@ti-csl.csc.ti.com> Sender: news@psueea.UUCP Reply-To: kirkenda@jove.cs.pdx.edu (Steve Kirkendall) Organization: Dept. of Computer Science, Portland State University; Portland OR Lines: 243 Sorry for posting this, But I had trouble sending email. Although I suppose this is probably of general interest anyway... In article <82362@ti-csl.csc.ti.com> Donahue@TILDE (Them bats is smart; they use radar!) writes: > >I'm looking for a Turbo C function that, when given >an arbitrary date, will return the day of the week that the date >falls on. > >E.G. given 11 6 1991 [nov 6, 1991], the function > returns WEDNESDAY (or 3, if SUNDAY = 0). This is slight overkill, but the rest of this message is a shar archive which contains an implementation of the UNIX SysV localtime() function, and another function called cvtdate() which converts and ASCII date string to UNIX format. Cvtdate() is called with two arguments: the date in "YYMMDD" format, and the time in "hhmm" format. It returns a long int value which is the number of seconds since time began (i.e. Jan 1, 1970). Localtime() is called with a single argument: a pointer to a long int variable which contains a time value. It returns a pointer to a structure which breaks the time down into year, month, day of year, day of month, day of week, hours minutes, and seconds. This structure type is called "struct tm" and is declared in time.h (included). An example: #include "time.h" extern long cvtdate(); main() { long when; struct tm *tptr; when = cvtdate("911106", "0000"); tptr = localtime(&when); printf("Day of week = %d\n", tptr->tm_wday); } If all you really care about is day-of-week, you could modify cvtdate to count days instead of seconds, and take (days % 7) to get the day of the week. # This is a shell archive. # Remove everything above and including the cut line. # Then run the rest of the file through sh. -----cut here-----cut here-----cut here-----cut here----- #!/bin/sh # shar: Shell Archiver # Run the following text with /bin/sh to create: # cvtdate.c # localtime.c # time.h # This archive created: Thu Jun 29 14:54:18 1989 cat << \SHAR_EOF > cvtdate.c /* cvtdate.c */ /* This file contains the cvtdate() function, which converts a date string * into a UNIX-style time value. */ /* Offset into the year for 1st of each month (disregarding leap years) */ static int monthoff[12]={ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; static int leapoff[12] ={ 0, 31, 60, 91, 121, 152, 182, 212, 244, 274, 305, 335 }; #define MIN 60L #define HOUR (60L*MIN) #define DAY (24L*HOUR) #define YEAR (365L*DAY) /* adjust for leapyears later */ static int cvt2(digits) char *digits; { return digits[0] * 10 + digits[1] - ('0' * 11); } long cvtdate(date, time) char *date; char *time; { long now; int t; /* seconds for each year since 1970 */ t = cvt2(date); if (t < 70) t += 100; now = (t - 70) * YEAR; /* adjust for intervening leapyears */ now += ((t - 69) >> 2) * DAY; #ifdef TEST printf("%ld + %ld = %ld\n", (t-70)*YEAR, now-(t-70)*YEAR, now); #endif /* add in the month - adjust if this year is leapyear */ now += ((t&3) ? monthoff : leapoff)[cvt2(date + 2) - 1] * DAY; /* add in the day, hour, and minute */ now += (cvt2(date + 4) - 1) * DAY + cvt2(time) * HOUR + cvt2(time + 2) * MIN; return now; } #ifdef TEST extern char *ctime(); main(argc, argv) int argc; char **argv; { long now; if (argc != 3) { printf("usage: %s yymmdd hhmm\n", argv[0]); exit(2); } now = cvtdate(argv[1], argv[2]); printf("now = %ld = \"%s\";\n", now, ctime(&now)); } #endif SHAR_EOF cat << \SHAR_EOF > localtime.c /* localtime.c */ /* This file contains the source to a SysV-compatiple localtime() function * Written by Steve Kirkendall, placed in public domain April,1989 */ #include /* Offset into the year for 1st of each month (disregarding leap years) */ int monthoff[12]={ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; int leapoff[12] ={ 0, 31, 60, 91, 121, 152, 182, 212, 244, 274, 305, 335 }; #define MIN 60L #define HOUR (60L*MIN) #define DAY (24L*HOUR) #define YEAR (365L*DAY) /* adjust for leapyears later */ struct tm *localtime(clock) long *clock; /* the clock value to convert */ { static struct tm t; /* the returned structure is STATIC!!! */ long timeleft; /* amount of time not consumed in calc so far */ int i; /* find the current year, allowing for preceding leapyears */ i = *clock / YEAR; /* year - accurate formula 'til ~doomsday */ t.tm_year = i + 1970; /* add '1970' year offset */ timeleft= *clock-i*YEAR;/* beginning of year, ignoring leapyears */ i = (i + 1) / 4; /* leap days, not including this year's */ timeleft -= i * DAY; /* use up all those leap days */ /* calculate day of the year */ t.tm_yday = timeleft / DAY; /* calculate month and day of month. This might be a leap year */ if (t.tm_year % 4 == 0) { /* use leapoff[] to find month */ for (i = 11; leapoff[i] > t.tm_yday; i--) { } t.tm_mday = t.tm_yday - leapoff[i] + 1; } else { /* use regular monthoff[] table */ for (i = 11; monthoff[i] > t.tm_yday; i--) { } t.tm_mday = t.tm_yday - monthoff[i] + 1; } t.tm_mon = i; timeleft -= t.tm_yday * DAY; /* calculate weekday. */ t.tm_wday = (*clock / DAY + 4) % 7; /* calculate hour, minute, second */ t.tm_hour = timeleft / HOUR; timeleft -= HOUR * t.tm_hour; t.tm_min = timeleft / MIN; timeleft -= MIN * t.tm_min; t.tm_sec = timeleft; /* calculate DST flag */ t.tm_isdst = 0; return &t; } #ifdef TEST main(argc, argv) int argc; char **argv; { long shot; /* will this time be converted right? */ struct tm *t; /* the converted time */ time(&shot); if (argc > 1) { shot += DAY * atoi(argv[1]); } t = localtime(&shot); printf("ctime() = \"%s\"\n", ctime(&shot)); printf("tm_sec=%d\ntm_min=%d\ntm_hour=%d\ntm_mday=%d\ntm_mon=%d\n", t->tm_sec, t->tm_min, t->tm_hour, t->tm_mday, t->tm_mon); printf("tm_year=%d\ntm_wday=%d\ntm_yday=%d\ntm_isdst=%d\n", t->tm_year, t->tm_wday, t->tm_yday, t->tm_isdst); } #endif SHAR_EOF cat << \SHAR_EOF > time.h struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; }; extern struct tm *localtime(); SHAR_EOF # End of shell archive exit 0 -- Steve Kirkendall ...uunet!tektronix!psu-cs!kirkenda