Make GetTimePeriod() and others accept days.

GetTimePeriod() was written to take a month
('YYYY-MM') and work with that. Make it accept
not only a month, but also a day ('YYYY-MM-DD')
by adding a $TYpe modifier.

Rename LastMonth() to LastMonthDay() and rewrite
it accordingly.

Rename CheckMonth() to CheckPeriod() and rewrite
it accordingly.

As GetTimePeriod() defaults to 'month' if no
modifier is passed this change should be backwards
compatible.

Signed-off-by: Thomas Hochstein <thh@inter.net>
This commit is contained in:
Thomas Hochstein 2013-09-03 21:47:25 +02:00
parent 599fefbf6a
commit 3634010808

View file

@ -265,33 +265,39 @@ sub ReadGroupList {
################################################################################ ################################################################################
sub GetTimePeriod { sub GetTimePeriod {
################################################################################ ################################################################################
### get a time period to act on from --month option; ### get a time period to act on from --month / --day option;
### if empty, default to last month ### if empty, default to last month / day
### IN : $Month: may be empty, 'YYYY-MM', 'YYYY-MM:YYYY-MM' or 'all' ### IN : $Period: may be empty, 'YYYY-MM(-DD)', 'YYYY-MM(-DD):YYYY-MM(-DD)'
### or 'all'
### $Type : may be 'month' or 'day'
### OUT: $Verbal,$SQL: verbal description and WHERE-clause ### OUT: $Verbal,$SQL: verbal description and WHERE-clause
### of the chosen time period ### of the chosen time period
my ($Month) = @_; my ($Period,$Type) = @_;
# define result variables # define result variables
my ($Verbal, $SQL); my ($Verbal, $SQL);
# define a regular expression for a month # check $Type
my $REMonth = '\d{4}-\d{2}'; $Type = 'month' if (!$Type or ($Type ne 'month' and $Type ne 'day'));
# define a regular expressions for a month or day
my $REPeriod = '\d{4}-\d{2}';
$REPeriod .= '-\d{2}' if ($Type eq 'day');
# default to last month if option is not set # default to last month / day if option is not set
if(!$Month) { if(!$Period) {
$Month = &LastMonth; $Period = &LastMonthDay($Type);
} }
# check for valid input # check for valid input
if ($Month =~ /^$REMonth$/) { if ($Period =~ /^$REPeriod$/) {
# single month (YYYY-MM) # single month/day [YYYY-MM(-DD)]
($Month) = &CheckMonth($Month); ($Period) = &CheckPeriod($Type,$Period);
$Verbal = $Month; $Verbal = $Period;
$SQL = sprintf("month = '%s'",$Month); $SQL = sprintf("%s = '%s'",$Type,$Period);
} elsif ($Month =~ /^$REMonth:$REMonth$/) { } elsif ($Period =~ /^$REPeriod:$REPeriod$/) {
# time period (YYYY-MM:YYYY-MM) # time period [YYYY-MM(-DD):YYYY-MM(-DD)]
$Verbal = sprintf('%s to %s',&SplitPeriod($Month)); $Verbal = sprintf('%s to %s',&SplitPeriod($Period,$Type));
$SQL = sprintf("month BETWEEN '%s' AND '%s'",&SplitPeriod($Month)); $SQL = sprintf("%s BETWEEN '%s' AND '%s'",$Type,
} elsif ($Month =~ /^all$/i) { &SplitPeriod($Period,$Type));
} elsif ($Period =~ /^all$/i) {
# special case: ALL # special case: ALL
$Verbal = 'all time'; $Verbal = 'all time';
$SQL = ''; $SQL = '';
@ -304,58 +310,82 @@ sub GetTimePeriod {
}; };
################################################################################ ################################################################################
sub LastMonth { sub LastMonthDay {
################################################################################ ################################################################################
### get last month from todays date in YYYY-MM format ### get last month/day from todays date in YYYY-MM format
### OUT: last month as YYYY-MM ### IN : $Type : may be 'month' or 'day'
# get today's date ### OUT: last month/day as YYYY-MM(-DD)
my (undef,undef,undef,undef,$Month,$Year,undef,undef,undef) = localtime(time); my ($Type) = @_;
my ($Day,$Month,$Year);
if ($Type eq 'day') {
# get yesterdays's date
(undef,undef,undef,$Day,$Month,$Year,undef,undef,undef) = localtime(time-86400);
# $Month is defined from 0 to 11, so add 1
$Month++;
} else {
# get today's date (month and year)
(undef,undef,undef,undef,$Month,$Year,undef,undef,undef) = localtime(time);
# $Month is already defined from 0 to 11, so no need to decrease it by 1 # $Month is already defined from 0 to 11, so no need to decrease it by 1
$Year += 1900;
if ($Month < 1) { if ($Month < 1) {
$Month = 12; $Month = 12;
$Year--; $Year--;
}; };
# return last month }
$Year += 1900;
# return last month / day
if ($Type eq 'day') {
return sprintf('%4d-%02d-%02d',$Year,$Month,$Day);
} else {
return sprintf('%4d-%02d',$Year,$Month); return sprintf('%4d-%02d',$Year,$Month);
}
}; };
################################################################################ ################################################################################
sub CheckMonth { sub CheckPeriod {
################################################################################ ################################################################################
### check if input (in YYYY-MM form) is valid with MM between 01 and 12; ### check if input (in YYYY-MM(-DD) form) is a valid month / day;
### otherwise, fix it ### otherwise, fix it
### IN : @Month: array of month ### IN : $Type : may be 'month' or 'day'
### OUT: @Month: a valid month ### @Period: array of month/day
my (@Month) = @_; ### OUT: @Period: a valid month/day
foreach my $Month (@Month) { my ($Type,@Period) = @_;
my ($OldMonth) = $Month; foreach my $Period (@Period) {
my ($CalMonth) = substr ($Month, -2); my ($OldPeriod) = $Period;
if ($CalMonth < 1 or $CalMonth > 12) { my ($CalMonth,$CalDay);
$Period .= '-01' if ($Type eq 'month');
$CalDay = substr ($Period, -2);
$CalMonth = substr ($Period, 5, 2);
if ($CalMonth < 1 or $CalMonth > 12 or $CalDay < 1 or $CalDay > 31) {
$CalMonth = '12' if $CalMonth > 12; $CalMonth = '12' if $CalMonth > 12;
$CalMonth = '01' if $CalMonth < 1; $CalMonth = '01' if $CalMonth < 1;
substr($Month, -2) = $CalMonth; substr($Period, 5, 2) = $CalMonth;
&Bleat(1,sprintf("'%s' is an invalid date (MM must be between '01' ". $CalDay = '01' if $CalDay < 1;
"and '12'), set to '%s'.",$OldMonth,$Month)); $CalDay = '31' if $CalDay > 31;
# FIXME! - month with less than 31 days ...
substr($Period, -2) = $CalDay;
&Bleat(1,sprintf("'%s' is an invalid date, set to '%s'.",
$OldPeriod,$Period));
} }
$Period = substr($Period,0,7) if ($Type eq 'month');
} }
return @Month; return @Period;
}; };
################################################################################ ################################################################################
sub SplitPeriod { sub SplitPeriod {
################################################################################ ################################################################################
### split a time period denoted by YYYY-MM:YYYY-MM into start and end month ### split a time period denoted by YYYY-MM(-DD):YYYY-MM(-DD) into start and end
### IN : $Period: time period ### IN : $Period: time period
### OUT: $StartMonth, $EndMonth ### $Type : may be 'month' or 'day'
my ($Period) = @_; ### OUT: $StartTime, $EndTime
my ($StartMonth, $EndMonth) = split /:/, $Period; my ($Period,$Type) = @_;
($StartMonth,$EndMonth) = CheckMonth($StartMonth,$EndMonth); my ($StartTime, $EndTime) = split /:/, $Period;
($StartTime,$EndTime) = CheckPeriod($Type,$StartTime,$EndTime);
# switch parameters as necessary # switch parameters as necessary
if ($EndMonth gt $StartMonth) { if ($EndTime gt $StartTime) {
return ($StartMonth, $EndMonth); return ($StartTime, $EndTime);
} else { } else {
return ($EndMonth, $StartMonth); return ($EndTime, $StartTime);
}; };
}; };