From: smw@trm.concordia.ca ( Steven Winikoff )
Subject: Re: Date Calculations
Date: 1998/10/09 [thread overview]
Message-ID: <6vju3c$emk$1@newsflash.concordia.ca> (raw)
In-Reply-To: 6v2mt1$74e$1@nnrp1.dejanews.com
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 <stdio.h>
#include <sys/types.h>
#include <time.h>
/*--------------------------------------------------------------------*/
/** 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
next prev parent reply other threads:[~1998-10-09 4:00 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-10-02 4:00 Date Calculations lloydy1090
1998-10-09 4:00 ` Steven Winikoff [this message]
1998-10-09 4:00 ` Chris Weber
1998-10-19 4:00 ` Mr Mayes
1998-10-29 5:00 ` Bob Vickers
1998-11-06 5:00 ` Richard Mayes
1998-11-13 5:00 ` Bob Vickers
-- strict thread matches above, loose matches on Subject: below --
1996-05-14 4:00 Robert Reed
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox