> After browsing a lot of documentation I cannot find how to get
yesterday's or tomorrow's date in Perl, like `date --date "-1 day"
"+%Y%m%d"` in bash.
localtime() takes an epoc number (number of seconds from 1/1/1970) and
produces either the date data list (perldoc -f localtime)::
# 0 1 2 3 4 5 6 7 8
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
- where 'time()' produces the current epoch number ("1228321233" as I type)
or, in scalar context, a nicely formated date string:
$ perl -e 'print time() . "\n"'
1228321233
$ perl -e 'print localtime() . "\n"'
Wed Dec 3 10:21:23 2008
(by default, 'time()' is the param for localtime() ). So you can get the
values for yest by subtracting 1 day worth of seconds:
my ($mday, $mon, $year) = (localtime(time() - 60 * 60 * 24) ) [ 3,4,5];
printf("%d/%02d/%02d\n", $year + 1900, $mon + 1, $mday - 1);
- note, month is zero based (hence the +1) and year is given as of 1900
(that is, the value for 2008 is 108). If you want a 2 digit year, you need
to mod by 100.
Otherwise there's the core module POSIX which include strftime which lets
you use the full bash/unix 'date' command's format strings. See perldoc
POSIX for details.
a
-------------------
Andy Bach
Systems Mangler
Internet: [EMAIL PROTECTED]
Voice: (608) 261-5738 Fax: 264-5932
If I have seen farther than others, it is because I was standing on the
shoulders of giants. -- Isaac Newton
In the sciences, we are now uniquely privileged to sit side by side
with the giants on whose shoulders we stand. -- Gerald
Holton
If I have not seen as far as others, it is because giants were standing
on my shoulders. -- Hal Abelson
In computer science, we stand on each other's feet. -- Brian
K. Reid
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs