[Rt-commit] rt branch, 4.2/charts, updated. rt-4.1.8-484-g7c068d2

Ruslan Zakirov ruz at bestpractical.com
Wed Jun 12 17:11:45 EDT 2013


The branch, 4.2/charts has been updated
       via  7c068d2fdfaefa24b89b39c372a6b92c9e474281 (commit)
       via  77fa6525da4009e9bbaa2b29dc0c4c4c60af35b0 (commit)
      from  fb5caf40fd61f9fd373408e2f33ca5701e77a78a (commit)

Summary of changes:
 lib/RT/Date.pm           | 122 +++++++++++++++++++++++++++++++++++------------
 lib/RT/Report/Tickets.pm |   2 +-
 2 files changed, 93 insertions(+), 31 deletions(-)

- Log -----------------------------------------------------------------
commit 77fa6525da4009e9bbaa2b29dc0c4c4c60af35b0
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu Jun 13 00:59:44 2013 +0400

    enhance $date->DurationAsString method
    
    * Show argument to show more than one unit, with Show => 2
      people will see more details, for example "4 hours 25 minutes"
      or "4 weeks 1 day".
    * Short argument to produce result with one character units.
    * use quant in localization

diff --git a/lib/RT/Date.pm b/lib/RT/Date.pm
index 5779b03..de80bd1 100644
--- a/lib/RT/Date.pm
+++ b/lib/RT/Date.pm
@@ -334,50 +334,112 @@ sub DiffAsString {
 Takes a number of seconds. Returns a localized string describing
 that duration.
 
+Takes optional named arguments:
+
+=over 4
+
+=item * Show
+
+How many elements to show, how precise it should be. Default is 1,
+most vague variant.
+
+=item * Short
+
+Turn on short notation with one character units, for example
+"3M 2d 1m 10s".
+
+=back
+
 =cut
 
+# loc("[_1]s")
+# loc("[_1]m")
+# loc("[_1]h")
+# loc("[_1]d")
+# loc("[_1]W")
+# loc("[_1]M")
+# loc("[_1]Y")
+# loc("[quant,_1,second]")
+# loc("[quant,_1,minute]")
+# loc("[quant,_1,hour]")
+# loc("[quant,_1,day]")
+# loc("[quant,_1,week]")
+# loc("[quant,_1,month]")
+# loc("[quant,_1,year]")
+
 sub DurationAsString {
     my $self     = shift;
     my $duration = int shift;
+    my %args = ( Show => 1, Short => 0, @_ );
 
-    my ( $negative, $s, $time_unit );
+    my $negative;
     $negative = 1 if $duration < 0;
     $duration = abs $duration;
 
-    if ( $duration < $MINUTE ) {
-        $s         = $duration;
-        $time_unit = $self->loc("sec");
-    }
-    elsif ( $duration < ( 2 * $HOUR ) ) {
-        $s         = int( $duration / $MINUTE + 0.5 );
-        $time_unit = $self->loc("min");
-    }
-    elsif ( $duration < ( 2 * $DAY ) ) {
-        $s         = int( $duration / $HOUR + 0.5 );
-        $time_unit = $self->loc("hours");
-    }
-    elsif ( $duration < ( 2 * $WEEK ) ) {
-        $s         = int( $duration / $DAY + 0.5 );
-        $time_unit = $self->loc("days");
-    }
-    elsif ( $duration < ( 2 * $MONTH ) ) {
-        $s         = int( $duration / $WEEK + 0.5 );
-        $time_unit = $self->loc("weeks");
-    }
-    elsif ( $duration < $YEAR ) {
-        $s         = int( $duration / $MONTH + 0.5 );
-        $time_unit = $self->loc("months");
-    }
-    else {
-        $s         = int( $duration / $YEAR + 0.5 );
-        $time_unit = $self->loc("years");
+    my %units = (
+        s => 1,
+        m => $MINUTE,
+        h => $HOUR,
+        d => $DAY,
+        W => $WEEK,
+        M => $MONTH,
+        Y => $YEAR,
+    );
+    my %long_units = (
+        s => 'second',
+        m => 'minute',
+        h => 'hour',
+        d => 'day',
+        W => 'week',
+        M => 'month',
+        Y => 'year',
+    );
+
+    my @res;
+
+    my $coef = 2;
+    my $i = 0;
+    while ( $duration > 0 && ++$i <= $args{'Show'} ) {
+
+        my $unit;
+        if ( $duration < $MINUTE ) {
+            $unit = 's';
+        }
+        elsif ( $duration < ( $coef * $HOUR ) ) {
+            $unit = 'm';
+        }
+        elsif ( $duration < ( $coef * $DAY ) ) {
+            $unit = 'h';
+        }
+        elsif ( $duration < ( $coef * $WEEK ) ) {
+            $unit = 'd';
+        }
+        elsif ( $duration < ( $coef * $MONTH ) ) {
+            $unit = 'W';
+        }
+        elsif ( $duration < $YEAR ) {
+            $unit = 'M';
+        }
+        else {
+            $unit = 'Y';
+        }
+        my $value = int( $duration / $units{$unit}  + ($i < $args{'Show'}? 0 : 0.5) );
+        $duration -= int( $value * $units{$unit} );
+
+        if ( $args{'Short'} ) {
+            push @res, $self->loc("[_1]$unit", $value);
+        } else {
+            push @res, $self->loc("[quant,_1,$long_units{$unit}]", $value);
+        }
+
+        $coef = 1;
     }
 
     if ( $negative ) {
-        return $self->loc( "[_1] [_2] ago", $s, $time_unit );
+        return $self->loc( "[_1] ago", join ' ', @res );
     }
     else {
-        return $self->loc( "[_1] [_2]", $s, $time_unit );
+        return join ' ', @res;
     }
 }
 

commit 7c068d2fdfaefa24b89b39c372a6b92c9e474281
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Thu Jun 13 01:10:16 2013 +0400

    use short, but less vague time intervals in charts

diff --git a/lib/RT/Report/Tickets.pm b/lib/RT/Report/Tickets.pm
index afa45da..28b9ee6 100644
--- a/lib/RT/Report/Tickets.pm
+++ b/lib/RT/Report/Tickets.pm
@@ -815,7 +815,7 @@ sub DurationAsString {
     my $date = RT::Date->new( $self->CurrentUser );
     my %res = %$v;
     foreach my $e ( values %res ) {
-        $e = $date->DurationAsString( $e ) if defined $e && length $e;
+        $e = $date->DurationAsString( $e, Short => 1, Show => 3 ) if defined $e && length $e;
         $e = $self->loc("(no value)") unless defined $e && length $e;
     }
     return \%res;

-----------------------------------------------------------------------


More information about the Rt-commit mailing list