Advertisement
Old-Lost

ApproximateTime Class

May 31st, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ApproximateTime {
  2.     [timespan]$Value
  3.     [string]$Prefix
  4.     [string]$Postfix
  5.     [string[]]$Units
  6.     static [string]Round([double]$Number, [string]$Unit) {
  7.         $Number, $Half = if ((($Number % 1) -gt 0.25) -and (($Number % 1) -lt 0.75)) { ($Number - 0.5), '½' } else { $Number, '' }
  8.         return ('{0}{1} {2}' -f [System.Math]::Round($Number, 0), $Half, $Unit)
  9.     }
  10.     ApproximateTime() {
  11.         $this.Init((New-TimeSpan -Seconds 0), $null, $null, $null)
  12.     }
  13.     ApproximateTime([timespan]$Value) {
  14.         $this.Init($Value, $null, $null, $null)
  15.     }
  16.     ApproximateTime([timespan]$Value, [string]$Prefix) {
  17.         $this.Init($Value, $Prefix, $null, $null)
  18.     }
  19.     ApproximateTime([timespan]$Value, [string]$Prefix, [string]$Postfix) {
  20.         $this.Init($Value, $Prefix, $Postfix, $null)
  21.     }
  22.     ApproximateTime([timespan]$Value, [string]$Prefix, [string]$Postfix, $Units) {
  23.         $this.Init($Value, $Prefix, $Postfix, $Units)
  24.     }
  25.     [string]ToString() {
  26.         [string]$result = switch ($this.Value.TotalSeconds) {
  27.             { $_ -le 3 }      { "<5 $($this.Units[0])"; break }
  28.             { $_ -le 7.5 }      { "5 $($this.Units[0])"; break }
  29.             { $_ -le 47.5 }     {
  30.                 "$([System.Math]::Round((($this.Value.TotalSeconds - 2.5) - (($this.Value.TotalSeconds - 2.5) % 5) + 5), 0)) $($this.Units[0])"
  31.                 break
  32.             }
  33.             { $_ -le 75 }    { $this.Units[1]; break }
  34.             { $_ -le 2700 }   {
  35.                 [ApproximateTime]::Round($this.Value.TotalMinutes, $this.Units[2])
  36.                 break
  37.             } # 45 minutes or less
  38.             { $_ -le 4500 }   { $this.Units[3]; break } # 45 minutes to 1.25 hours
  39.             { $_ -le 173700 }  {
  40.                 [ApproximateTime]::Round($this.Value.TotalHours, $this.Units[4])
  41.                 break
  42.             } # less than 2 days 15 minutes
  43.             default {
  44.                 [ApproximateTime]::Round($this.Value.TotalDays, $this.Units[5])
  45.             }
  46.         }
  47.         return ("$($this.Prefix) $result $($this.Postfix) ".Trim())
  48.     } # ApproximateTime
  49.     hidden Init ([timespan]$Value, [string]$Prefix, [string]$Postfix, [string[]]$Units) {
  50.         $this.Value = $Value
  51.         $this.Prefix = $Prefix
  52.         $this.Postfix = $Postfix
  53.         $this.Units = @('seconds', 'a minute', 'minutes', 'an hour', 'hours', 'days')
  54.         if ($Units -is [array]) {
  55.             for ($i = 0; ($i -lt $this.Units.Count) -and ($i -lt $Units.Count); $i++) {
  56.                 $this.Units[$i] = $Units[$i]
  57.             }
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement