Private/Get-LocalDaylight.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Function Get-LocalDaylight {
    <#
        .SYNOPSIS
            Returns the current sunrise and sunset times for the local user in localtime.
 
        .EXAMPLE
            Get-LocalDaylight
 
            Result
            -----------
            Sunrise : 06/08/2019 06:04:57
            Sunset : 06/08/2019 20:22:17
             
    #>
      
    [cmdletbinding()]
    Param(
        [Parameter(Mandatory)]
        [double]
        $Latitude,

        [Parameter(Mandatory)]
        [double]
        $Longitude
    )

    # Return sunrise/sunset
    $Daylight = (Invoke-RestMethod "https://api.sunrise-sunset.org/json?lat=$Latitude&lng=$Longitude").results

    # Convert to local time datetime objects
    [pscustomobject]@{
        Sunrise = ($Daylight.Sunrise | Get-Date).ToLocalTime()
        Sunset  = ($Daylight.Sunset | Get-Date).ToLocalTime()
    }
}