This function can be used to compare
two dates using PERL. The function accepts two string(date)
arguments, let's say date1 and date2 and returns
0 - If the two dates are equal.
1 - If the date1 passed is greater than
date2.
-1 - If the date1 passed is less than
date2
Note: The Dates to be passed should be
in the following format
YYYY-MM-DD HH:MI:SS
Please find here below Sample script in
which this function is called:
#! /usr/bin/env perl my $mstr_cdate1 = "2008-04-29 12:00:09"; my $mstr_cdate2 = "2008-04-28 22:00:09"; my $mint_retval = fstr_compareDateAndTime($mstr_cdate1, $mstr_cdate2); print $mint_retval; ********************************** Output in this case is 1 as date1 is greater than date2. ######################################################################## # Name : fstr_compareDateAndTime # # Description : compare two dates using Perl # # Parameters : 1. Date1 (string) # 2. Date2 (string) # # Return : Integer. This function can be used to compare two dates using PERL. The function accepts two string(date) #arguments, let's say date1 and date2 and returns # 0 - If the two dates are equal. # 1 - If the date1 passed is greater than date2. # -1 - If the date1 passed is less than date2 # # Calls functions : fstr_compareDateAndTime # # Executables Called : None # # Perl Modules Used : None ######################################################################## 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 @marr_date1; my @marr_date2; my $mint_limit =0; my ($mstr_onlydate1, $mstr_onlytime1) = split(/ /, $mstr_date1); push(@marr_date1,split(/-/, $mstr_onlydate1)); push(@marr_date1,split(/-/, $mstr_onlytime1)); my ($mstr_onlydate2, $mstr_onlytime2) = split(/ /, $mstr_date2); push(@marr_date2,split(/-/, $mstr_onlydate2)); push(@marr_date2,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(@marr_date1 == @marr_date2) { $mint_limit = @marr_date1 } elsif (@marr_date1 > @marr_date2) { $mint_limit = @marr_date2 } elsif (@marr_date1 < @marr_date2) { $mint_limit = @marr_date1 } for (my $mint_count = 0; $mint_count < $mint_limit; $mint_count++) { if ($marr_date1[$mint_count] > $marr_date2[$mint_count]) { return 1; last; }# date1 greater than date2 if ($marr_date1[$mint_count] < $marr_date2[$mint_count]) { return -1; last; }# date1 less than date2 } return 0;# dates are equal }
This script not validate the following scenario.
2015-07-01 00:50:00,2015-07-01 00:00:00, returns : 0
This is a dreadful example, and putting a comment block at the start gives it an air of authority that it doesn't possess
- Don't use Hungarian notation. "fstr_" is meaningless to most people and is generally necessary only with strictly-typed languages that can't otherwise convey the type of a variable. "@marr_" is especially silly because the "@" tells us that it's an array, and "$mint_" is unenforceable because Perl variables may hold many value types simultaneously
- Don't use subroutine prototypes. ($$) here almost certainly doesn't do what you think it does. It gives no advantage and will break your code under sime circumstances
- When you write "split / /" you almost certainly mean "split ' '"
- "@marr_date1" and "@marr_date2" should be "@date" and "@time". Adding numbers to the end of variable names is rarely meaningful
- You shouldn't declare any variables until you need them. That is usually where they are first defined
- You say nothing about the format of the date-times that your subroutine accepts (see the comment above) but unless you want to support the parameters having different parameters then your entire subroutine is better discarded and written as
my $retval = $cdate1 cmp $cdate2
This is a dreadful example, and putting a comment block at the start gives it an air of authority that it doesn't possess
- Don't use Hungarian notation. "fstr_" is meaningless to most people and is generally necessary only with strictly-typed languages that can't otherwise convey the type of a variable. "@marr_" is especially silly because the "@" tells us that it's an array, and "$mint_" is unenforceable because Perl variables may hold many value types simultaneously
- Don't use subroutine prototypes. ($$) here almost certainly doesn't do what you think it does. It gives no advantage and will break your code under sime circumstances
- When you write "split / /" you almost certainly mean "split ' '"
- "@marr_date1" and "@marr_date2" should be "@date" and "@time". Adding numbers to the end of variable names is rarely meaningful
- You shouldn't declare any variables until you need them. That is usually where they are first defined
- You say nothing about the format of the date-times that your subroutine accepts (see the comment above) but unless you want to support the parameters having different parameters then your entire subroutine is better discarded and written as
my $retval = $cdate1 cmp $cdate2