e72c601a195ef7d0f8035bfc83ef8348f8f3e7a0
[ci-management.git] / vagrant / lib / Respin.pm
1 package Respin;
2
3 use strict;
4 use warnings;
5 use DateTime;
6 use DateTime::Format::Duration;
7 use DateTime::Duration;
8 use JSON::XS;
9
10 my $iso8601_rx = qr{^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})$};
11
12 my $json = JSON::XS->new->utf8;
13
14 my $dur_fmt = DateTime::Format::Duration->new(
15     normalize => 1,
16     pattern =>
17       q{{"week":"%V","day":"%u","hour":"%k","minute":"%M","second":"%S"}}
18 );
19
20 sub latest_src_age {
21     my ( $now, $src ) = @_;
22
23     my ( %now, %src );
24     @now{qw(year month day hour minute second)} = ( $now =~ $iso8601_rx );
25     @src{qw(year month day hour minute second)} = ( $src =~ $iso8601_rx );
26
27     print $dur_fmt->format_duration_from_deltas(
28         DateTime->new(%now)->subtract_datetime_absolute( DateTime->new(%src) )
29           ->deltas );
30 }