Finding date 1 day earlier than a given date!

E

Edward

Hi all,

The below script works fine for all dates (change the value in $time),
except if the date is the 2nd of the month.

Given a date of 02/09/04 the script returns the date 32/08/04.

It's kind of the correct result in terms of number of days/months, but
should obviously read 01/09/04!

Anyone know how I can fix it??

Thanks,

Edward.


#!/path/to/perl

use POSIX;

$time="01/09/04";
my ($d, $m, $y) = split ('/', $time);
my $s = mktime (0, 0, 0, $d - 1, $m - 1, $y - 1900);
($d, $m, $y) = (localtime($s - 86400))[3..5];
$time = sprintf ('%02d/%02d/%04d', $d + 1, $m + 1, $y + 1900);
print "$time\n";
 
P

Paul Lalli

Edward said:
The below script works fine for all dates (change the value in $time),
except if the date is the 2nd of the month.

Given a date of 02/09/04 the script returns the date 32/08/04.

No it doesn't. As written, it prints nothing at all. Partially because
you didn't enable warnings, and partially because you gave the time in a
2-digit format, but told mktime to expect it in a 4-digit format.

DO NOT RETYPE CODE. Always always always copy and paste what you
actually have into your post.
It's kind of the correct result in terms of number of days/months, but
should obviously read 01/09/04!

Anyone know how I can fix it??
#!/path/to/perl
use strict;
use warnings;
use POSIX;

$time="01/09/04";
my $time = '01/09/2004';
my ($d, $m, $y) = split ('/', $time);
my $s = mktime (0, 0, 0, $d - 1, $m - 1, $y - 1900);

This makes $s a timestamp to represent one day ago. So far so good...
($d, $m, $y) = (localtime($s - 86400))[3..5];

This subtracts another full day from the time [1]
$time = sprintf ('%02d/%02d/%04d', $d + 1, $m + 1, $y + 1900);

This creates a time one day ahead of $s.
print "$time\n";

So the question becomes - why are you subtracting two days and then
adding one. Granted, x -2 + 1 is always equal to x - 1, but wouldn't it
be easier to just subtract the one day and be done with it? This is, of
course, the cause of the problem you're seeing. You subtract two days
from Sept 2nd, leaving you with Aug 31, and then you manually add to the
day count, rather than having the time functions do the day count.

#!/usr/bin/perl
use strict;
use warnings;
use POSIX;
my $time="02/09/2004";
my ($d, $m, $y) = split ('/', $time);
my $s = mktime (0, 0, 0, $d - 1, $m - 1, $y - 1900);
($d, $m, $y) = (localtime($s))[3..5];
$time = sprintf ('%02d/%02d/%04d', $d, $m + 1, $y + 1900);
print "$time\n";

__END__

I suspect that your problem was in not understanding the $mday parameter
to mktime() and $mday return value from localtime(). Allow me to
suggest you reread
perldoc -f localtime
and
perldoc POSIX

Paul Lalli

[1] This is generally a bad idea, as not every day consists of exactly
38400 seconds. Daylight Savings Time must be accounted for. The
builtins you used account for it, your manual calculation of 60 x 60 x
24 does not.
 
T

Tad McClellan

Edward said:
Given a date of 02/09/04 the script returns the date 32/08/04.

#!/path/to/perl


You should always enable warnings when developing Perl code.

use POSIX;

$time="01/09/04";
my ($d, $m, $y) = split ('/', $time);
my $s = mktime (0, 0, 0, $d - 1, $m - 1, $y - 1900);
^^^^^^^^^
^^^^^^^^^ $y + 100

($d, $m, $y) = (localtime($s - 86400))[3..5];
$time = sprintf ('%02d/%02d/%04d', $d + 1, $m + 1, $y + 1900);
^^^^^^

The day value from localtime() is not off-by-one.
 
G

Gunnar Hjalmarsson

Edward said:
The below script works fine for all dates (change the value in
$time), except if the date is the 2nd of the month.

No, it does not. It does not work for any date.
Anyone know how I can fix it??

Read the docs for the function you are using and find out which
arguments it expects. Compare that with what you actually are passing
to the function.
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top