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){
            $dtf = [DateTimeFormula]::new()
            $date = $dtf.getDateTimeValue($referentDate)

            if ($dtf.IsDateTimeFormula) {
                $date = $date
            }
            else {
                $date = [datetime]::parse($referentDate)
            }

            $date2 = Get-Date
        }
        elseif (-not $referentDate -and $timespan){
            $date = Get-Date
            $dtf = [DateTimeFormula]::new()

            $date2 = $dtf.applyTimeSpanFormula($date, $timespan)
            if (-not $dtf.IsTimespanFormula) {
                $date2 = $date + [timespan]$timespan
            }
        }
        elseif ($referentDate -and $timespan){
            $dtf = [DateTimeFormula]::new()
            $date = $dtf.getDateTimeValue($referentDate)

            if ($dtf.isDateTimeFormula){
                $date =  $date
            }
            else {
                $date = [datetime]::parse($referentDate)
            }

            $date2 = $dtf.applyTimeSpanFormula($date, $timespan)
            if (-not $dtf.IsTimespanFormula) {
                $date2 = $date + [timespan]$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)
    }
}