public/Utils/DateTimeFunctions.ps1

using module '..\..\modules\Helper\DateTimeFormula.psm1'

function Get-Now(){
    [Alias('now')]
    [OutputType([datetime])]
    param([string] $expression, [double]$year=0, [double]$month=0, [double]$day=0, [double]$hour=0, [double]$minute=0, [double]$second=0)

    $date = Get-Date
    if ($expression){
        $dtp = [DateTimeFormula]::new($date)
        $date = $dtp.applyTimeSpanFormula($expression)
    }
    else {
        if ($year) { $date = $date.AddYears($year) }
        if ($month) { $date = $date.AddMonths($month) }
        if ($day) { $date = $date.AddDays($day) }
        if ($hour) { $date = $date.AddHours($hour) }
        if ($minute) { $date = $date.AddMinutes($minute) }
        if ($second) { $date = $date.AddSeconds($second) }
    }

    return $date
}

function Get-Today() {
    [Alias('today')]
    [OutputType([datetime])]
    param([string] $expression, [double]$year=0, [double]$month=0, [double]$day=0, [double]$hour=0, [double]$minute=0, [double]$second=0)

    $now = Get-Date
    $date = Get-Date -Year $now.Year -Month $now.Month -Day $now.Day -Hour 0 -Minute 0 -Second 0 -Millisecond 0
    if ($expression){
        $dtp = [DateTimeFormula]::new($date)
        $date = $dtp.applyTimeSpanFormula($expression)
    }
    else {
        if ($year) { $date = $date.AddYears($year) }
        if ($month) { $date = $date.AddMonths($month) }
        if ($day) { $date = $date.AddDays($day) }
        if ($hour) { $date = $date.AddHours($hour) }
        if ($minute) { $date = $date.AddMinutes($minute) }
        if ($second) { $date = $date.AddSeconds($second) }
    }

    return $date
}

function New-TimeSpanBySeconds() {
    [Alias('seconds')]
    [OutputType([TimeSpan])]
    param([double]$seconds)

    return ([timespan]::FromSeconds($seconds))
}

function New-TimeSpanByMinutes() {
    [Alias('minutes')]
    [OutputType([TimeSpan])]
    param([double]$minutes)

    return ([timespan]::FromMinutes($minutes))
}

function New-TimeSpanByHours() {
    [Alias('hours')]
    [OutputType([TimeSpan])]
    param([double]$hours)

    return ([timespan]::FromHours($hours))
}

function New-TimeSpanByDays() {
    [Alias('days')]
    [OutputType([TimeSpan])]
    param([double]$days)

    return ([timespan]::FromDays($days))
}

function Test-DateFormulaRange {
    param (
        [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)
        try {
            $date2 = $dtp.datetime + [timespan]$timespan
        }
        catch {
            $date2 = $dtp.applyTimeSpanFormula($timespan)
        }
    }
    elseif ($referentDate -and $timespan){
        $dtp = [DateTimeFormula]::new($referentDate)

        $date =  $dtp.datetime
        try {
            $date2 = $dtp.datetime + [timespan]$timespan
        }
        catch {
            $date2 = $dtp.applyTimeSpanFormula($timespan)
        }
    }
    else {
        return [DateTimeHelper]::initAfterBefore()
    }

    if ($date -gt $date2) {
        return @{'after'=$date2; 'before'=$date}
    }
    else {
        return @{'after'=$date; 'before'=$date2}
    }

}

function Test-DateFormula {
    param (
        [string] $referentDate
    )

    return [DateTimeFormula]::new($referentDate)
}