localtime and Date::Manip

S

soup_or_power

Is there any advantage in using Date::Manip API instead of localtime?
Here is some sample code I'm working on. Thanks!

my $d1=DateCalc("today", "+ 0 days");
my $sv_mon=0, $sv_yr=0;
for (my $i=0; $i<14;$i++) {

my $d=ParseDate($d1);
my $yr=substr($d,0,4);
my $mon=substr($d,4,2);
my $mday=substr($d,6,2);
push @freq_mday, qq{<OPTION value="$mday"> $mday </OPTION>};
push @freq_mon, qq{<OPTION value="$mon"> $mon </OPTION>} if ($mon
!= $sv_mon);
push @freq_year, qq{<OPTION value="$yr"> $yr </OPTION>} if ($yr
!= $sv_yr);
$d1=DateCalc($d1,"+ 1 days");
$sv_mon=$mon;
$sv_yr=$yr;
}

same thing with localtime:

my $day_ref = [];
my $day = 0;
foreach my $day ( 0 .. 14 ) {
my ( $ss, $mi, $hh, $dd, $mm, $yy ) = localtime( time() + ( $day * 24
* 3600 ) );
$yy += 1900;
$mm += 1;
push @$day_ref, [ "$yy/$mm/$dd", "$yy/$mm/$dd" ];
}
 
G

Gunnar Hjalmarsson

Is there any advantage in using Date::Manip API instead of localtime?

One advantage with using a suitable module (probably some other module
than Date::Manip) is that it takes care of DST. (Alternatively that
might be dealt with by using gmtime() instead of localtime().)
Here is some sample code I'm working on. Thanks!

same thing with localtime:

my $day_ref = [];
my $day = 0;
foreach my $day ( 0 .. 14 ) {
my ( $ss, $mi, $hh, $dd, $mm, $yy ) = localtime( time() + ( $day * 24
* 3600 ) );
$yy += 1900;
$mm += 1;
push @$day_ref, [ "$yy/$mm/$dd", "$yy/$mm/$dd" ];
}

This is how I would have written it:

my $day_ref;
my $time = time; # save current time in variable to
# avoid repeated calls to time()
foreach ( 0 .. 14 ) {
# no reason to assign seconds, minutes and hours to
# variables since they are not used
my ($d, $m, $y) = ( localtime($time + $_ * 24 * 3600) )[3..5];
# suppose you meant to create a reference to an array
# rather than a reference to an AoA
push @$day_ref, sprintf '%d/%d/%d', $y+1900, $m+1, $d;
}
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top