On Jun 28, Katie Elliott said:

>Can you tell me if Date:Calc is the best way for calcuating dates?  I
>would like to pass specific dates to analog to do log file analysis on
>the dates.  

Katie, I tend to use the standard Time::Local module to do the work.  I
don't mind doing some mathematic calculations.  The Date::Calc module
might be fine for you, but I like these approaches:

>1)Previous Month

  use Time::Local;  # for the timelocal() function

  my ($mon, $yr) = (localtime)[4,5];

  # starting day
  my $start = 1;

  # ending day
  my $end = (localtime timelocal(0,0,0, 1,$mon,$yr) - 1)[3];

  # handle month before january
  $mon = 11, $yr-- if --$mon == -1;

  # for visual reasons
  $mon++, $yr += 1900;

  print "Last Month was from $mon/$start/$yr to $mon/$end/$yr\n";


>2)Previous Week

  use Time::Local;  # for the timelocal() function

  my ($day, $mon, $yr, $dow) = (localtime)[3,4,5,6];
  my $now = timelocal(0,0,12, $day,$mon,$yr);  # noon today
  my $week_back = $now - 86400 * 7;            # noon last week
  
  # sunday time
  my $start_t = $week_back - 86400 * $dow;
  my ($start_d, $start_m) = (localtime $start_t)[3,4];
  
  # saturday_time
  my $end_t = $start_t + 86400 * 6;
  my ($end_d, $end_m) = (localtime $end_t)[3,4];      
  
  # for visual reasons
  $start_m++, $end_m++;
  
  print "Last Week was from $start_m/$start_d to $end_m/$end_d\n";          


-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734
**      Manning Publications, Co, is publishing my Perl Regex book      **


  • date:calc Katie Elliott
    • Jeff 'japhy' Pinyan

Reply via email to