Navigation Menu

Skip to content

Commit

Permalink
h4x: Conditionally add units to uptime.
Browse files Browse the repository at this point in the history
  • Loading branch information
bambams committed Sep 24, 2015
1 parent ca49b7e commit 4d464ce
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions lib/Bambot.pm
Expand Up @@ -368,12 +368,36 @@ sub get_uptime_str {
my ($self) = @_;

my $now = DateTime->now();

my $uptime = $now - $self->{creation_date};

my $pattern = sub {
my ($duration) = @_;

my ($years, $months, $days, $hours, $minutes) =
$duration->in_units(qw/years months days hours minutes/);

my $toggle = 0;

my $pattern = join ' ', (reverse
map $_->[0],
grep $_->[1],
reverse
['%1y years', $toggle += $years],
['%1m months', $toggle += $months],
['%1e days', $toggle += $days],
['%1H hours', $toggle += $hours],
['%1M minutes', $toggle += $minutes]),
'%1s seconds';

return $pattern;
};

my $formatter = DateTime::Format::Duration->new(
base => $now,
normalize => 1,
pattern => "%e days %H hours %M minutes %S seconds");
pattern => $pattern->($uptime));

my $uptime = $now - $self->{creation_date};
my $str = sprintf 'Up for %s.', $formatter->format_duration($uptime);

my $last_connected_date = $self->{connected_date}[-1];
Expand All @@ -385,6 +409,11 @@ sub get_uptime_str {
return $str if DateTime::Duration->compare(
$uptime, $connected_duration, $now) == 0;

$formatter = DateTime::Format::Duration->new(
base => $now,
normalize => 1,
pattern => $pattern->($connected_duration));

$str = $str . sprintf ' Last connected %s ago.',
$formatter->format_duration($connected_duration);

Expand Down

0 comments on commit 4d464ce

Please sign in to comment.