Skip to content

Instantly share code, notes, and snippets.

@fatherjack
Created December 19, 2018 09:31
Show Gist options
  • Save fatherjack/2af7bfdd2e43320878a721a075919e90 to your computer and use it in GitHub Desktop.
Save fatherjack/2af7bfdd2e43320878a721a075919e90 to your computer and use it in GitHub Desktop.
Function Change-Time {
<#
.SYNOPSIS
To convert a larger time measure to a smaller measure ie how many milliseconds in an hour
.DESCRIPTION
Function takes a duration(a) and a measure(b) and returns an amount in a preferred measure(c)
eg 4(a) hours(b) milliseconds(c)
.EXAMPLE
Change-Time -duration 4 -interval hours -output milliseconds
To get the number of milliseconds in 4 hours
.EXAMPLE
Change-Time -duration 1440 -interval minutes -output milliseconds
To get the number of milliseconds in 1440 minutes
#>
[CmdletBinding()]
param(
# duration is the amount of time to be translated
[parameter(mandatory=$true)]
[int]$duration = 1,
# Interval is the input time measure
[parameter(mandatory=$true)]
[validateset('milliseconds','seconds','minutes','hours')]
[string]$interval = "hours",
# Output is the output time measure
[validateset('milliseconds','seconds','minutes','hours')]
$output = "milliseconds"
)
begin {$msg = $null}
process {
switch ($interval) {
"milliseconds" {$ts = New-TimeSpan -Seconds $duration/1000}
"seconds" {$ts = New-TimeSpan -Seconds $duration}
"minutes" {$ts = New-TimeSpan -Minutes $duration}
"hours" {$ts = New-TimeSpan -Hours $duration}
"days" {$ts = New-TimeSpan -Days $duration}
}
switch ($output) {
"milliseconds" {$msg =$ts.totalmilliseconds}
"seconds" {$msg = $ts.totalseconds}
"minutes" {$msg = $ts.totalminutes}
"hours" {$msg = $ts.hours}
}
[string]$return = "$duration $interval equals $msg $output"
return $return
}
end {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment