Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How Do I Get Date 7 Days Ago ?

2,323 views
Skip to first unread message

Peter Talbot

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
Can anyone tell me if there is a way on Solaris 2.6 to get the date 7
(or x number of) days ago ?

Specifically, I'd like to replicate the Linux command:

date --date '7 days ago'

but I cannot think of an easy way to do this on Solaris, nor do the man
pages offer much help.

Any suggestions welcome...thanks.

--
Peter Talbot
peter....@home.com

Timothy J. Lee

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
Peter Talbot <peter....@home.com> writes:
|Can anyone tell me if there is a way on Solaris 2.6 to get the date 7
|(or x number of) days ago ?
|
|Specifically, I'd like to replicate the Linux command:
|
|date --date '7 days ago'
|
|but I cannot think of an easy way to do this on Solaris, nor do the man
|pages offer much help.

"man date" on Linux says "GNU Shell Utilities" on the footer. So
the simplest way to get that feature would be to compile GNU date
in the GNU Shell Utilities, available at ftp://prep.ai.mit.edu/pub/gnu,
among other places.

--
------------------------------------------------------------------------
Timothy J. Lee timlee@
Unsolicited bulk or commercial email is not welcome. netcom.com
No warranty of any kind is provided with this message.

Andrew Gabriel

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
In article <36DE0BB0...@home.com>,

Peter Talbot <peter....@home.com> writes:
>Can anyone tell me if there is a way on Solaris 2.6 to get the date 7
>(or x number of) days ago ?
>
>Specifically, I'd like to replicate the Linux command:
>
>date --date '7 days ago'
>
>but I cannot think of an easy way to do this on Solaris, nor do the man
>pages offer much help.
>
>Any suggestions welcome...thanks.

7 days is 168 hours, so something like
(in sh or ksh)...

TZ=`date +%Z`168 date

(in csh)...

(setenv TZ `date +%Z`168; date)

If daylight savings changed in the last week, then this might
be an hour out and/or wrong timezone...

--
Andrew Gabriel
Consultant Software Engineer


logan...@yahoo.com

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
In article <36DE0BB0...@home.com>,

Peter Talbot <peter....@home.com> wrote:
> Can anyone tell me if there is a way on Solaris 2.6 to get the date 7
> (or x number of) days ago ?
>
> Specifically, I'd like to replicate the Linux command:
>
> date --date '7 days ago'
>
> but I cannot think of an easy way to do this on Solaris, nor do the man
> pages offer much help.

Well,

perl -e 'print scalar(localtime(time)), "\n";'

will print the current time, and

perl -e 'print scalar(localtime(time - 7 * 24 * 60 * 60)), "\n";'

will print the time seven days ago.

- Logan

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Robert B. Kasten

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
Peter Talbot <peter....@home.com> wrote:
> Can anyone tell me if there is a way on Solaris 2.6 to get the date 7
> (or x number of) days ago ?

> date --date '7 days ago'


My preference is a very simple c program I wrote that takes the date(1)
+format parameters and the number of days in the past. For your purposes
just call it like this: progname \ 7 and it will print the date a week
ago in standard date(1) format.

Here are the relevant pieces:

#include<stdio.h>
#include<time.h>
main(int argc,char **argv)
{
time_t clock,old_clock;
struct tm *tm_ptr;
int days_ago;
char buff[32];

days_ago=atoi(argv[2]);
time(&clock);
old_clock=clock-(days_ago*3600*24);
tm_ptr=localtime(&old_clock);
strftime(&buff,32,argv[1],tm_ptr);
printf("%s\n",buff);
}

Opinions expressed herein are my own and may not represent those of my employer.


David L. Cassell

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
logan...@yahoo.com wrote:

[snip for my convenience]


> perl -e 'print scalar(localtime(time - 7 * 24 * 60 * 60)), "\n";'
> will print the time seven days ago.

Logan is right.. up to epsilon. But, of course this will be off a
little a couple weeks a year. If you don't mind a 1-hour error, this is
fine. But there is a more complete Perl solution available in a subroutine
that Larry Rosler wrote and posted in commp.lang.perl.misc just a few
weeks ago. Search DejaNews for it. Hint: the discussion was on how
to get *yesterday's* time.

David
--
David L. Cassell, OAO cas...@mail.cor.epa.gov
Senior computing specialist
mathematical statistician

Mason Ip

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
Peter Talbot wrote...

>
>Can anyone tell me if there is a way on Solaris 2.6 to get the date 7
>(or x number of) days ago ?

There is a util mktime out there in the Web.

% mktime
1999-03-04 15:16:51
% mktime -d -7
1999-02-25 15:16:57

% man 1 mktime
...
DESCRIPTION
The mktime command is a command - line interface to the
mktime(3) function from Standard C, and can be used to mani-
pulate times and dates in shell scripts. It has a few
enhancements over mktime(3), and a few minor differences
which are detailed below.
...

Forget where I got it :(

Mason Ip

unread,
Mar 5, 1999, 3:00:00 AM3/5/99
to
Peter Talbot wrote...
>
>Can anyone tell me if there is a way on Solaris 2.6 to get the date 7
>(or x number of) days ago ?

http://www.interlog.com/~john/technogeek/source/mktime/mktime.tar.gz

logan...@yahoo.com

unread,
Mar 5, 1999, 3:00:00 AM3/5/99
to
In article <36DEDBCD...@mail.cor.epa.gov>,
"David L. Cassell" <cas...@mail.cor.epa.gov> wrote:

> logan...@yahoo.com wrote:
> > perl -e 'print scalar(localtime(time - 7 * 24 * 60 * 60)), "\n";'
> > will print the time seven days ago.
>
> Logan is right.. up to epsilon. But, of course this will be off a
> little a couple weeks a year. If you don't mind a 1-hour error, this is
> fine.

Well, it depends on whether you think of this as an error or not.
The perl one-liner I gave will print the time that you would see on
a clock if you travelled back in time exactly 7 days.

If your purpose is, instead, to get the number of the calendar day
that was seven days before today, that one-liner will be off for one
hour every day for I guess 14 days a year.

Personally, I would try not to get myself into a situation where I
had to do the latter and not the former, if only because the latter
violates the "keep it simple stupid" rule.

Peter Talbot

unread,
Mar 6, 1999, 3:00:00 AM3/6/99
to
> Thanks to everyone for your helpful suggestions !

Several of your responses do exactly what I require.

Cheers :)

Peter Talbot
peter....@home.com

William F. Wyatt

unread,
Mar 8, 1999, 3:00:00 AM3/8/99
to Andrew Gabriel
Andrew Gabriel wrote:
> Peter Talbot <peter....@home.com> writes:
[...]

> >Specifically, I'd like to replicate the Linux command:
> >
> >date --date '7 days ago'
[...]

> 7 days is 168 hours, so something like
> (in sh or ksh)...
>
> TZ=`date +%Z`168 date
[...]

Curiously, this doesn't work in Linux (Red Hat 5.2), and 'sort of'
works in Solaris 2.5.1, but not exactly as expected.

On both the Solaris and Linux systems, any 0-23 hour offset seems
to silently convert the output to GMT:

> date
Mon Mar 8 12:52:41 EST 1999
> (setenv TZ EST0; date)
Mon Mar 8 17:53:16 EST 1999

On the Linux system *only*, any value greater than 24 is equivalent to
GMT+23, and any value less than -23 is the same as GMT-23:

> ( setenv TZ GMT+23 ; date )
Sun Mar 7 18:54:30 GMT 1999
> ( setenv TZ GMT168; date )
Sun Mar 7 18:54:54 GMT 1999

--
Bill Wyatt (REMOVEw...@cfa0.harvard.edu) "remove this" for email
Smithsonian Astrophysical Observatory (Cambridge, MA, USA)

Alvis Harding Jr.

unread,
Mar 8, 1999, 3:00:00 AM3/8/99
to
You can grab the GNU version of date from prep.ai.mit.edu to duplicate
your Linux date syntax you mentioned.

Alvis

Andrew Gabriel

unread,
Mar 10, 1999, 3:00:00 AM3/10/99
to
In article <36E41120...@cfa.harvard.edu>,

"William F. Wyatt" <REMOVEw...@cfa.harvard.edu> writes:
>Andrew Gabriel wrote:
>> Peter Talbot <peter....@home.com> writes:
>[...]
>> >Specifically, I'd like to replicate the Linux command:
>> >
>> >date --date '7 days ago'
>[...]
>> 7 days is 168 hours, so something like
>> (in sh or ksh)...
>>
>> TZ=`date +%Z`168 date
>[...]
>
>Curiously, this doesn't work in Linux (Red Hat 5.2), and 'sort of'
>works in Solaris 2.5.1, but not exactly as expected.
>
>On both the Solaris and Linux systems, any 0-23 hour offset seems
>to silently convert the output to GMT:
>
> > date
> Mon Mar 8 12:52:41 EST 1999
> > (setenv TZ EST0; date)
> Mon Mar 8 17:53:16 EST 1999

Actually, it did occur to me just after I posted that it was
only going to work for someone who is really on GMT (I am!).
For anyone else, you will need to add the shift from GMT to
the 168, so for EST (assuming 5 hours difference from GMT),

(setenv TZ EST172; date)

Phil Meyer

unread,
Mar 12, 1999, 3:00:00 AM3/12/99
to
In article <7c6nsu$b...@cucumber.demon.co.uk>,

and...@cucumber.demon.co.uk (Andrew Gabriel) writes:
> In article <36E41120...@cfa.harvard.edu>,
> "William F. Wyatt" <REMOVEw...@cfa.harvard.edu> writes:
>>Andrew Gabriel wrote:
>>> Peter Talbot <peter....@home.com> writes:
>>[...]
>>> >Specifically, I'd like to replicate the Linux command:
>>> >
>>> >date --date '7 days ago'

There was a HUGE debate some time ago over the most efficient methods for
doing this. Some argued different perl scripts until the source to
daysback was posted. Everyone bowed to that.

Here is a copy of the complete article.

From la...@bu.edu Tue Mar 11 13:40:09 1997
Path: nntp.sprintmail.com!nntp.sprintmail.com!news.sprintlink.net!news-stk-11.sprintlink.net!news-pull.sprintlink.net!news.sprintlink.net!news-dc-9.sprintlink.net!news.maxwell.syr.edu!worldnet.att.net!cbgw2.lucent.com!news.bu.edu!valente!not-for-mail
From: la...@bu.edu (Lars Kellogg-Stedman)
Newsgroups: comp.unix.solaris,comp.unix.questions
Subject: Re: Yesterdays's date from a shell script
Followup-To: comp.unix.solaris,comp.unix.questions
Date: 11 Mar 1997 19:40:10 GMT
Organization: Boston University
Lines: 72
Distribution: inet
Message-ID: <5g4ceq$e...@news.bu.edu>
References: <5f3mrg$k...@camel2.mindspring.com> <331592...@nym.sc.philips.com> <5f4ci5$l...@crchh327.rich.bnr.ca> <5f5c4a$e...@camel4.mindspring.com> <3320AE...@kimball.e-mail.com> <5g05tm$b8g$1...@shade.twinsun.com>
NNTP-Posting-Host: valente.bu.edu
X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
Xref: nntp.sprintmail.com comp.unix.solaris:28999 comp.unix.questions:14024

Paul Eggert (egg...@twinsun.com) wrote:
: dmiller <dmi...@kimball.e-mail.com> writes:
:
: >The following looks great for situations when you are using present time.
:
: But there are lots of more-everyday examples where the solutions posted so far
: all fail, for some reasonable definition of ``fail''.

The following is the source code for a very small C program I wrote
some time ago to solve just this problem. The program is called
'daysback' (although you're welcome to call it whatever you want).

Usage is:

daysback <number of days back> [<date format>]

The <date format> string is the same as that used by the 'date' command
(i.e., the strftime() function). You can also define the environment
variable DAYSBACK if you want to change the default format string (the
one used if you don't pass a date format argument on the command line).

This is known to work under Solaris 2.5.x, and should probably work under
other versions of Unix with little or no modification.

#include <time.h>
#include <stdio.h>

#define ONEDAY (60*60*24)
#define DEFAULT_FORMAT "%d-%b-%Y"

int main(int argc,char *argv[])
{
time_t now;
char *date_format=NULL;
struct tm *now_s;
int daysback;
char stamp[80];

if (argc < 2 || argc > 3)
{
fprintf(stderr,"usage: daysback <number of days> [\"<date format>\"]\n");
exit(2);
}

daysback=atoi(argv[1]);

if (argc==3)
{
date_format=(char *)malloc(strlen(argv[2]));
strcpy(date_format,argv[2]);
}
else if (getenv("DAYSBACK"))
date_format=(char *)getenv("DAYSBACK");

if ((!date_format) || (*date_format=='\0'))
{
date_format=(char *)malloc(strlen(DEFAULT_FORMAT));
strcpy(date_format,DEFAULT_FORMAT);
}

now=time(0)-daysback*ONEDAY;
now_s=localtime(&now);

strftime(stamp,80,date_format,now_s);
puts(stamp);

return(0);
}

---
Lars Kellogg-Stedman * la...@bu.edu * (617)353-8277
Office of Information Technology, Boston University

--
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=+
| ph...@unix.sprint.net Phil Meyer UNIX Admin Extraordinaire |
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=+
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: 2.7.1

mQBNAzI9coIAAAECAOsKzNO5yCJiXX3/fLIH3x0JSU0LvNdlOrXpkJFtf/a5ahz4
H2l+qIS5RgdYRbhIy7yDRppgmLEWkhlRdwp4IXEABRG0HlBoaWwgTWV5ZXIgPHBo
aWxAd29ya3MudGkuY29tPg==
=Pdpx
-----END PGP PUBLIC KEY BLOCK-----

logan...@yahoo.com

unread,
Mar 13, 1999, 3:00:00 AM3/13/99
to
In article <7cc2m2$icc$1...@newsbeast.sj.unix.sprint.net>,

ph...@unix.sprint.net wrote:
> There was a HUGE debate some time ago over the most efficient methods for
> doing this. Some argued different perl scripts until the source to
> daysback was posted. Everyone bowed to that.
>
> Here is a copy of the complete article:
:
:

> The following is the source code for a very small C program I wrote
> some time ago to solve just this problem. The program is called
> 'daysback' (although you're welcome to call it whatever you want).
>
> Usage is:
>
> daysback <number of days back> [<date format>]
>
> The <date format> string is the same as that used by the 'date' command
> (i.e., the strftime() function). You can also define the environment
> variable DAYSBACK if you want to change the default format string (the
> one used if you don't pass a date format argument on the command line).

And just for fun, the same thing in perl looks like this:

#! /usr/local/bin/perl

use POSIX qw(strftime);

$seconds_per_day = 3600 * 24;
$default_format = defined $ENV{'DAYSBACK'} ? $ENV{'DAYSBACK'} : '%d-%b-%Y';

($days, $format) = @ARGV;
$format = $default_format unless defined $format;

print strftime ($format, localtime (time - $days * $seconds_per_day)), "\n";

Note that this won't be very efficient in terms of execution time, if
that's what is meant by the most efficient method for doing this. If,
however, you have perl on your system, this will be low-hassle (which
is another form of efficiency). It doesn't have quite the same error
checking as the C code, but it will not blow up horribly even if you
give it too many or too few arguments, so this isn't a big deal.

- Logan

0 new messages