Checking a date whether it is held between two given dates or not in perl

#! /usr/bin/env perl #0=equal, 1=greater than, -1=smaller than. my $start_date = "2017-06-20 10:17:00"; my $end_date = "2017-06-24 09:47:00"; my $kernel_date = "2017-06-28 10:17:00"; #my $mint_retval = fstr_compareDateAndTime($start_date, $end_date); my $comp1 = fstr_compareDateAndTime($start_date, $end_date); #print $mint_retval; print "A:$comp1,"; sub fstr_compareDateAndTime($$) { # answers how does date1 compare to date2 # (greater than "1", less than "-1", or equal to "0") #my ($mstr_date1, $mstr_date2) = @_; my ($start_date, $end_date) = @_; #my @marr_date1; #my @marr_date2; my @m_start_date; my @m_end_date; my $mint_limit =0; my ($mstr_onlydate1, $mstr_onlytime1) = split(/ /, $start_date); push(@m_start_date,split(/-/, $mstr_onlydate1)); push(@m_start_date,split(/-/, $mstr_onlytime1)); my ($mstr_onlydate2, $mstr_onlytime2) = split(/ /, $end_date); push(@m_end_date,split(/-/, $mstr_onlydate2)); push(@m_end_date,split(/-/, $mstr_onlytime2)); # compare up to the lesser number of elements # (like if one datetime only has a date and no time, don't try to compare time) if(@m_start_date == @m_end_date) { $mint_limit = @m_start_date } elsif (@m_start_date > @m_end_date) { $mint_limit = @m_end_date } elsif (@m_start_date < @m_end_date) { $mint_limit = @m_start_date } for (my $mint_count = 0; $mint_count < $mint_limit; $mint_count++) { if ($m_start_date[$mint_count] > $m_end_date[$mint_count]) { return 1; last; }# date1 greater than date2 if ($m_start_date[$mint_count] < $m_end_date[$mint_count]) { return -1; last; }# date1 less than date2 } return 0;# dates are equal } #2nd Comparision.... my $comp2 = fstr_compareDateAndTime($start_date, $kernel_date); #print $mint_retval; print "B:$comp2,"; sub fstr_compareDateAndTime($$) { # answers how does date1 compare to date2 # (greater than "1", less than "-1", or equal to "0") #my ($mstr_date1, $mstr_date2) = @_; my ($start_date, $kernel_date) = @_; #my @marr_date1; #my @marr_date2; my @m_start_date; my @m_kernel_date; my $mint_limit =0; my ($k_date1, $k_time1) = split(/ /, $start_date); push(@m_start_date,split(/-/, $k_date1)); push(@m_start_date,split(/-/, $k_time1)); my ($k_date2, $k_time2) = split(/ /, $kernel_date); push(@m_end_date,split(/-/, $k_date2)); push(@m_end_date,split(/-/, $k_time2)); # compare up to the lesser number of elements # (like if one datetime only has a date and no time, don't try to compare time) if(@m_start_date == @m_end_date) { $mint_limit = @m_start_date } elsif (@m_start_date > @m_end_date) { $mint_limit = @m_kernel_date } elsif (@m_start_date < @m_end_date) { $mint_limit = @m_start_date } for (my $mint_count = 0; $mint_count < $mint_limit; $mint_count++) { if ($m_start_date[$mint_count] > $m_kernel_date[$mint_count]) { return 1; last; }# date1 greater than date2 if ($m_start_date[$mint_count] < $m_kernel_date[$mint_count]) { return -1; last; }# date1 less than date2 } return 0;# dates are equal } #3rd Comparision my $comp3 = fstr_compareDateAndTime($end_date,$kernel_date); #print $mint_retval; print "C:$comp3\n"; sub fstr_compareDateAndTime($$) { # answers how does date1 compare to date2 # (greater than "1", less than "-1", or equal to "0") #my ($mstr_date1, $mstr_date2) = @_; my ($end_date, $kernel_date) = @_; #my @marr_date1; #my @marr_date2; my @m_end_date; my @m_kernel_date; my $mint_limit =0; my ($n_date1, $n_time1) = split(/ /, $end_date); push(@m_end_date,split(/-/, $n_date1)); push(@m_end_date,split(/-/, $n_time1)); my ($n_date2, $k_time2) = split(/ /, $kernel_date); push(@m_kernel_date,split(/-/, $n_date2)); push(@m_kernel_date,split(/-/, $n_time2)); # compare up to the lesser number of elements # (like if one datetime only has a date and no time, don't try to compare time) if(@m_end_date == @m_kernel_date) { $mint_limit = @m_end_date } elsif (@m_end_date > @m_kernel_date) { $mint_limit = @m_kernel_date } elsif (@m_end_date < @m_kernel_date) { $mint_limit = @m_end_date } for (my $mint_count = 0; $mint_count < $mint_limit; $mint_count++) { if ($m_end_date[$mint_count] > $m_kernel_date[$mint_count]) { return 1; last; }# date1 greater than date2 if ($m_end_date[$mint_count] < $m_kernel_date[$mint_count]) { return -1; last; }# date1 less than date2 } return 0;# dates are equal } if ($comp1 = -1 && $comp2 = -1) { $x=>0; } else { $x=>1; } if ($x=0 && c) { return $x; } else { $x=>1; } print $x; } #if ($comp1 = -1 && $comp2 = -1 && $comp3 = 1) { #print 0; #} else { #print 1; #}
Checking a date whether it is held between two given dates or not by using perl..

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.