* On 2002.03.27, in <[EMAIL PROTECTED]>, * "Sven Guckes" <[EMAIL PROTECTED]> wrote: > > apropos: > anyone have a utility to calculate the > number of days between two given dates? > i mean - easily? no perl script > with dozens of modules, please!
shell$ ./timediff "Wed Mar 27 17:33:00 2002" "Wed Mar 22 11:02:19 2002" Difference is 5.27 days. shell$ env TIMEFMT="%d %b %Y" ./timediff "19 Feb 2001" "21 Apr 2002" Difference is 426.00 days. shell$ env TIMEFMT="%D" ./timediff "1/1/70" "12/31/79" Difference is 3651.00 days. -- -D. [EMAIL PROTECTED] NSIT University of Chicago
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include <sysexits.h> #define DEFAULT_FORMAT "%c" char *A0; main(int argc, char *argv[]) { time_t t1, t2; struct tm tm1, tm2; char *p, *fmt = DEFAULT_FORMAT; if (A0 = strchr(argv[0], '/')) ++A0; else A0 = argv[0]; if (argc != 3 || (argc > 1 && !strcmp(argv[1], "-h"))) { fprintf(stderr, "usage: %s \"time1\" \"time2\"\n", A0); exit(EX_USAGE); } if (p = getenv("TIMEFMT")) fmt = p; if (strptime(argv[1], fmt, &tm1) == NULL || strptime(argv[2], fmt, &tm2) == NULL) { fprintf(stderr, "%s: bad date for format \"%s\"\n", A0, fmt); exit(EX_DATAERR); } t1 = mktime(&tm1); t2 = mktime(&tm2); printf("Difference is %-.2f days.\n", (double)abs(t2-t1)/(60*60*24)); exit(EX_OK); }