Convert seconds to compound duration: Difference between revisions
Convert seconds to compound duration (view source)
Revision as of 04:47, 11 March 2016
, 8 years ago→{{header|PowerShell}}
(Added Lua version) |
|||
Line 907:
=={{header|PowerShell}}==
<lang PowerShell>
function
{
<#
.SYNOPSIS
Gets a time string in the form: # wk, # d, # hr, # min, # sec
.DESCRIPTION
(Values of 0 are not displayed in the string.)
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
.EXAMPLE
Get-Time -Start (Get-Date) -End (Get-Date).AddSeconds(6000000)
#>
[CmdletBinding(DefaultParameterSetName='Date')]
Param
(
[Parameter(Mandatory=$false, ParameterSetName='Date',
Position=0)]
[
$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]
$
# 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
)
{
[PSCustomObject]$timeObject = "PSCustomObject" |
Select-Object -Property Weeks,RemainingDays,
Days,Hours,Minutes,Seconds,Milliseconds,Ticks,
TotalDays,TotalHours,TotalMinutes,TotalSeconds,TotalMilliseconds
[int]$remainingDays = 0
[int]$weeks = 0
}
Process
{
switch ($PSCmdlet.ParameterSetName)
{
'Date' { $
'Time' { $timeSpan = New-TimeSpan -Days $Days -Hours $Hours -Minutes $Minutes -Seconds $Seconds }
}
$weeks = [System.Math]::DivRem($timeSpan.Days, 7, [ref]$remainingDays)
$timeObject.RemainingDays = $remainingDays
$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" }
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 |
2 hr, 59 sec
1 d
9 wk, 6 d, 10 hr, 40 min
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}}==
|