Unix Shell - Finding yesterdays date

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > Finding yesterdays date





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Finding yesterdays date
varkey

2007-12-03, 1:46 am

Hi can anyone tell how we can find yesterdays date using shell ??

thanks in advance
Bill Marcum

2007-12-03, 1:46 am

On 2007-12-03, varkey <anupvarghese@gmail.com> wrote:
>
>
> Hi can anyone tell how we can find yesterdays date using shell ??
>
> thanks in advance

date -d yesterday (if you have GNU date)
Or add this to your crontab:
59 23 * * * date > yesterday

colin.macleod@tesco.net

2007-12-03, 7:33 am

On 3 Dec, 07:19, Bill Marcum <marcumb...@bellsouth.net> wrote:
> date -d yesterday (if you have GNU date)
> Or add this to your crontab:
> 59 23 * * * date > yesterday


Or if you have expect, try:

expect -c 'puts [clock format [clock scan yesterday] -format %m/%d/
%y]'

- adjust the format string to taste, see details at
http://home.houston.rr.com/brianoha...uide.html#Clock
Kenny McCormack

2007-12-03, 7:33 am

In article <slrnfl7bgs.6iv.marcumbill@lark.localnet>,
Bill Marcum <marcumbill@bellsouth.net> wrote:
>On 2007-12-03, varkey <anupvarghese@gmail.com> wrote:
>date -d yesterday (if you have GNU date)
>Or add this to your crontab:
>59 23 * * * date > yesterday


Or:

gawk 'BEGIN {print strftime("%c",systime()-86400)}'

Someone will probably come up with a PERL solution...

William James

2007-12-03, 7:33 am

On Dec 3, 12:53 am, varkey <anupvargh...@gmail.com> wrote:
> Hi can anyone tell how we can find yesterdays date using shell ??
>
> thanks in advance


ruby -r date -e 'puts (Date.today - 1)'
Gretch

2007-12-03, 7:33 am

In news:527e1fa4-c97c-4e0e-9cf1-8c0d15b394b2@l16g2000hsf.googlegroups.com,
William James <w_a_x_man@yahoo.com> wrote:

>
> ruby -r date -e 'puts (Date.today - 1)'


mktime -d -1

using http://www.weirdways.com/technogeek/source/mktime the best, most
versatile and intuitive tool I've found for manipulating date values.

Stephane Chazelas

2007-12-03, 7:33 am

On Mon, 3 Dec 2007 02:14:57 -0800 (PST), William James wrote:
> On Dec 3, 12:53 am, varkey <anupvargh...@gmail.com> wrote:
>
> ruby -r date -e 'puts (Date.today - 1)'

[...]

ksh93 -c 'printf "%T\n" yesterday'

--
Stephane
Stephane Chazelas

2007-12-03, 7:33 am

On 03 Dec 2007 11:39:40 GMT, Stephane Chazelas wrote:
[...]
> ksh93 -c 'printf "%T\n" yesterday'


zsh -c 'zmodload zsh/datetime
strftime %c $((EPOCHSECONDS-86400))'

--
Stephane
thomasriise

2007-12-03, 7:33 am

create this PERL script:

-----
#!/usr/bin/perl
# simulate gnu "date --date '1 day ago'
#
use Getopt::Std;
getopt('d');
if ($opt_d){
use Time::Local;
my ($ctime) = time;
$ctime -= $opt_d*24*60*60;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst) =
localtime($ctime);
printf("%04i%02i%02i\n", $year+1900, $mon+1, $mday);
} else {
print "No flag received. Usage:\n";
print "daysago.pl -d [no. of days]\n";
}
-----

- then run it with:
$ ./daysago.pl -d 1

where 1 is the number of days you want to go back in time ....
Patrick

2007-12-03, 1:28 pm

On 3 Dic, 14:25, thomasriise <thomasri...@gmail.com> wrote:
> create this PERL script:
>
> -----
> #!/usr/bin/perl
> # simulate gnu "date --date '1 day ago'
> #
> use Getopt::Std;
> getopt('d');
> if ($opt_d){
> use Time::Local;
> my ($ctime) = time;
> $ctime -= $opt_d*24*60*60;
> my ($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst) =
> localtime($ctime);
> printf("%04i%02i%02i\n", $year+1900, $mon+1, $mday);} else {
>
> print "No flag received. Usage:\n";
> print "daysago.pl -d [no. of days]\n";}
>
> -----
>
> - then run it with:
> $ ./daysago.pl -d 1
>
> where 1 is the number of days you want to go back in time ....


This is a good script di add or remove days from date:
#!/bin/ksh
#
# Reldate: Calculate a date relative to a given date.
#
# T. Hofmann w...@chbs.ciba.com 1994-09-19
#
# Synopsis: reldate <year> <month> <day> <delta>
#
# Reldate adds <delta> days to the specified date and determines
# the resulting date.
#
# Examples: reldate 1999 12 31 1 --> 2000-1-1
# reldate 1996 4 1 -32 --> 1996-2-29
#
# Diagnostics: Only the Gregorian Calendar is supported
# (effective date: Oct. 15, 1582 in then catholic
# countries, otherwise differing, later dates).
#
# Bug: Reldate produces wrong results for given or resulting dates
# before March 1, 1 B.C. (equals "0 A.D."), Gregorian Calendar.

if (($#!=4))
then SNPS="`basename $0` <year> <month> <day> <delta>"
print "Usage: $SNPS" >&2 ; exit 1
else typeset -i YEAR=$1 MONTH=$2 DAY=$3 DELTA=$4
fi

if ((MONTH<3)) ; then let YEAR-=1 MONTH+=9 ; else let MONTH-=3 ; fi
let DD=D=YEAR*365+YEAR/4-YEAR/100+YEAR/400+\(MONTH*367+7\)/
12+DAY-1+DELTA
let DD-=\(D+1\)/146097 DD+=DD/36524 DD-=\(DD+1\)/1461 YEAR=DD/365
let DOY=D-YEAR*365-YEAR/4+YEAR/100-YEAR/400
let MONTH=\(DOY*12+4\)/367 DAY=DOY+1-\(MONTH*367+7\)/12
if ((MONTH>9)) ; then let YEAR+=1 MONTH-=9 ; else let MONTH+=3 ; fi

print "$YEAR-$MONTH-$DAY" # ISO date notation
Randal L. Schwartz

2007-12-03, 1:28 pm

>>>>> "varkey" == varkey <anupvarghese@gmail.com> writes:

varkey> Hi can anyone tell how we can find yesterdays date using shell ??

I've offered this before, but here it is:

YESTERDAY="`date`"
sleep 86400
echo "it was $YESTERDAY yesterday"

Fouls up on DST boundaries, but close enough for most uses.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment PERL training!
Randal L. Schwartz

2007-12-03, 1:28 pm

>>>>> "thomasriise" == thomasriise <thomasriise@gmail.com> writes:

thomasriise> create this PERL script:

You get points for doing this the hard way, even with Perl.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment PERL training!
Glenn Jackman

2007-12-03, 1:28 pm

At 2007-12-03 08:25AM, "thomasriise" wrote:
> create this PERL script:
>
> -----
> #!/usr/bin/perl
> # simulate gnu "date --date '1 day ago'
> #
> use Getopt::Std;
> getopt('d');
> if ($opt_d){
> use Time::Local;
> my ($ctime) = time;
> $ctime -= $opt_d*24*60*60;
> my ($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst) =
> localtime($ctime);
> printf("%04i%02i%02i\n", $year+1900, $mon+1, $mday);
> } else {
> print "No flag received. Usage:\n";
> print "daysago.pl -d [no. of days]\n";
> }
> -----
>
> - then run it with:
> $ ./daysago.pl -d 1


Or:
perl -MDate::Manip -le 'print UnixDate(ParseDate("yesterday"),"%Y%m%d")'
or
perl -M'Date::Calc q{:all}' -le '
printf "%4d%02d%02d", Add_Delta_Days(Today(), -1)
'

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
William James

2007-12-03, 1:28 pm

On Dec 3, 4:14 am, William James <w_a_x_...@yahoo.com> wrote:
> On Dec 3, 12:53 am, varkey <anupvargh...@gmail.com> wrote:
>
>
>
> ruby -r date -e 'puts (Date.today - 1)'


The parentheses were superfluous.

ruby -r date -e 'puts Date.today - 1'
Cyrus Kriticos

2007-12-03, 1:28 pm


Glenn Jackman wrote:
> Or:
> PERL -MDate::Manip -le 'print UnixDate(ParseDate("yesterday"),"%Y%m%d")'
> or
> PERL -M'Date::Calc q{:all}' -le '
> printf "%4d%02d%02d", Add_Delta_Days(Today(), -1)
> '


or

$ PERL -le ($d,$d,$d,$d,$m,$y)=localtime(time-86400);$y+=1900;print"$d.$m.$y"'
2.11.2007
--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
Edward Rosten

2007-12-03, 7:25 pm

On Dec 2, 11:53 pm, varkey <anupvargh...@gmail.com> wrote:
> Hi can anyone tell how we can find yesterdays date using shell ??
>
> thanks in advance


If you have gawk, then get the current time and subtract 24 hours.

BEGIN{print strftime("%c", systime() - 60*60*24)}

You may want to choose something other than %c.

-Ed

--
(You can't go wrong with psycho-rats.) (http://mi.eng.cam.ac.uk/
~er258)

/d{def}def/f{/Times findfont s scalefont setfont}d/s{11}d/r{roll}d f 2/
m
{moveto}d -1 r 230 350 m 0 1 179{1 index show 88 rotate 4 mul 0
rmoveto}
for /s 12 d f pop 235 420 translate 0 0 moveto 1 2 scale show
showpage
Cyrus Kriticos

2007-12-03, 7:25 pm

Cyrus Kriticos wrote:
>
> Glenn Jackman wrote:
>
> or
>
> $ PERL -le
> ($d,$d,$d,$d,$m,$y)=localtime(time-86400);$y+=1900;print"$d.$m.$y"'
> 2.11.2007


fix:

perl -le '($d,$d,$d,$d,$m,$y)=localtime(time-86400);$y+=1900;print"$d.$m.$y"'

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
John DuBois

2007-12-04, 7:25 pm

In article <a01f92f8-1daa-4734-a53f-933f53eca5ea@e10g2000prf.googlegroups.com>,
Edward Rosten <Edward.Rosten@gmail.com> wrote:
>On Dec 2, 11:53 pm, varkey <anupvargh...@gmail.com> wrote:
>
>If you have gawk, then get the current time and subtract 24 hours.
>
>BEGIN{print strftime("%c", systime() - 60*60*24)}


It's worth pointing out that a common meaning of "yesterday's date" is not the
date of 24 hours ago, but rather the calendar date of the day prior to the
current day. In locales that use DST, those aren't the same thing. By the
latter meaning, all of these "subtract 24 hours" methods will produce incorrect
results during certain periods near the DST transitions, and those that use
similar means for "n days in the past/future" will produce incorrect results
for other times as well.

John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
thomasriise

2007-12-05, 7:32 am

On Dec 3, 5:17 pm, mer...@stonehenge.com (Randal L. Schwartz) wrote:

> You get points for doing this the hard way, even with Perl.


Thanks. I use it as an utility for an archive script on solaris. Some
file types I archive x-days back in time, and others xy-days back in
time and so fourth... The datestamp is easily set as a parameter for
these tasks.
Bobby.Higgins

2007-12-29, 1:28 pm

I live in the CST time zone so I use:
echo "$(TZ=CST+30 date '+%m/%d/%Y %H:%H:%S')

"varkey" <anupvarghese@gmail.com> wrote in message
news:09bcd307-8914-4086-98b6-47d233793bd9@d27g2000prf.googlegroups.com...
> Hi can anyone tell how we can find yesterdays date using shell ??
>
> thanks in advance



Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com