summaryrefslogtreecommitdiffstats
path: root/vagrant/lib/Respin.pm
blob: e72c601a195ef7d0f8035bfc83ef8348f8f3e7a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package Respin;

use strict;
use warnings;
use DateTime;
use DateTime::Format::Duration;
use DateTime::Duration;
use JSON::XS;

my $iso8601_rx = qr{^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})$};

my $json = JSON::XS->new->utf8;

my $dur_fmt = DateTime::Format::Duration->new(
    normalize => 1,
    pattern =>
      q{{"week":"%V","day":"%u","hour":"%k","minute":"%M","second":"%S"}}
);

sub latest_src_age {
    my ( $now, $src ) = @_;

    my ( %now, %src );
    @now{qw(year month day hour minute second)} = ( $now =~ $iso8601_rx );
    @src{qw(year month day hour minute second)} = ( $src =~ $iso8601_rx );

    print $dur_fmt->format_duration_from_deltas(
        DateTime->new(%now)->subtract_datetime_absolute( DateTime->new(%src) )
          ->deltas );
}