I know in Bourne Shell, the command `date + '%yymmdd'`
would get me something like 970402, but I want the date
of the day before in the same format.. So I should be
able to get 970401 on April 2, 97. Can I pase any argments
to the date program and get this result? The UNIX books I
have are not of much help.
Murali
Kurt> $ TZ=24 date ...
Kurt> Works for me.
That's not a reliable technique, since there's no good reason why the
timezone code should handle offsets outside the range +/- 14 hours or so.
I've seen systems that silently ignore the value of TZ if the offset is
too large.
--
Andrew.
comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
Try this one for starters:
---- BEGIN C CODE -------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
extern char *optarg;
extern int optind, opterr, optopt;
int main( argc, argv )
int argc;
char **argv;
{
int c, fshort = 0;
int day, mon, year, adj;
struct tm t, *tp;
char dum[3];
time_t ti;
optind = 1;
while (( c = getopt( argc,argv, "s" )) != EOF ) {
switch (c) {
case 's': fshort = 1; break;
case 'l': fshort = 0; break;
case '?': fprintf( stderr, "Unknown option '%c'.\n", c );
exit(1);
break;
}
}
if ( argc <= optind + 1 ) {
fprintf( stderr, "Usage: newdate [-s|-l] <DDMMYY> <adjustment>.\n"
);
exit(1);
}
dum[3] = '\0';
memcpy( dum, argv[optind], 2 );
day = atoi( dum );
memcpy( dum, argv[optind] + 2, 2 );
mon = atoi( dum );
memcpy( dum, argv[optind] + 4, 2 );
if ( strcmp( dum, "00" ) ) {
year = atoi( dum );
year = ( year != 0 ) ? year : -1;
year = ( year >= 70 ) ? year : year + 100;
} else {
year = 100;
}
if ( !day || !mon || ( year == -1 ) ) {
fprintf( stderr, "Invalid dateformat" );
exit(1);
}
adj = atoi( argv[ optind + 1 ] );
t.tm_sec = t.tm_min = t.tm_hour = 0;
t.tm_mday = day;
t.tm_mon = mon - 1;
t.tm_year = year;
ti = mktime( &t );
ti += ( 3600 * 24 ) * adj;
tp = localtime( &ti );
/* printf( "%s\n", ctime( &ti ) ); */
if ( fshort ) {
printf( "%02d/%02d\n", tp -> tm_mday, tp -> tm_mon + 1 );
} else {
printf( "%02d/%02d/%02d\n", tp -> tm_mday, tp -> tm_mon + 1, tp ->
tm_year );
}
return 0;
}
---- END C CODE ---------------
========================================================================
Jan de Visser etm...@etm.ericsson.se
ETM/OPP TMOS Technical support jan.de...@nlbdafsc.origin.nl
tel. +31 161 242650
<enter any 12 digit prime to continue>
========================================================================
: I know in Bourne Shell, the command `date + '%yymmdd'`
: would get me something like 970402, but I want the date
: of the day before in the same format.. So I should be
: able to get 970401 on April 2, 97. Can I pase any argments
: to the date program and get this result? The UNIX books I
: have are not of much help.
: Murali
Since this subject comes up frequently, I want to know why is
everyone so anxious to get yesterday's date? I can't help but
think that this is the kind of question that sounds like a
homework assignment in shell programming, in which case the
various kluges offered - use GNU date, play with the TZ variable
(which does not work on all architectures, notably HP-UX, whic
apparently is smart enough to know there are 24 hours in a day) -
won't be satisfactory answers. So tell me, why do you want to
know yesterday's date?
--
Dan Mercer
Reply To: dame...@mmm.com
Opinions expressed herein are my own and may not represent those of my employer.
Murali Chari <cmu...@ix.netcom.com> wrote in article
<5ht61p$l...@dfw-ixnews9.ix.netcom.com>...
Get the GNU version of date - it does exactly this (and more).
--
Matt Ryan - Network Engineer ma...@planet.net.uk
Planet OnLine Ltd, The White House, Tel: +44 113 2345566
Melbourne Street, Leeds, LS2 7PS, UK Fax: +44 113 2345656
have you tried:
date --date yesterday +'%y%m%d'
?
regards
redax
What about:
expr `date +'%y%m%d'` - 1
??? ;-)
gary -=- The C Programmers' Reference -=-
http://users.southeast.net/~garyg/C_ref/C/c.html
-=- Avenue Programmers' Classes' Requests -=-
http://users.southeast.net/~garyg/classes.htm
Yeah right,
970401 - 1 -> 970400
That makes sense, doesn't it!
>
> gary -=- The C Programmers' Reference -=-
> http://users.southeast.net/~garyg/C_ref/C/c.html
> -=- Avenue Programmers' Classes' Requests -=-
> http://users.southeast.net/~garyg/classes.htm
--
I checked this thread out on Deja News and didn't see any Perl
solutions. Here's one:
#!/usr/local/bin/perl -w
use Date::Manip;
print &UnixDate("yesterday", "%y%m%d\n");
I'd prefer YYYY, in which case use %Y instead of %y.
BTW, Date::Manip is a Perl module that does all sorts of handy dandy
date comparisons/calculations.
--
Michael Fuhr
http://www.dimensional.com/~mfuhr/
Murali> Hi, I know in Bourne Shell, the command `date + '%yymmdd'`
Murali> would get me something like 970402, but I want the date of
Murali> the day before in the same format.. So I should be able to
Murali> get 970401 on April 2, 97. Can I pase any argments to the
Murali> date program and get this result? The UNIX books I have
Murali> are not of much help.
Actually date is not part of the bourne shell and the syntax is
the same in any shell.
I'm using GNU date and can get the result you ask for with
`date -dyesterday +'%y%m%d'`. I know this is different on other
versions of date.
Murali> Murali
--
Dale Henderson <mailto:nil...@faeryland.tamu-commerce.edu>
"Imaginary universes are so much more beautiful than this stupidly-
constructed 'real' one..." -- G. H. Hardy
<nil...@faeryland.tamu-commerce.edu> wrote:
I had the same problem in sh / csh, therefor I wrote this small C
prog. If you use Perl instead of shell, its easy
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time
- 86400);
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
main(argt,argv)
int argt;
char **argv;
{
int mode;
int days;
char *month[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"};
struct tm *ptr;
time_t lt;
if (argt == 3)
days = abs(atoi(argv[2]));
else days = 1;
lt = time(NULL) - (86400 * days);
ptr = localtime(<);
mode = abs(atoi(argv[1]));
if(mode == 1)
printf("%s %.2d\n",month[ptr->tm_mon],ptr->tm_mday);
else if(mode == 2)
printf("%s %2d\n",month[ptr->tm_mon],ptr->tm_mday);
else if(mode == 3)
printf("%.2d%.2d%.2d\n",ptr->tm_year,ptr->tm_mon+1,ptr->tm_mday);
else {
printf("No or wrong option selected.\n");
printf("yesterday -1 : %s
%.2d\n",month[ptr->tm_mon],ptr->tm_mday);
printf("yesterday -2 : %s
%2d\n",month[ptr->tm_mon],ptr->tm_mday);
printf("yesterday -3 :
%.2d%.2d%.2d\n",ptr->tm_year,ptr->tm_mon+1,ptr->tm_mday);
printf("yesterday -3 4 :
%.2d%.2d%.2d\n",ptr->tm_year,ptr->tm_mon+1,ptr->tm_mday-3);
}
return 0;
}
Hope it helps,