Gossamer Forum
Home : Products : DBMan : Customization :

&get_date to &get_yesterday

Quote Reply
&get_date to &get_yesterday
(I've also posted this in a more specific thread, but the question has become more general).

How can I make a subroutine that functions just like sub get_date, but always returns yesterday's date in the correct format?

Here's what I've tried- this works TODAY, but I'm afraid it will soon break, for example on the 1st of a month. Someone who knows perl better than I should be able to answer this!

sub get_yesterday {
#------------------------------------------------------------
# Returns yesterdays date in proper format
#
my ($time) = $_[0];
($time) or ($time = time());
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time);
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
$day=$day-1;
($day < 10) and ($day = "0$day");
$year = $year + 1900;
return "$day-$months[$mon]-$year";
}

John

Quote Reply
Re: &get_date to &get_yesterday In reply to
OK, I'm replying to myself again today.

Am I the only one working on perl scripts on Sunday morning? (PDT)

I think I figured out my get_yesterday function:

sub get_yesterday {
#------------------------------------------------------------
# Returns yesterdays date in proper format
#

my ($time) = $_[0];
($time) or ($time = time());
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime(($time-86400));
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;

return "$day-$months[$mon]-$year";
}

Because, as I understand it, the localtime($time) call is parsing the number of seconds since 1970, returned by time.

So, subtracting 86400 from that number before it is parsed should give yesterdays local time, right? What the local time was 86,400 seconds ago?

It seems to work when I write a simple printenv.cgi to test it. But again, will it break on odd days?

John
Quote Reply
Re: &get_date to &get_yesterday In reply to
I'm around, at least for a while. :-)

Yep. That would be the way to do it. It shouldn't "break" at all.

Another way to do it would be to just have a call to sub get_date:

Code:
&get_date(time()-86400);


------------------
JPD