perlwannabe <[EMAIL PROTECTED]> wrote:
: 
: I have a relatively simple script that needs to get
: two separate dates, today's date, and yesterday's
: date.  The dates are in mmddyy format.
: Here is the script:
: 
: 
: ##################  BEGIN SCRIPT  ################################
: my ($mday, $mon, $year) = (localtime)[3..5];
: my $datetoday = sprintf ("%02d%02d%02d", ++$mon, $mday, $year-100);
: 
: print ("the value of datetoday is $datetoday\n");
: 
: 
: my ($yesterdaymday, $yesterdaymon, $yesterdayyear) = 
: (localtime)[3..5];
: my $dateyesterday = sprintf ("%02d%02d%02d", ++$yesterdaymon,
: $yesterdaymday-1, $yesterdayyear-100);
: print ("the value of datetoday is $dateyesterday\n");
: 
: ###################  END SCRIPT  #####################################

    A couple of others have pointed out the errors
above and fixes. I'd like to introduce you to the
POSIX function 'strftime' and the perl variable
'$^T'.
    One problem above involves testing very close
to midnight. It is unlikely, but possible for a
routine built on two calls to localtime() to return
the same date for today and yesterday. The first
call would need to be before midnight and the
second call would have to be after midnight.
Admittedly, this would be very uncommon, but
certainly possible.

    The perl variable '$^T' is the time the script
started. Using localtime( $^T ) provides the same
date each time and may still be relevant to the
current time.

    'strftime' is very similar to the same unix
function. The biggest problem is the POSIX
documentation, which assumes you have access to
the unix command line. Here is an online link to
the 'strftime' function.

http://www.scit.wlv.ac.uk/cgi-bin/mansec?3C+strftime


    Here's one solution using strftime() and '$^T':

use POSIX 'strftime';
use constant DATE_FORMAT => '%m%d%y';

        .
        .
        .

my $yesterday = strftime DATE_FORMAT, localtime $^T - 86400;
my $today         = strftime DATE_FORMAT, localtime $^T;

printf "Today: %10s\nYesterday: %4s\n", $today, $yesterday;



HTH,

Charles K. Clarkson
-- 
Head Bottle Washer,
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328















-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to