For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > July 2005 > calcule date









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author calcule date
Alexandre Jaquet

2005-07-26, 9:02 am

Hi I need to determine the a date based on calculation

exemple I want to return what's the date when adding

n day to an another date

2005-07-26 15:47:56
+ 31 day = ?
Paul Lalli

2005-07-26, 9:02 am

Alexandre Jaquet wrote:
> Hi I need to determine the a date based on calculation
>
> exemple I want to return what's the date when adding
>
> n day to an another date
>
> 2005-07-26 15:47:56
> + 31 day = ?


There are a rather large amount of modules available on CPAN to deal
with these. Have a look at Date::Calc or Date::Manip (make sure you
read the caveats about whether you should be using that module or not).

Or you could always do things the "hard way", using a combination of
time(), localtime(), and POSIX::strftime()

perldoc -f time
perldoc -f localtime
perldoc POSIX

Paul Lalli

Alexandre Jaquet

2005-07-26, 5:02 pm

Paul Lalli a écrit :
> Alexandre Jaquet wrote:
>
>
>
> There are a rather large amount of modules available on CPAN to deal
> with these. Have a look at Date::Calc or Date::Manip (make sure you
> read the caveats about whether you should be using that module or not).
>
> Or you could always do things the "hard way", using a combination of
> time(), localtime(), and POSIX::strftime()
>
> perldoc -f time
> perldoc -f localtime
> perldoc POSIX
>
> Paul Lalli
>


Thx for responding,

I've made a simple test script :

#!/usr/bin/perl -w
use strict;
use Date::Manip;
use DateTime;

local our
($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst)=localtime();
local our $date = sprintf "%4d-%02d-%02d",$year+1900,$mon+1,$mday;
local our $time = sprintf("%4d:%02d:%02d",$hour,$min,$sec);
$ENV{TZ} = 'EST';
my $currentDate = $date;
my $cutoffDate = &DateCalc($date, "+ 7 days");
local our $year2 = substr($cutoffDate,0,4);
local our $month2 = substr($cutoffDate,4,2);
local our $day2 = substr($cutoffDate,6,2);

local our $date_finish = "$year2-$month2-$day2";

print $date_finish;

who works fine but when I use it in mod_perl env $date_finish doesn't be set

here the function where I use it :

sub loadEnchereProperties {
local our $string;
$ENV{TZ} = 'EST';
local our
($sec,$min,$hour,$mday,$mon,$year,$wday,
$yday,$isdst)=localtime();
local our $date = sprintf "%4d-%02d-%02d",$year+1900,$mon+1,$mday;
local our $time = sprintf("%4d:%02d:%02d",$hour,$min,$sec);


local our $currentDate = $date;
local our $cutoffDate = &DateCalc($date, "+ 7 days");
local our $year2 = substr($cutoffDate,0,4);
local our $month2 = substr($cutoffDate,4,2);
local our $day2 = substr($cutoffDate,6,2);
local our $date_finish = $year2;

$string .="<tr>";
$string .="<td>$SERVER{'debut_enchere_label'}</td>";
$string .="<td><input type=\"text\" name=\"enchere_date_start\"
value=\"$date $time\"></td>";
$string .="</tr>";
$string .="<tr>";
$string .="<td>$SERVER{'duree_enchere_label'}</td>";
$string .="<td><input type=\"text\" name=\"enchere_duration_day\"
value=\"7\"></td>";
$string .="</tr>";
$string .="<tr>";
$string .="<td>$SERVER{'fin_enchere_label'}</td>";
$string .="<td><input type=\"text\" name=\"enchere_end_day\"
value=\"$date_finish\"></td>";
$string .="</tr>";

return $string;
}
Mothra

2005-07-27, 9:03 am

Hi Alexandre,

Alexandre Jaquet" <""alexjaquet\"@[no spam]msn.com wrote:
(snipped)
> #!/usr/bin/perl -w
> use strict;
> use Date::Manip;
> use DateTime;

^^^^^^^^^^^^^

I see you are using DateTime :-) you could do something
like this:

use strict;
use warnings;
use CGI;
use DateTime;
use DateTime::Duration;

my $dt = DateTime->now( time_zone => 'America/Chicago' );

my $dur = DateTime::Duration->new( days => 7 );
my $dt2 = $dt + $dur;

my $table = get_table( $dt, $dt2 );

print $table;

sub get_table {
my ( $dt, $dt2 ) = @_;
my $q = CGI->new;

my $table = $q->table(
{ -border => undef },
$q->Tr(
{ -align => 'CENTER', -valign => 'TOP' },
[
$q->td(
[
'debut_enchere_label',
$q->textfield(
-name => 'enchere_date_start',
-default => "$dt",
-override => 1,
-size => 10,
-maxlength => 20
)
]
),
$q->td(
[
'duree_enchere_label',
$q->textfield(
-name => 'enchere_duration_day',
-default => '7',
-override => 1,
-size => 3,
-maxlength => 5
)
]
),
$q->td(
[
'fin_enchere_label',
$q->textfield(
-name => 'enchere_end_day',
-default => "$dt2",
-override => 1,
-size => 10,
-maxlength => 20
)
]
)
]
)
);

return $table;
}
I hope this helps :-)

Mothra


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com