modules/Helper/DateTimeHelper.psm1
using module '.\DateTimeFormula.psm1' class DateTimeHelper{ hidden static [datetime[]] initAfterBefore(){ return @((Get-Date).AddMonths(-3), (Get-Date)) } static [datetime[]] getAfterBefore([string]$referentdate, [string]$timeSpan){ if ($referentDate -and -not $timespan){ $dtp = [DateTimeFormula]::new($referentDate) $date = $dtp.datetime $date2 = Get-Date } elseif (-not $referentDate -and $timespan){ $date = Get-Date $dtp = [DateTimeFormula]::new($date) $date2 = $dtp.applyTimeSpanFormula($timespan) } elseif ($referentDate -and $timespan){ $dtp = [DateTimeFormula]::new($referentDate) $date = $dtp.datetime $date2 = $dtp.applyTimeSpanFormula($timespan) } else { return [DateTimeHelper]::initAfterBefore() } if ($date -gt $date2) { return $date2, $date } else { return $date, $date2 } } static [datetime] getStringToLocalDate([string]$date, [string]$timeZone='') { if ($date -eq '') { return [datetime]::MinValue } $date = $date.Replace('Z', '') if ($timeZone -ne ''){ $_retValue = [datetime]::parse([string]$date + ' ' + $timeZone) } else { $_retValue = [datetime]::parse([string]$date) } return $_retValue } static [bool] IsBetween([datetime] $date, [datetime] $fromDateTime, [datetime] $toDateTime){ return (($date -ge $fromDateTime) -and ($date -le $toDateTime)) } static [string] FormatMilliseconds([long]$timeMilliseconds){ if ($timeMilliseconds -ge (10 * 60 * 1000)) { $_retValue = "{0:N0} min" -f ($timeMilliseconds / (60 * 1000)) } elseIf ($timeMilliseconds -gt (60 * 1000)) { $_retValue = "{0:N1} min" -f ($timeMilliseconds / (60 * 1000)) } elseIf ($timeMilliseconds -ge 1000) { $_retValue = "{0:N1} sec" -f ($timeMilliseconds / 1000) } else { $_retValue = "{0:N0} ms" -f $timeMilliseconds } return $_retValue } static [string] FormatTimespan([timespan]$timespan){ if ($timespan.TotalSeconds -lt 60) { $value = $timespan.TotalSeconds; $unit = 'seconds' } elseif ($timespan.TotalHours -lt 24) { $value = $timespan.TotalHours; $unit = 'hours' } else { $value = $timespan.TotalDays; $unit = 'days' } return ("{0:N2} {1}" -f @($value, $unit)) } static [long] getTimestampDiff([long]$timestamp, [long]$timestamp2){ $_diff = $timestamp - $timestamp2 return $_diff } static [string] ConvertFromTimestamp([string] $timestamp, [bool] $toLocalTime = $true){ if ($timestamp -eq '') { return ''} $_time = (Get-Date "1970-01-01 00:00").AddMilliSeconds($timestamp) if ($toLocalTime) { $_time = $_time.ToLocalTime() } return $_time } static [long] ConvertToTimestamp([datetime]$date){ $date2 = Get-Date -Date "1970-01-01 00:00" return (New-TimeSpan -Start $date2 -End $date).TotalMilliseconds } } function ConvertLogDate([string] $date, [string] $timezone){ if ($date -eq '') { return [datetime]::MinValue } else { return [datetime]($date + ' ' + $timezone) } } |