Convert seconds to compound duration: Difference between revisions

Content added Content deleted
(Added Lua version)
Line 907:
=={{header|PowerShell}}==
<lang PowerShell>
function OutGet-TimeStringTime
{
<#
.SYNOPSIS
Gets a time string in the form: # wk, # d, # hr, # min, # sec
Convert seconds to a compound duration and format seconds into a time string.
.DESCRIPTION
TakesGets a positivetime integerstring representingin athe durationform: in# wk, # d, # hr, seconds# asmin, input# andsec
(Values of 0 are not displayed in the string.)
returns a string which shows the same duration decomposed into weeks, days,
 
hours, minutes, and seconds.
Days, Hours, Minutes or Seconds in any combination may be used
as well as Start and End dates.
 
When used with the -AsObject switch an object containing properties
similar to a System.TimeSpan object is returned.
.INPUTS
DateTime or Int32
.OUTPUTS
String or PSCustomObject
.EXAMPLE
Get-Time -Seconds 7259
.EXAMPLE
Get-Time -Days 31 -Hours 4 -Minutes 8 -Seconds 16
.EXAMPLE
Get-Time -Days 31 -Hours 4 -Minutes 8 -Seconds 16 -AsObject
.EXAMPLE
OutGet-TimeStringTime -SecondsStart 1003/10/2016 -End 1/20/2017
.EXAMPLE
Get-Time -Start (Get-Date) -End (Get-Date).AddSeconds(6000000)
7259, 86400, 6000000 | Out-TimeString
#>
[CmdletBinding(DefaultParameterSetName='Date')]
[OutputType([string])]
Param
(
[Parameter(Mandatory=$true,# Start date
[Parameter(Mandatory=$false, ParameterSetName='Date',
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[ValidateRange(1,315537811200)datetime]
$Start = (Get-Date),
 
# End date
[Parameter(Mandatory=$false, ParameterSetName='Date',
Position=1)]
[datetime]
$End = (Get-Date),
 
# Days in the time span
[Parameter(Mandatory=$false, ParameterSetName='Time')]
[int]
$SecondsDays = 0,
 
# Hours in the time span
[Parameter(Mandatory=$false, ParameterSetName='Time')]
[int]
$Hours = 0,
 
# Minutes in the time span
[Parameter(Mandatory=$false, ParameterSetName='Time')]
[int]
$Minutes = 0,
 
# Seconds in the time span
[Parameter(Mandatory=$false, ParameterSetName='Time')]
[int]
$Seconds = 0,
 
[switch]
$AsObject
)
 
ProcessBegin
{
[PSCustomObject]$timeObject = "PSCustomObject" |
$timeSpan = New-TimeSpan -Seconds $Seconds
Select-Object -Property Weeks,RemainingDays,
Days,Hours,Minutes,Seconds,Milliseconds,Ticks,
TotalDays,TotalHours,TotalMinutes,TotalSeconds,TotalMilliseconds
[int]$remainingDays = 0
[int]$weeks = 0
 
if ([string[]]$timeSpan.DaystimeString -gt= 6@()
}
Process
{
switch ($PSCmdlet.ParameterSetName)
{
'Date' { $weekstimeSpan = [int](New-TimeSpan -Start $timeSpan.DaysStart /-End $End 7)}
'Time' { $timeSpan = New-TimeSpan -Days $Days -Hours $Hours -Minutes $Minutes -Seconds $Seconds }
$days = $timeSpan.Days % 7
}
else
{
$weeks = 0
$days = $timeSpan.Days
}
 
$weeks = [System.Math]::DivRem($timeSpan.Days, 7, [ref]$remainingDays)
[string[]]$output = @()
 
if ($weeks)timeObject.Weeks { $output += "$weeks wk" }
$timeObject.RemainingDays = $remainingDays
if ($days) { $output += "$days d" }
if ($timeSpantimeObject.Hours)Days { $output += "$($timeSpan.Hours) hr" } = $timeSpan.Days
if ($timeSpantimeObject.Minutes)Hours { $output += "$($timeSpan.Minutes) min" }Hours
if ($timeSpantimeObject.Seconds)Minutes { $output += "$($timeSpan.Seconds) sec" }Minutes
$timeObject.Seconds = $timeSpan.Seconds
$timeObject.Milliseconds = $timeSpan.Milliseconds
$timeObject.Ticks = $timeSpan.Ticks
$timeObject.TotalDays = $timeSpan.TotalDays
$timeObject.TotalHours = $timeSpan.TotalHours
$timeObject.TotalMinutes = $timeSpan.TotalMinutes
$timeObject.TotalSeconds = $timeSpan.TotalSeconds
$timeObject.TotalMilliseconds = $timeSpan.TotalMilliseconds
}
End
{
if ($AsObject) { return $timeObject }
 
if ($timeObject.Weeks) { $timeString += "$($timeObject.Weeks) wk" }
$output -join ", "
if ($timeObject.RemainingDays) { $timeString += "$($timeObject.RemainingDays) d" }
if ($timeObject.Hours) { $timeString += "$($timeObject.Hours) hr" }
if ($timeObject.Minutes) { $timeString += "$($timeObject.Minutes) min" }
if ($timeObject.Seconds) { $timeString += "$($timeObject.Seconds) sec" }
 
return ($timeString -join ", ")
}
}
Line 964 ⟶ 1,027:
{{Out}}
<pre>
PS C:\Scripts> 7259, 86400, 6000000 | OutForEach-TimeStringObject { Get-Time -Seconds $_ }
2 hr, 59 sec
1 d
9 wk, 6 d, 10 hr, 40 min
</pre>
 
 
PS C:\Scripts> Get-Time -Start 3/10/2016 -End 1/20/2017
45 wk, 1 d
 
 
PS C:\Scripts> Get-Time -Seconds 6000000 -AsObject
 
 
Weeks : 9
RemainingDays : 6
Days : 69
Hours : 10
Minutes : 40
Seconds : 0
Milliseconds : 0
Ticks : 60000000000000
TotalDays : 69.4444444444444
TotalHours : 1666.66666666667
TotalMinutes : 100000
TotalSeconds : 6000000
TotalMilliseconds : 6000000000
</pre>
 
=={{header|Python}}==