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
"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.
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
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
> 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.
[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
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 :(
http://www.interlog.com/~john/technogeek/source/mktime/mktime.tar.gz
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.
Several of your responses do exactly what I require.
Cheers :)
Peter Talbot
peter....@home.com
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
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)
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-----
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