using perl to print yesterday's date, but with formatting options ?

E

Eric J. Roode

Hi,

I need a way to use yesterday's date in a variable. The tricks with
'date' don't work on my system, but perl does.

so i've got:
perl -le 'print scalar localtime time - 86400' which outputs:
Sun Oct 30 17:56:52 2005

But the format I need is: 30.10.05

Any ideas on how to add this to the above piece of code ?

It doesn't get much simpler than:

use Time::Format;
print "Yesterday was $time{'dd.mm.yy', time-86400}\n"

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
R

robic0

Hi,

I need a way to use yesterday's date in a variable. The tricks with 'date'
don't work on my system, but perl does.

so i've got:
perl -le 'print scalar localtime time - 86400' which outputs:
Sun Oct 30 17:56:52 2005

But the format I need is: 30.10.05

Any ideas on how to add this to the above piece of code ?

thanks,

tom.

Tom, if your into thinking and roll-your-own, try this:

sub FmtTime
{
my ($ttype, $since) = @_;
$since = time() unless defined $since;
$ttype = 0 unless (defined $ttype);
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday,
$isdst) = localtime($since);
my %months = (1 => 'Jan', 2 => 'Feb', 3 => 'Mar', 4 => 'Apr',
5 => 'May', 6 => 'Jun',
7 => 'Jul', 8 => 'Aug', 9 => 'Sep', 10 => 'Oct',
11 => 'Nov', 12 => 'Dec');
return sprintf ("%s %s %d:%0.2d:%0.2d", $months{$mon+1},
$mday, $hour, $min, $sec) if ($ttype == 1);
return sprintf ("%0.2d%0.2d%4d", $mon+1, $mday, ($year+1900))
if ($ttype == 2);
return sprintf ("%d/%d/%0.2d", $mon+1, $mday, ($year-100)) if
($ttype == 3);
return sprintf ("%0.2d/%0.2d/%0.2d", $mon+1, $mday,
($year-100)) if ($ttype == 4);
if ($ttype == 5) {
my $am_pm = ($hour >= 12 && $min >= 0 && $sec >= 0) ?
"PM" : "AM";
my $hour_12 = ($hour < 13) ? $hour : $hour-12;
return sprintf ("%0.2d/%0.2d/%0.2d - %2s:%0.2d %s",
$mon+1, $mday, ($year-100), $hour_12, $min, $am_pm)
}
return sprintf ("%0.2d/%0.2d/%d", $mon+1, $mday, ($year+1900))
if ($ttype == 6);
#return sprintf ("%0.2d/%0.2d/%0.2d", $mon+1, $mday,
($year-100)) if ($ttype == 6);
return "";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top