# Calculate local sunset time and set Windows theme to "dark" if it's past sunset time or "light" if it's before sunset time # Author: Armen Michaeli # 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) $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) 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)