activeperl@listserv.ActiveState.com
[Top] [All Lists]

Re: More time calculations

Subject: Re: More time calculations
From: "Dennis G. Wicks"
Date: Mon, 7 Mar 2005 17:03:58 -0600 CST


On Mon, 7 Mar 2005, Moreno, Javier wrote:

> Date: Mon, 7 Mar 2005 15:37:56 -0600
> From: "Moreno, Javier" <JavierMoreno@xxxxxxxxx>
> To: ActivePerl@xxxxxxxxxxxxxxxxxxxxxxxx
> Subject: More time calculations
>
> Hi all,
>
> I keep having issues trying to calculate dates. Basically what I want to do 
> is to go to the beginning of the previous month. So, I thought I could deduct 
> the seconds that make up as many days as today is plus the seconds that make 
> up as many days as the last month has. So I ended up with:
>
> $timevalue-=((($thisday - 4) * 24 * 60 * 60) + ($month_days * 24 * 60 * 60));
>
> However, I do not know why I hit it by 4 days off. Could someone let me know 
> if my logic was just wrong, if there's something I'm not aware of or if there 
> is a better way to do it?
>
> Regards,
>
> Javier Moreno
>

Greetings;

For every function there is an equal and opposite function,
or nearly so.

Get your time in the format as from localtime(), adjust the the
desired components appropriately, and convert it back as
follows. In the example I am finding the date 15 days ago.

Good Luck!
Dennis

(I hope this doesn't wrap too badly!)

use Time::Local 'timelocal_nocheck';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year=$year+1900;
print "Time now: secs=$sec min=$min hr=$hour day=$mday mnth=$mon year=$year  
\n";
$mday = $mday-15;
$time = timelocal_nocheck($sec,$min,$hour,$mday,$mon,$year);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
$year=$year+1900;
print "Time then: secs=$sec min=$min hr=$hour day=$mday mnth=$mon year=$year  
\n";

_______________________________________________
ActivePerl mailing list
ActivePerl@xxxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

<Prev in Thread] Current Thread [Next in Thread>