line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Time::Concise; |
2
|
|
|
|
|
|
|
# $Id: Concise.pm,v 1.1 2004/01/05 20:35:42 cwest Exp $ |
3
|
1
|
|
|
1
|
|
35442
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
44
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
require Exporter; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
4
|
use vars qw[$VERSION @ISA @EXPORT %CONVERT @CONVERT $CONVERT]; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
412
|
|
8
|
|
|
|
|
|
|
$VERSION = (qw$Revision: 1.1 $)[1]; |
9
|
|
|
|
|
|
|
@ISA = qw[Exporter]; |
10
|
|
|
|
|
|
|
@EXPORT = qw[to_concise from_concise]; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Time::Concise - Convert to and from concise duration formats. |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 SYNOPSIS |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use Time::Concise; |
19
|
|
|
|
|
|
|
my $seconds = from_concise "5y4d3h2m1s"; # 158141171 |
20
|
|
|
|
|
|
|
my $concise = to_concise 158141171; # 5y4d3h2m1s |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 DESCRIPTION |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
B exports two functions by default, C and |
25
|
|
|
|
|
|
|
C. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
The term I was borrowed from L. |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head2 Concise Format |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
The format is an integer followed immediatley by its duration |
32
|
|
|
|
|
|
|
identifier. White-space will be ignored. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
The following table explains the format. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
identifier duration |
37
|
|
|
|
|
|
|
---------- -------- |
38
|
|
|
|
|
|
|
y year |
39
|
|
|
|
|
|
|
d day |
40
|
|
|
|
|
|
|
h hour |
41
|
|
|
|
|
|
|
m minute |
42
|
|
|
|
|
|
|
s second |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=cut |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
@CONVERT = qw[y d h m s]; |
47
|
|
|
|
|
|
|
$CONVERT = join '', @CONVERT; |
48
|
|
|
|
|
|
|
%CONVERT = ( |
49
|
|
|
|
|
|
|
y => 31_556_930, |
50
|
|
|
|
|
|
|
d => 86_400, |
51
|
|
|
|
|
|
|
h => 3_600, |
52
|
|
|
|
|
|
|
m => 60, |
53
|
|
|
|
|
|
|
s => 1, |
54
|
|
|
|
|
|
|
); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head2 Functions |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=over 8 |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item to_concise I<$seconds> |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
This function requires one argument, an integer number of seconds, and |
63
|
|
|
|
|
|
|
returns a concise string representation of the duration. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
If the input is not an integer this function returns C. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=cut |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub to_concise($;) { |
70
|
2
|
|
|
2
|
1
|
6
|
my ($seconds) = @_; |
71
|
2
|
100
|
|
|
|
14
|
return undef if $seconds =~ /\D/; |
72
|
1
|
|
|
|
|
3
|
my $string = ''; |
73
|
1
|
|
|
|
|
3
|
foreach my $type ( @CONVERT ) { |
74
|
5
|
|
|
|
|
9
|
my $leftover = $seconds % $CONVERT{$type}; |
75
|
5
|
|
|
|
|
11
|
my $amount = ( $seconds - $leftover ) / $CONVERT{$type}; |
76
|
5
|
50
|
|
|
|
27
|
$string .= "$amount$type" if $amount; |
77
|
5
|
|
|
|
|
9
|
$seconds = $leftover; |
78
|
|
|
|
|
|
|
} |
79
|
1
|
|
|
|
|
9
|
return $string; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=item from_concise I<$concise> |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
This function requires one argument, a concise string representation |
85
|
|
|
|
|
|
|
of the duration, and returns the number of seconds in the duration. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
If the concise string contains characters outside those represented |
88
|
|
|
|
|
|
|
in a concise duration string this function will return C. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=cut |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub from_concise($;) { |
93
|
2
|
|
|
2
|
1
|
12
|
my ($string) = @_; |
94
|
2
|
100
|
|
|
|
45
|
return undef if $string =~ /[^${CONVERT}0-9 ]/o; |
95
|
1
|
|
|
|
|
4
|
my $seconds = 0; |
96
|
1
|
|
|
|
|
5
|
foreach my $type ( @CONVERT ) { |
97
|
5
|
50
|
|
|
|
72
|
if ( my ($amount) = ( $string =~ /(\d+)$type/ ) ) { |
98
|
5
|
|
|
|
|
18
|
$seconds += $amount * $CONVERT{$type}; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
1
|
|
|
|
|
8
|
return $seconds; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=pod |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=back |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=cut |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
1; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
__END__ |