tags:

views:

93

answers:

4

Hi All, I had a problem with date manipulation.This is my scenario.I have a daily generating report files,they will be generated from Monday to Friday.Now I need to compare those reports.My perl code should work in such a way that if today is Monday I need to compare todays report with previous week Friday's report.And my date format will be like this 20100803 if i give like this i need to be compared with 20100802. If i give Monday's report i.e 20100802 it should compare with 20100730 i.e 30th of July. Can anyone please help me.

Thanks in Advance.

A: 

Depending on the level of performance that you require, there is a Perl package that is a swiss-army-knife of date manipulation and conversion.

It is called Date::Manip.

From that, you could easily ask for any part of the date (like day-of-week or week-of-year), or compare or subtract dates in a really wide variety of formats.

It is doable with other tools too, I'm sure. I've used Date::Manip for funky stuff like dealing with the date string "last friday" and getting a real value.

Andrew Shelansky
+2  A: 

You'll probably want to look at some kind of Date/DateTime object like Date::Calc. With that one, for example, you could split the string into a day, month, and year (using Regex or whatever you'd like to use) for something like:

#
#Set $date = "20100308" somehow, based on your file structure
#

$date =~ /^(\d{4})(\d{2})(\d{2})$/;
$year = $1;
$month = $2;
$day = $3;

$dow = Day_of_Week($day, $month, $year);

if ( $dow == 1 )
    $offset = -3; #Date is a Monday
} else {
    $offset = -1; #Date is Tuesday-Friday
}

#Find the date of the last report
$prev_report_date = Add_Delta_Days($day, $month, $year, $offset);

#
#Compare reports
#

The above is just a generalized example, of course. I don't know exactly how your files are structure or where the date field is coming from, but you can also go to Date::Calc's CPAN page for more help. There are a plethora of other packages that deal with dates, too. This is just one.

eldarerathis
+1  A: 

This won't handle holidays (and you should really think about those), but it's the minimal implementation in Perl:

use strict;
use warnings;

use POSIX ();

my $date_string = '20100802';
my ( $year, $month, $day ) = unpack 'A4 A2 A2', $date_string;
my $today = POSIX::mktime( 0, 0, 0, $day, $month - 1, $year - 1900 );
( $day, $month, $year, my $wday ) = ( localtime $today )[3..6];
my $day_back 
    = POSIX::mktime( 0, 0, 0, $day - ( $wday == 1 ? 3 : 1 ), $month, $year )
    ;
my $day_str = POSIX::strftime( '%Y%m%d', localtime( $day_back ));

Where $time is a specified time value.

Axeman
A: 

With Time::Piece there meanwhile is a base distribution module, that offers date manipulation, comparison etc.

Time::Piece objects e.g. support - and + operators. From the POD:

   The following are valid ($t1 and $t2 are Time::Piece objects):

      $t1 - $t2; # returns Time::Seconds object
      $t1 - 42;  # returns Time::Piece object
      $t1 + 533; # returns Time::Piece object
al