File Coverage

blib/lib/DateTime/Format/Duration/ConciseHMS.pm
Criterion Covered Total %
statement 19 19 100.0
branch 4 4 100.0
condition 6 9 66.6
subroutine 5 5 100.0
pod 2 2 100.0
total 36 39 92.3


line stmt bran cond sub pod time code
1             package DateTime::Format::Duration::ConciseHMS;
2              
3             our $DATE = '2019-06-19'; # DATE
4             our $VERSION = '0.001'; # VERSION
5              
6 1     1   631763 use 5.010001;
  1         11  
7 1     1   6 use strict;
  1         3  
  1         27  
8 1     1   5 use warnings;
  1         2  
  1         326  
9              
10             sub new {
11 1     1 1 1622 my ($class, %args) = @_;
12              
13 1         5 return bless \%args, $class;
14             }
15              
16             sub format_duration {
17 5     5 1 518 my ($self, $dtdur) = @_;
18              
19 5 100       9 unless (eval { $dtdur->isa('DateTime::Duration') }) {
  5         32  
20 1         10 die "'$dtdur' not a DateTime::Duration instance";
21             }
22              
23 4         16 my ($y, $m, $w, $d, $H, $M, $S, $ns) = (
24             $dtdur->years,
25             $dtdur->months,
26             $dtdur->weeks,
27             $dtdur->days,
28             $dtdur->hours,
29             $dtdur->minutes,
30             $dtdur->seconds,
31             $dtdur->nanoseconds,
32             );
33              
34 4         983 $d += $w * 7;
35              
36 4   33     21 my $has_date = $y || $m || $w || $d;
37 4   66     17 my $has_time = $H || $M || $S;
38              
39 4 100 100     61 join(
40             " ",
41             ("${y}y") x !!$y,
42             ("${m}mo") x !!$m,
43             ("${d}d") x !!$d,
44             (
45             sprintf("%02d:%02d:%02d%s", $H, $M, $S,
46             $ns ? sprintf(".%03d", $ns/1e6) : "")
47             ) x !!($has_time || !$has_date),
48              
49             );
50             }
51              
52             1;
53             # ABSTRACT: Format DateTime::Duration object as concise HMS format
54              
55             __END__
56              
57             =pod
58              
59             =encoding UTF-8
60              
61             =head1 NAME
62              
63             DateTime::Format::Duration::ConciseHMS - Format DateTime::Duration object as concise HMS format
64              
65             =head1 VERSION
66              
67             This document describes version 0.001 of DateTime::Format::Duration::ConciseHMS (from Perl distribution DateTime-Format-Duration-ConciseHMS), released on 2019-06-19.
68              
69             =head1 SYNOPSIS
70              
71             use DateTime::Format::Duration::ConciseHMS;
72              
73             my $format = DateTime::Format::Duration::ConciseHMS->new;
74             say $format->format_duration(
75             DateTime::Duration->new(years=>3, months=>5, seconds=>10),
76             ); # => "3y 5mo 00:00:10"
77              
78             =head1 DESCRIPTION
79              
80             This module formats L<DateTime::Duration> objects as "concise HMS" format.
81             Duration of days and larger will be represented like "1y" (1 year), "2mo" (2
82             months), "3d" (3 days) while duration of hours/minutes/seconds will be
83             represented using hh:mm:ss e.g. 04:05:06. Examples:
84              
85             00:00:00
86             1d
87             3y 5mo 00:00:10.123
88              
89             =head1 METHODS
90              
91             =head2 new
92              
93             =head2 format_duration
94              
95             =head1 HOMEPAGE
96              
97             Please visit the project's homepage at L<https://metacpan.org/release/DateTime-Format-Duration-ConciseHMS>.
98              
99             =head1 SOURCE
100              
101             Source repository is at L<https://github.com/perlancar/perl-DateTime-Format-Duration-ConciseHMS>.
102              
103             =head1 BUGS
104              
105             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=DateTime-Format-Duration-ConciseHMS>
106              
107             When submitting a bug or request, please include a test-file or a
108             patch to an existing test-file that illustrates the bug or desired
109             feature.
110              
111             =head1 SEE ALSO
112              
113             L<DateTime::Duration>
114              
115             =head1 AUTHOR
116              
117             perlancar <perlancar@cpan.org>
118              
119             =head1 COPYRIGHT AND LICENSE
120              
121             This software is copyright (c) 2019 by perlancar@cpan.org.
122              
123             This is free software; you can redistribute it and/or modify it under
124             the same terms as the Perl 5 programming language system itself.
125              
126             =cut