* Date Calculations @ 1996-05-14 4:00 Robert Reed 0 siblings, 0 replies; 8+ messages in thread From: Robert Reed @ 1996-05-14 4:00 UTC (permalink / raw) Hello, I'd like to do date arithmetic (e.g., get the date from two months ago). I notice that "date" under Linux allows this, but I can't find a similar facility under OSF. Any suggestions would be appreciated. Digital Unix (OSF/1) v3.2, DEC Alpha 2100 Thanks, Robert Reed rreed@ist.ucf.edu ^ permalink raw reply [flat|nested] 8+ messages in thread
* Date Calculations @ 1998-10-02 4:00 lloydy1090 1998-10-09 4:00 ` Steven Winikoff 0 siblings, 1 reply; 8+ messages in thread From: lloydy1090 @ 1998-10-02 4:00 UTC (permalink / raw) 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 would be eternally grateful for any suggestions :-) TIA Lloydy -----== Posted via Deja News, The Leader in Internet Discussion ==----- http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Date Calculations 1998-10-02 4:00 lloydy1090 @ 1998-10-09 4:00 ` Steven Winikoff 1998-10-09 4:00 ` Chris Weber 1998-10-19 4:00 ` Mr Mayes 0 siblings, 2 replies; 8+ messages in thread From: Steven Winikoff @ 1998-10-09 4:00 UTC (permalink / raw) 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 ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Date Calculations 1998-10-09 4:00 ` Steven Winikoff @ 1998-10-09 4:00 ` Chris Weber 1998-10-19 4:00 ` Mr Mayes 1 sibling, 0 replies; 8+ messages in thread From: Chris Weber @ 1998-10-09 4:00 UTC (permalink / raw) In article <6vju3c$emk$1@newsflash.concordia.ca>, smw@trm.concordia.ca ( Steven Winikoff ) wrote: >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 GNU _date_ can do this via its "-d" flag, used to tell _date_ what time to pretend it is. The syntax accepted by "-d" is quite flexible. To have GNU _date_ return what you require, you could incant: $ date -d "-7days" +%d-%b-%y GNU _date_ is part of the GNU Shell Utilities package, which can be found at: ftp://ftp.gnu.org/pub/gnu/sh-utils-1.16.tar.gz -- Chris Weber <weber@NOSPAM.bucknell.edu> Bucknell Computer and Communication Services ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Date Calculations 1998-10-09 4:00 ` Steven Winikoff 1998-10-09 4:00 ` Chris Weber @ 1998-10-19 4:00 ` Mr Mayes 1998-10-29 5:00 ` Bob Vickers 1 sibling, 1 reply; 8+ messages in thread From: Mr Mayes @ 1998-10-19 4:00 UTC (permalink / raw) >>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. > It should be simple enough using the TZ environment variable. TZ=GMT168 date +"+"%d-%b-%y" ought to do the job. Substitute your favourite time zone for GMT. Cheers Rich ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Date Calculations 1998-10-19 4:00 ` Mr Mayes @ 1998-10-29 5:00 ` Bob Vickers 1998-11-06 5:00 ` Richard Mayes 0 siblings, 1 reply; 8+ messages in thread From: Bob Vickers @ 1998-10-29 5:00 UTC (permalink / raw) Mr Mayes wrote: > > >>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. > > > > It should be simple enough using the TZ environment variable. > > TZ=GMT168 date +"+"%d-%b-%y" ought to do the job. > Well it doesn't work on DU 4.0B, and you haven't really any right to expect it to work. You should either get hold of the GNU date program or do it in Perl. Bob -- ====================================================================== Bob Vickers R.Vickers@dcs.rhbnc.ac.uk Dept of Computer Science, Royal Holloway College, University of London WWW: http://www.cs.rhbnc.ac.uk/home/bobv Phone: +44 1784 443691 ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Date Calculations 1998-10-29 5:00 ` Bob Vickers @ 1998-11-06 5:00 ` Richard Mayes 1998-11-13 5:00 ` Bob Vickers 0 siblings, 1 reply; 8+ messages in thread From: Richard Mayes @ 1998-11-06 5:00 UTC (permalink / raw) Bob Vickers wrote in message <3638A5FB.2A015FD7@dcs.rhbnc.ac.uk>... >Mr Mayes wrote: >> >> >>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. >> > >> >> It should be simple enough using the TZ environment variable. >> >> TZ=GMT168 date +"+"%d-%b-%y" ought to do the job. >> > >Well it doesn't work on DU 4.0B, and you haven't really any right to >expect it to work. You should either get hold of the GNU date program or >do it in Perl. > >Bob You're spot on Bob. This does not work in DU 4. At least not beyond 48 hours. It's a shame really as this is a natty little feature that is available on all other flavours of Unix that I have been involved with. Incidentally why have I no right to expect it to work ? It seems slightly odd logic to suggest that I shouldn't expect a normal Unix facility to work whilst putting forward alternatives that are not part of a standard commercial Unix distribution. Cheers Rich ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Date Calculations 1998-11-06 5:00 ` Richard Mayes @ 1998-11-13 5:00 ` Bob Vickers 0 siblings, 0 replies; 8+ messages in thread From: Bob Vickers @ 1998-11-13 5:00 UTC (permalink / raw) Richard Mayes wrote: > > Bob Vickers wrote in message <3638A5FB.2A015FD7@dcs.rhbnc.ac.uk>... > >Mr Mayes wrote: > >> > >> >>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. > >> > > >> > >> It should be simple enough using the TZ environment variable. > >> > >> TZ=GMT168 date +"+"%d-%b-%y" ought to do the job. > >> > > > >Well it doesn't work on DU 4.0B, and you haven't really any right to > >expect it to work. You should either get hold of the GNU date program or > >do it in Perl. > > > >Bob > > You're spot on Bob. This does not work in DU 4. At least not beyond 48 > hours. It's a shame really as this is a natty little feature that is > available on all other flavours of Unix that I have been involved with. > > Incidentally why have I no right to expect it to work ? It seems slightly > odd logic to suggest that I shouldn't expect a normal Unix facility to work > whilst putting forward alternatives that are not part of a standard > commercial Unix distribution. > Rich, I think you are confusing a 'feature' with an accident of implementation. You can only describe something as a feature if it is defined in a manual somewhere. The TZ variable is defined in the tzset man page, and it explicitly states that the hour offset is between 0 and 24. It is the same on Linux and Solaris (see environ(5) man page). So the behaviour on these systems for an hour of 168 is undefined. You might get lucky and find it behaves in the way you want, but there is no guarantee it will work tomorrow, or after you upgrade or switch to another operating system. Bob -- ====================================================================== Bob Vickers R.Vickers@dcs.rhbnc.ac.uk Dept of Computer Science, Royal Holloway College, University of London WWW: http://www.cs.rhbnc.ac.uk/home/bobv Phone: +44 1784 443691 ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~1998-11-13 5:00 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 1996-05-14 4:00 Date Calculations Robert Reed -- strict thread matches above, loose matches on Subject: below -- 1998-10-02 4:00 lloydy1090 1998-10-09 4:00 ` Steven Winikoff 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
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox