Convert seconds to compound duration: Difference between revisions

Content added Content deleted
mNo edit summary
No edit summary
Line 857:
3 more to come.
</pre>
 
=={{header|PowerShell}}==
<lang PowerShell>
function Out-TimeString
{
<#
.SYNOPSIS
Convert seconds to a compound duration and format seconds into a time string.
.DESCRIPTION
Takes a positive integer representing a duration in seconds as input and
returns a string which shows the same duration decomposed into weeks, days,
hours, minutes, and seconds.
.EXAMPLE
Out-TimeString -Seconds 100
.EXAMPLE
7259, 86400, 6000000 | Out-TimeString
#>
[CmdletBinding()]
[OutputType([string])]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[ValidateRange(1,315537811200)]
[int]
$Seconds
)
 
Process
{
$timeSpan = New-TimeSpan -Seconds $Seconds
 
if ($timeSpan.Days -gt 6)
{
$weeks = [int]($timeSpan.Days / 7)
$days = $timeSpan.Days % 7
}
else
{
$weeks = 0
$days = $timeSpan.Days
}
 
[string[]]$output = @()
 
if ($weeks) { $output += "$weeks wk" }
if ($days) { $output += "$days d" }
if ($timeSpan.Hours) { $output += "$($timeSpan.Hours) hr" }
if ($timeSpan.Minutes) { $output += "$($timeSpan.Minutes) min" }
if ($timeSpan.Seconds) { $output += "$($timeSpan.Seconds) sec" }
 
$output -join ", "
}
}
</lang>
 
{{Out}}
<pre>
PS C:\Scripts> 7259, 86400, 6000000 | Out-TimeString
2 hr, 59 sec
1 d
9 wk, 6 d, 10 hr, 40 min
</pre>
 
 
=={{header|Python}}==