localtime and Date::Manip

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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");

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" ];

Re: localtime and Date::Manip

soup_or_power@yahoo.com wrote:
Quoted text here. Click to load it

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

Quoted text here. Click to load it

<seemingly complicated code snipped>

Quoted text here. Click to load it

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;

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

Site Timeline