markt@tse.uucp (Mark Turner) (06/20/91)
Hi folks! Can anyone help me with suggestions on how to convert a date to a day of the week? For example, I start with a date like '19910619'; I'd like some function to return 'Wed' or '4' or some such indication of the day. This must be doable! One caveat: I'd rather not use a shell script (for instance, I could do this using cal and awk) - C would be best. Please don't suggest that I should reset the system date and then pass a "tm" structure to the one of the ctime-related functions. I'd like to keep the machine up and running throughout the entire calculation!! :-) Please mail and I'll post summaries if there is any interest (and the answer *isn't* too embarassingly obvious). -------------------------------------------------------------------------------- Mark Turner (markt@tse.UUCP), The Toronto Stock Exchange
darcy@druid.uucp (D'Arcy J.M. Cain) (06/20/91)
In article <1991Jun19.170753.593@tse.uucp> Mark Turner writes: >Hi folks! Can anyone help me with suggestions on how to convert a date to a >day of the week? ... >Please don't suggest that I should reset the system date and then pass a "tm" >structure to the one of the ctime-related functions. I'd like to keep the No need to muck with the system date. Just create a tm structure and fill it in with the known values. now call mktime(3) and you should have the day of the week in the tm_wday field. If you don't have mktime then send me mail and I will send you a version. It's a flawed version but the flaw is well defined and can be worked around. It calls localtime which is not strictly allowed under ANSI. BTW, anyone know where I can get a PD or freeware localtime so I can fix and post my mktime? -- D'Arcy J.M. Cain (darcy@druid) | D'Arcy Cain Consulting | There's no government Toronto, Ontario, Canada | like no government! +1 416 424 2871 |
tr@samadams.princeton.edu (Tom Reingold) (06/22/91)
This will work for this century. I'll leave fixing it for any century as an exercise for the reader. #include <stdio.h> #include <sys/types.h> #include <time.h> static int monlens[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; static char *daytab[] = { "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri" }; static int keys[12]; void initkeys(); main(argc, argv) int argc; char *argv[]; { int day; time_t t; struct tm *tp; if (argc != 3 && argc != 4) { yuck: fprintf(stderr, "usage: %s month date [year]\n", argv[0]); exit(1); } t = time(&t); tp = localtime(&t); tp->tm_mon = atoi(argv[1]) - 1; tp->tm_mday = atoi(argv[2]); if (argc == 4) tp->tm_year = atoi(argv[3]); initkeys(tp->tm_year); if (tp->tm_mon == -1 || tp->tm_mday == 0 || tp->tm_year == 0) goto yuck; if (tp->tm_mday > monlens[tp->tm_mon]) { fprintf(stderr, "%s: invalid date\n", argv[0]); exit(2); } day = dateof(tp); printf("%02d/%02d/%02d is a %s\n", tp->tm_mon + 1, tp->tm_mday, tp->tm_year, daytab[day]); exit(0); } dateof(tp) /* return day of week: Saturday=0, ..., Friday=6 */ struct tm *tp; { int crap; crap = tp->tm_year + tp->tm_mday + keys[tp->tm_mon] + ((int) tp->tm_year / 4); return(crap % 7); } isleap(year) int year; { if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) return(1); return(0); } void initkeys(n) int n; { int i; /* This generates a table. I could hard code it, but I wanted to show what the table means. */ if (isleap(n)) keys[0] = 0, monlens[1] = 29; else keys[0] = 1; for (i = 1; i < 12; i++) keys[i] = (monlens[i-1] + keys[i-1]) % 7; } -- Tom Reingold tr@samadams.princeton.edu OR ...!princeton!samadams!tr "Warning: Do not drive with Auto-Shade in place. Remove from windshield before starting ignition."
newberry@nmsu.edu (Jeff Newberry) (06/25/91)
There is an article in "Computer Language" magazine in the July 1989 issue called "The Dating Game." It has an algorithm for finding the day of week and a host of other date functions. Jeff Newberry Computing Research Lab New Mexico State University