From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,37d7a16bb2db2c0b X-Google-Attributes: gidfee84,public From: smw@trm.concordia.ca ( Steven Winikoff ) Subject: Re: Date Calculations Date: 1998/10/09 Message-ID: <6vju3c$emk$1@newsflash.concordia.ca>#1/1 X-Deja-AN: 399222103 References: <6v2mt1$74e$1@nnrp1.dejanews.com> Organization: Concordia University, Montreal, Canada NNTP-Posting-User: root Newsgroups: comp.unix.osf.osf1 In <6v2mt1$74e$1@nnrp1.dejanews.com> lloydy1090@my-dejanews.com writes: >I want to be able to set a variable to the current date minus 7. I'm using >date +"%d-%b-%y"to return the current date and I want to be able to return >the same format for 1 week ago. I don't have *exactly* what you're looking for, but I have two suggestions that come fairly close. First of all, there's a program called "calcdate" in Volume 3, Issue 22 of comp.sources.misc. AltaVista reports a copy at http://ftp.doc.ic.ac.uk/packages/unix-c/calendars/calcdate.c.gz I also have a program I wrote myself to do a subset of what calcdate does. In case anyone might find it useful, I'll append the C source to this message. To use it to return the date of one week ago today, type % dateoffset -7 When executed on October 8th, 1998, the result will be 1998/10/01 It wouldn't take much work to modify this to produce the same format as `date +"%d-%b-%y"`. - Steven 8<---------------------------- cut here -------------------------->8 /* * * Steven Winikoff (smw@alcor.concordia.ca) * 1997/04/25 * * dateoffset -- compute today's date plus an offset into the future * * * usage: dateoffset [number_of_days] * * if a number of days is specified, we'll print the date * corresponding to that many days into the future from now * (or into the past if the number is negative); if no number * is supplied, we'll default to printing tomorrow's date; * the output format is "yyyy/mm/dd", eg "1997/04/25" * * DISCLAIMER: This program is made freely available, but with * NO WARRANTY of any kind, express or implied. Use * at your own risk. I believe that it works (and yes, * I use it regularly myself :-), but if it doesn't work * for you, and in failing causes you to lose your job, * your spouse, your children and your entire life * savings, neither I nor my employer will be accept * any responsibility (although I will sympathize :-). */ /*--------------------------------------------------------------------*/ /** includes: **/ #include #include #include /*--------------------------------------------------------------------*/ /** defines: **/ #define USAGE "usage: dateoffset [number_of_days]\n" #define DEFAULT_OFFSET 1 /* how many days, if not user-specified? */ #define SECONDS_PER_DAY (60 * 60 * 24) #define BASE_YEAR 1900 /* see localtime(3) */ /*--------------------------------------------------------------------*/ /*** entry here: ***/ main(int argc, char *argv[]) { int number_of_days; struct tm *now; time_t time_in_seconds; /** check calling syntax: **/ if (argc == 2) number_of_days = atoi(argv[1]); else if (argc == 1) number_of_days = DEFAULT_OFFSET; else { (void) fprintf(stderr, USAGE); exit(1); } /** get the current date: **/ (void) time(&time_in_seconds); /** add the appropriate offset: **/ time_in_seconds += (number_of_days * SECONDS_PER_DAY); /** convert to local time and print the appropriate fields: **/ now = localtime(&time_in_seconds); (void)printf("%4.4d/%2.2d/%2.2d\n", (BASE_YEAR + now->tm_year), now->tm_mon+1, now->tm_mday); /** make lint happy: **/ return(0); } /* end of main() */ 8<---------------------------- cut here -------------------------->8 ________________________________________________________________________ Steven Winikoff | "The reasonable man adapts himself to Concordia University | the world; the unreasonable one Montreal, QC, Canada | persists in trying to adapt the world smw@alcor.concordia.ca | to himself. Therefore all progress http://alcor.concordia.ca/~smw | depends on the unreasonable man." | - George Bernard Shaw