summaryrefslogtreecommitdiffstats
path: root/extensions/BMO/lib/Util.pm
diff options
context:
space:
mode:
Diffstat (limited to 'extensions/BMO/lib/Util.pm')
-rw-r--r--extensions/BMO/lib/Util.pm111
1 files changed, 57 insertions, 54 deletions
diff --git a/extensions/BMO/lib/Util.pm b/extensions/BMO/lib/Util.pm
index dc9c904f9..4d376aecb 100644
--- a/extensions/BMO/lib/Util.pm
+++ b/extensions/BMO/lib/Util.pm
@@ -19,74 +19,77 @@ use DateTime;
use base qw(Exporter);
our @EXPORT = qw( string_to_datetime
- time_to_datetime
- parse_date
- is_active_status_field );
+ time_to_datetime
+ parse_date
+ is_active_status_field );
sub string_to_datetime {
- my $input = shift;
- my $time = parse_date($input)
- or ThrowUserError('report_invalid_date', { date => $input });
- return time_to_datetime($time);
+ my $input = shift;
+ my $time = parse_date($input)
+ or ThrowUserError('report_invalid_date', {date => $input});
+ return time_to_datetime($time);
}
sub time_to_datetime {
- my $time = shift;
- return DateTime->from_epoch(epoch => $time)
- ->set_time_zone('local')
- ->truncate(to => 'day');
+ my $time = shift;
+ return DateTime->from_epoch(epoch => $time)->set_time_zone('local')
+ ->truncate(to => 'day');
}
sub parse_date {
- my ($str) = @_;
- if ($str =~ /^(-|\+)?(\d+)([hHdDwWmMyY])$/) {
- # relative date
- my ($sign, $amount, $unit, $date) = ($1, $2, lc $3, time);
- my ($sec, $min, $hour, $mday, $month, $year, $wday) = localtime($date);
- $amount = -$amount if $sign && $sign eq '+';
- if ($unit eq 'w') {
- # convert weeks to days
- $amount = 7 * $amount + $wday;
- $unit = 'd';
- }
- if ($unit eq 'd') {
- $date -= $sec + 60 * $min + 3600 * $hour + 24 * 3600 * $amount;
- return $date;
- }
- elsif ($unit eq 'y') {
- return str2time(sprintf("%4d-01-01 00:00:00", $year + 1900 - $amount));
- }
- elsif ($unit eq 'm') {
- $month -= $amount;
- while ($month < 0) { $year--; $month += 12; }
- return str2time(sprintf("%4d-%02d-01 00:00:00", $year + 1900, $month + 1));
- }
- elsif ($unit eq 'h') {
- # Special case 0h for 'beginning of this hour'
- if ($amount == 0) {
- $date -= $sec + 60 * $min;
- } else {
- $date -= 3600 * $amount;
- }
- return $date;
- }
- return undef;
+ my ($str) = @_;
+ if ($str =~ /^(-|\+)?(\d+)([hHdDwWmMyY])$/) {
+
+ # relative date
+ my ($sign, $amount, $unit, $date) = ($1, $2, lc $3, time);
+ my ($sec, $min, $hour, $mday, $month, $year, $wday) = localtime($date);
+ $amount = -$amount if $sign && $sign eq '+';
+ if ($unit eq 'w') {
+
+ # convert weeks to days
+ $amount = 7 * $amount + $wday;
+ $unit = 'd';
+ }
+ if ($unit eq 'd') {
+ $date -= $sec + 60 * $min + 3600 * $hour + 24 * 3600 * $amount;
+ return $date;
+ }
+ elsif ($unit eq 'y') {
+ return str2time(sprintf("%4d-01-01 00:00:00", $year + 1900 - $amount));
}
- return str2time($str);
+ elsif ($unit eq 'm') {
+ $month -= $amount;
+ while ($month < 0) { $year--; $month += 12; }
+ return str2time(sprintf("%4d-%02d-01 00:00:00", $year + 1900, $month + 1));
+ }
+ elsif ($unit eq 'h') {
+
+ # Special case 0h for 'beginning of this hour'
+ if ($amount == 0) {
+ $date -= $sec + 60 * $min;
+ }
+ else {
+ $date -= 3600 * $amount;
+ }
+ return $date;
+ }
+ return undef;
+ }
+ return str2time($str);
}
sub is_active_status_field {
- my ($field) = @_;
+ my ($field) = @_;
- if ($field->type == FIELD_TYPE_EXTENSION
- && $field->isa('Bugzilla::Extension::TrackingFlags::Flag')
- && $field->flag_type eq 'tracking'
- && $field->name =~ /_status_/
- ) {
- return $field->is_active;
- }
+ if ( $field->type == FIELD_TYPE_EXTENSION
+ && $field->isa('Bugzilla::Extension::TrackingFlags::Flag')
+ && $field->flag_type eq 'tracking'
+ && $field->name =~ /_status_/)
+ {
+ return $field->is_active;
+ }
- return 0;
+ return 0;
}
1;