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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
;# NAME
;# elapsed.pl - convert seconds to elapsed time format
;#
;# AUTHOR
;# Michael S. Muegel <mmuegel@mot.com>
;#
;# RCS INFORMATION
;# mmuegel
;# /usr/local/ustart/src/mail-tools/dist/foo/libs/elapsed.pl,v
;# 1.1 of 1993/07/28 08:07:19
package elapsed;
# Time field types
$DAYS = 1;
$HOURS = 2;
$MINUTES = 3;
$SECONDS = 4;
# The array contains four records each with four fields. The fields are,
# in order:
#
# Type Specifies what kind of time field this is. Once of
# $DAYS, $HOURS, $MINUTES, or $SECONDS.
#
# Multiplier Specifies what time field this is via the minimum
# number of seconds this time field may specify. For
# example, the minutes field would be non-zero
# when there are 60 or more seconds.
#
# Separator How to separate this time field from the next
# *greater* field.
#
# Format sprintf() format specifier on how to print this
# time field.
@MULT_AND_SEPS = ($DAYS, 60 * 60 * 24, "+", "%d",
$HOURS, 60 * 60, ":", "%d",
$MINUTES, 60, ":", "%02d",
$SECONDS, 1, "", "%02d"
);
;###############################################################################
;# Seconds_To_Elapsed
;#
;# Coverts a seconds count to form [d+]h:mm:ss. If $Collapse
;# is true then the result is compacted somewhat. The string returned
;# will be of the form [d+][[h:]mm]:ss.
;#
;# Arguments:
;# $Seconds, $Collapse
;#
;# Examples:
;# &Seconds_To_Elapsed (0, 0) -> 0:00:00
;# &Seconds_To_Elapsed (0, 1) -> :00
;#
;# &Seconds_To_Elapsed (119, 0) -> 0:01:59
;# &Seconds_To_Elapsed (119, 1) -> 01:59
;#
;# &Seconds_To_Elapsed (3601, 0) -> 1:00:01
;# &Seconds_To_Elapsed (3601, 1) -> 1:00:01
;#
;# &Seconds_To_Elapsed (86401, 0) -> 1+0:00:01
;# &Seconds_To_Elapsed (86401, 1) -> 1+:01
;#
;# Returns:
;# $Elapsed
;###############################################################################
sub main'Seconds_To_Elapsed
{
local ($Seconds, $Collapse) = @_;
local ($Type, $Multiplier, @Multipliers, $Separator, $DHMS_Used,
$Elapsed, @Mult_And_Seps, $Print_Field);
$Multiplier = 1;
@Mult_And_Seps = @MULT_AND_SEPS;
# Keep subtracting the number of seconds corresponding to a time field
# from the number of seconds passed to the function.
while (1)
{
($Type, $Multiplier, $Separator, $Format) = splice (@Mult_And_Seps, 0, 4);
last if (! $Multiplier);
$Seconds -= $DHMS_Used * $Multiplier
if ($DHMS_Used = int ($Seconds / $Multiplier));
# Figure out if we should print this field
if ($Type == $DAYS)
{
$Print_Field = $DHMS_Used;
}
elsif ($Collapse)
{
if ($Type == $HOURS)
{
$Print_Field = $DHMS_Used;
}
elsif ($Type == $MINUTES)
{
$Print_Field = $DHMS_Used || $Printed_Field {$HOURS};
}
else
{
$Format = ":%02d"
if (! $Printed_Field {$MINUTES});
$Print_Field = 1;
};
}
else
{
$Print_Field = 1;
};
$Printed_Field {$Type} = $Print_Field;
$Elapsed .= sprintf ("$Format%s", $DHMS_Used, $Separator)
if ($Print_Field);
};
return ($Elapsed);
};
1;
|