Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Finding date 1 day earlier than a given date!

Reply
Thread Tools

Finding date 1 day earlier than a given date!

 
 
Edward
Guest
Posts: n/a
 
      09-08-2004
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";
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      09-08-2004
"Edward" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.


 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-08-2004
Edward <(E-Mail Removed)> wrote:

> 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.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      09-08-2004
Edward wrote:
> 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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding is a given date is a month end date?? rsujatha@gmail.com Java 10 05-15-2006 06:02 AM
Given a date, how to find the beginning date and ending date of that week Matt ASP General 11 11-08-2003 11:24 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Given a date, how to find the beginning date and ending date of that week Matt C Programming 3 11-08-2003 09:07 PM
Given a date, how to find the beginning date and ending date of that week Matt C++ 2 11-08-2003 08:30 PM



Advertisments