Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ConvertTo-Approximation {
- #.Synopsis
- # Calculates a relative text version of a duration
- #.Description
- # Generates a string approximation of a timespan, like "x minutes" or "x days." Note this method does not add "about" to the front, nor "ago" to the end unless you pass them in.
- #.Parameter Span
- # A TimeSpan to convert to a string
- #.Parameter Before
- # A DateTime representing the start of a timespan.
- #.Parameter After
- # A DateTime representing the end of a timespan.
- #.Parameter Prefix
- # The prefix string, pass "about" to render: "about 4 minutes"
- #.Parameter Postfix
- # The postfix string, like "ago" to render: "about 4 minutes ago"
- [CmdletBinding(DefaultParameterSetName = "TwoDates")]
- PARAM (
- [Parameter(ParameterSetName = "TimeSpan", Mandatory, Position = 0)][TimeSpan]$TimeSpan,
- [Parameter(ParameterSetName = "TwoDates", Mandatory, ValueFromPipeline)][Alias("DateCreated")][DateTime]$StartTime,
- [Parameter(ParameterSetName = "TwoDates", Mandatory, Position = 0)][DateTime]$EndTime,
- [Parameter(Position = 1)][String]$Prefix = "",
- [Parameter(Position = 2)][String]$Postfix = "",
- [Parameter(Position = 3)][ValidateCount(6, 6)][String[]]$Units = @('seconds', 'a minute', 'minutes', 'an hour', 'hours', 'days')
- )
- PROCESS {
- if ($PSCmdlet.ParameterSetName -eq "TwoDates") {
- $TimeSpan = $EndTime - $StartTime
- }
- "$Prefix $($TimeSpan.Approximate($Units)) $Postfix ".Trim()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment