Convert seconds to compound duration: Difference between revisions
Content added Content deleted
(Added Lua version) |
|||
Line 907: | Line 907: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<lang PowerShell> |
<lang PowerShell> |
||
function |
function Get-Time |
||
{ |
{ |
||
<# |
<# |
||
.SYNOPSIS |
.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 |
.DESCRIPTION |
||
Gets a time string in the form: # wk, # d, # hr, # min, # sec |
|||
(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 |
.EXAMPLE |
||
Get-Time -Start 3/10/2016 -End 1/20/2017 |
|||
.EXAMPLE |
.EXAMPLE |
||
Get-Time -Start (Get-Date) -End (Get-Date).AddSeconds(6000000) |
|||
7259, 86400, 6000000 | Out-TimeString |
|||
#> |
#> |
||
[CmdletBinding()] |
[CmdletBinding(DefaultParameterSetName='Date')] |
||
[OutputType([string])] |
|||
Param |
Param |
||
( |
( |
||
# Start date |
|||
[Parameter(Mandatory=$false, ParameterSetName='Date', |
|||
ValueFromPipeline=$true, |
|||
ValueFromPipelineByPropertyName=$true, |
|||
Position=0)] |
Position=0)] |
||
[ |
[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] |
[int] |
||
$ |
$Days = 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 |
|||
) |
) |
||
Begin |
|||
{ |
{ |
||
[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 |
|||
[string[]]$timeString = @() |
|||
} |
|||
Process |
|||
{ |
|||
switch ($PSCmdlet.ParameterSetName) |
|||
{ |
{ |
||
$ |
'Date' { $timeSpan = New-TimeSpan -Start $Start -End $End } |
||
'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 = @() |
|||
$timeObject.Weeks = $weeks |
|||
$timeObject.RemainingDays = $remainingDays |
|||
if ($days) { $output += "$days d" } |
|||
$timeObject.Days = $timeSpan.Days |
|||
$timeObject.Hours = $timeSpan.Hours |
|||
$timeObject.Minutes = $timeSpan.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: | Line 1,027: | ||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
PS C:\Scripts> 7259, 86400, 6000000 | |
PS C:\Scripts> 7259, 86400, 6000000 | ForEach-Object { Get-Time -Seconds $_ } |
||
2 hr, 59 sec |
2 hr, 59 sec |
||
1 d |
1 d |
||
9 wk, 6 d, 10 hr, 40 min |
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}}== |
=={{header|Python}}== |