Last active
September 4, 2024 09:46
-
-
Save amn/487d02fab0b28e497a1ef4082b1300aa to your computer and use it in GitHub Desktop.
Calculate local sunset time and switch Windows theme accordingly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calculate local sunset time and set Windows theme to "dark" if it's past sunset time or "light" if it's before sunset time | |
# May be run on a schedule (e.g. every 15 minutes) or manually. | |
Add-Type -AssemblyName System.Device | |
$gcw = New-Object System.Device.Location.GeoCoordinateWatcher | |
$gcw.Start() | |
$date = Get-Date | |
$DaysInYear = if ($date.Year % 4) { 366 } else { 365 } | |
$timezone = Get-TimeZone | |
# Solar math largely derived from http://gml.noaa.gov/grad/solcalc/solareqns.PDF et al | |
$gamma = (2 * [Math]::PI / $DaysInYear) * ($date.DayOfYear - 1 + ($date.Hour - 12) / 24) | |
function DegToRad($deg) { | |
return $deg * [Math]::PI / 180; | |
} | |
function RadToDeg($rad) { | |
return $rad * 180 / [Math]::PI; | |
} | |
$eqtime = 229.18 * (0.000075 + 0.001868 * [Math]::Cos($gamma) - 0.032077 * [Math]::Sin($gamma) - 0.014615 * [Math]::Cos(2 * $gamma) - 0.040849 * [Math]::Sin(2 * $gamma)) | |
$decl = 0.006918 - 0.399912 * [Math]::Cos($gamma) + 0.070257 * [Math]::Sin($gamma) - 0.006758 * [Math]::Cos(2 * $gamma) + 0.000907 * [Math]::Sin(2 * $gamma) - 0.002697 * [Math]::Cos(3 * $gamma) + 0.00148 * [Math]::Sin(3 * $gamma) | |
While($gcw.Status -ne "Ready") { | |
Start-Sleep 1 | |
} | |
$latitude = DegToRad($gcw.Position.Location.Latitude) | |
$longitude = DegToRad($gcw.Position.Location.Longitude) | |
# $time_offset = $eqtime + 4 * (RadToDeg($longitude)) - 60 * $timezone.BaseUTCOffset.Hours | |
# $tst = $date.Hour * 60 + $date.Minute + $date.Second / 60 + $time_offset | |
# $ha = ($tst / 4) - 180 | |
# $cos_phi = [Math]::Sin($latitude) * [Math]::Sin($decl) + [Math]::Cos($latitude) * [Math]::Cos($decl) * [Math]::Cos($ha) | |
$zenith = DegToRad(90.833) | |
$ha = [Math]::Acos([Math]::Cos($zenith) / ([Math]::Cos($latitude) * [Math]::Cos($decl)) - [Math]::Tan($latitude) * [Math]::Tan($decl)) | |
$sunrise = (Get-Date "00:00Z").AddMinutes(720 - (4 * (RadToDeg($longitude + $ha))) - $eqtime) | |
$sunset = (Get-Date "00:00Z").AddMinutes(720 - (4 * (RadToDeg($longitude - $ha))) - $eqtime) | |
# $solar_noon = (Get-Date "00:00Z").AddMinutes(720 - (4 * (RadToDeg($longitude))) - $eqtime) | |
function ResetTheme($IsLightTheme) { | |
@("SystemUsesLightTheme", "AppsUseLightTheme") | % { Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize -Name $_ $IsLightTheme } | |
} | |
ResetTheme($date.Hour -ge $sunrise.Hour -and $date.Hour -lt $sunset.Hour) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment