Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date within range 2

Status
Not open for further replies.

dmazzini

Programmer
Jan 20, 2004
480
US
Hi guys

I have to validate that one date is withinrange of two different dates.

For instance:

DateToCheck = 2008-03-27
InitialDate= 2008-03-23
FinalDate = 2008- 03-28

In this case above, the result will be ok, datetocheck is within range.

Now, I have found that Date::Manip module can do the job,
Actually this routine compare 2 dates and it works good for me.

Code:
sub run{
	
  if (! $date1) {
     Win32::MsgBox("Date 1 is missing please add a Date",0,'REPORTMX');
     redo;
  }  
  if (! $date2) {
     Win32::MsgBox("Date 2 is missing please add a Date",0,'REPORTMX');
     redo;
  }
  
	$date1 = ParseDate($date1);
  $date2 = ParseDate($date2);
  $flag = Date_Cmp($date1,$date2);
  
  if ($flag>0) {
    	Win32::MsgBox("Date 2 is earlier than Date 1. Invalid Option",0,'REPORTMX');
      redo;    
  } 
  
  
  if ($flag<0) {
   Win32::MsgBox("Date 1 is earlier than Date 2",0,'REPORTMX');
   redo;
  } elsif ($flag==0) {
  	Win32::MsgBox("the two dates are identical",0,'REPORTMX');
  	redo;   
  } else {
  Win32::MsgBox("Date 2 is earlier than Date 1",0,'REPORTMX');
  	redo;    
  }	
}

Now, my problem start when I want to compare one specified date ($ms_date) against two dates ($date1 and $date2) (check if $ms_date is within range).
Look at it, maybe it's because it's Friday :-( and I am thinking too much about the weekend..

Code:
sub check_date_range{
    
    $flag1 = Date_Cmp($ms_date,$date1);
    if ($flag1<0) {
    	 $date_within_range_1=1;    	 
    }
    
    if ($flag1==0) {
    	 $date_within_range_1=1;    	 
    }
    
    $flag2 = Date_Cmp($ms_date,$date2);
    if ($flag2<0 && $date_within_range_1==1) {
    	 $date_within_range_2=1;
    }
    
    $ms_is_in_range=1 if ($date_within_range_1==1 && $date_within_range_2==1);
}

What's wrong here?? Advices








dmazzini
GSM/UMTS System and Telecomm Consultant

 
You could convert your dates to epoch seconds and make life easy on yourself.
Code:
use Time::Local;
my ($ms_date,$date1,$date2) = ('2008-03-27', '2008-03-23', '2008-03-28');

# Convert dates to epoch seconds
foreach ($ms_date,$date1,$date2) {
	my ($year, $mon, $mday) = split '-', $_;
	$_ = timelocal(0,0,0,$mday,$mon,$year);
}

my $ms_is_in_range = check_date_range($ms_date,$date1,$date2);
print $ms_is_in_range;

sub check_date_range {
	my ($target, $d1, $d2) = @_;
	return ($d1 <= $target && $target <= $d2) ? 1 : 0;
}
 
Hi

I have found problems for December months. E.g.

Code:
my ($ms_date,$date1,$date2) = ('2007-12-14', '2007-12-11', '2007-11-16');

Error message

Code:
Month '12' out of range 0..11

I have fixed it using:

Code:
$_ = timegm(0, 0, 0, $mday, $mon-1, $year-1900);

Thanks

dmazzini
GSM/UMTS System and Telecomm Consultant

 
Good catch, the Time::Local documentation instructs that you have to subtract one from the month (if necessary) so that it matches localtimes ordering of 0-11 instead of 1-12.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Yeah, that was a good catch. I forgot about that! I should RTFM! :)
 
If your dates are nicely formatted as yyyy-mm-dd or also yyyymmdd, they can be compared as strings and are in the correct order:
Code:
if($ms_date ge $date1 && $ms_date le $date2){
  ...
}

Franco
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top