public/Utils/DateTimeFunctions.ps1

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

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

    $date = Get-Date
    if ($expression){
        $dtf = [DateTimeFormula]::new()

        $date = $dtf.applyTimeSpanFormula($date, $expression)
        if (-not $dtf.isTimeSpanFormula){
            Write-Error 'Unrecognized timespan expression'
            return
        }
    }
    else {
        if ($year) { $date = $date.AddYears($year) }
        if ($month) { $date = $date.AddMonths($month) }
        if ($week) { $date = $date.AddDays(7 * $week) }
        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]$week=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){
        $dtf = [DateTimeFormula]::new()

        $date = $dtf.applyTimeSpanFormula($date, $expression)
        if (-not $dtf.isTimeSpanFormula){
            Write-Error 'Unrecognized timespan expression'
            return
        }
    }
    else {
        if ($year) { $date = $date.AddYears($year) }
        if ($month) { $date = $date.AddMonths($month) }
        if ($week) { $date = $date.AddDays(7 * $week) }
        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 Get-DateRangeByFormula {
    [OutputType([datetime[]])]
    param (
        [string] $referentDate,
        [string] $timespan
    )

    return [DateTimeHelper]::getAfterBefore($referentDate, $timeSpan)
}

function Get-DateTimeByFormula {
    [OutputType([datetime])]
    param (
        [string] $formula
    )

    $dtf = [DateTimeFormula]::new()
    return ($dtf.getDateTimeValue($formula))
}

function Get-TimeSpanByFormula {
    [OutputType([timespan])]
    param (
        [string] $formula
    )

    $dtf = [DateTimeFormula]::new()
    return $dtf.getTimeSpanValue($formula)
}