Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Update-TypeData -Force -TypeName TimeSpan -MemberType ScriptMethod -MemberName Approximate -Value {
- if ($args.Count -gt 1) {
- throw ('Cannot find an overload for "{0}" and the argument count: "{1}".' -f 'Approximate', $Args.Count)
- }
- function Round([double]$Number, [string]$Unit) {
- $Number, $Half = if ((($Number % 1) -gt 0.25) -and (($Number % 1) -lt 0.75)) { ($Number - 0.5), '½' } else { $Number, '' }
- '{0}{1} {2}' -f [System.Math]::Round($Number, 0), $Half, $Unit
- }
- $Units = @('seconds', 'a minute', 'minutes', 'an hour', 'hours', 'days')
- if ($args.Count -gt 0 -and $args[0] -is [array]) {
- for ($i = 0; ($i -lt $Units.Count) -and ($i -lt $args[0].Count); $i++) {
- $Units[$i] = $args[0][$i]
- }
- }
- $result = switch ($this.TotalSeconds) {
- { $_ -le 3 } { "<5 $($Units[0])"; break }
- { $_ -le 7.5 } { "5 $($Units[0])"; break }
- { $_ -le 47.5 } {
- "$([System.Math]::Round((($this.TotalSeconds - 2.5) - (($this.TotalSeconds - 2.5) % 5) + 5), 0)) $($Units[0])"
- break
- }
- { $_ -le 75 } { $Units[1]; break }
- { $_ -le 2700 } {
- Round -Number $this.TotalMinutes -Unit $Units[2]
- break
- } # 45 minutes or less
- { $_ -le 4500 } { $Units[3]; break } # 45 minutes to 1.25 hours
- { $_ -le 173700 } {
- Round -Number $this.TotalHours -Unit $Units[4]
- break
- } # less than 2 days 15 minutes
- default {
- Round -Number $this.TotalDays -Unit $Units[5]
- }
- }
- return $result
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement