Created
April 17, 2023 05:34
-
-
Save remi6397/3d0781bbea5fccf70e3b63908460dd48 to your computer and use it in GitHub Desktop.
Is it light, or is it dark outside? / Set theme based on sunrise/sunset times.
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
""" | |
Outputs "dark" when it's dark and "light" when it's light. | |
Only works on Freedesktop platforms (Linux, *BSD, etc.). | |
NOTE: This code was stolen from the depths of hell. I give absolutely no warranty for this. I also hate this. | |
""" | |
import math | |
import datetime | |
import time | |
import gi | |
gi.require_version("Geoclue", "2.0") | |
from gi.repository import Geoclue | |
RAD = math.pi/180.0 | |
h = -(50.0/60.0)*RAD | |
def get_user_location(): | |
clue = Geoclue.Simple.new_sync("", Geoclue.AccuracyLevel.CITY, None) | |
location = clue.get_location() | |
return location.get_property("latitude"), location.get_property("longitude") | |
def sonnendeklination(T): | |
return 0.40954*math.sin(0.0172*(T-79.35)) | |
def zeitdifferenz(deklination): | |
global B | |
return 12.0*math.acos((math.sin(h) - math.sin(B)*math.sin(deklination)) / (math.cos(B)*math.cos(deklination)))/math.pi | |
def zeitgleichung(T): | |
return -0.1752*math.sin(0.033430 * T + 0.5474) - 0.1340*math.sin(0.018234*T - 0.1939) | |
def aufgang1(T): | |
DK = sonnendeklination(T) | |
return 12 - zeitdifferenz(DK) - zeitgleichung(T) | |
def untergang1(T): | |
DK = sonnendeklination(T) | |
return 12 + zeitdifferenz(DK) - zeitgleichung(T) | |
def get_sun(lat, lon): | |
global B | |
Laenge = lon | |
Breite = lat | |
now = datetime.datetime.now() | |
Zone = (now - datetime.datetime.utcnow()).seconds / 60**2 | |
T = now.timetuple().tm_yday | |
B = Breite*RAD | |
Aufgang = aufgang1(T) | |
Untergang = untergang1(T) | |
Aufgang = Aufgang - Laenge/15.0 + Zone | |
Untergang = Untergang - Laenge/15.0 + Zone | |
Aufgangh = math.floor(Aufgang) | |
AufgangH = "0" + str(Aufgangh) if Aufgangh < 10 else str(Aufgangh) | |
Aufgangm = math.floor(((Aufgang-Aufgangh)*100)*3/5) | |
AufgangM = "0" + str(Aufgangm) if Aufgangm < 10 else str(Aufgangm) | |
Aufgangs = math.floor(((((((Aufgang-Aufgangh)*100)*3/5)-Aufgangm)*100)*3/5)) | |
AufgangS = "0" + str(Aufgangs) if Aufgangs < 10 else str(Aufgangs) | |
Aufgang = datetime.time.fromisoformat(AufgangH + ":" + AufgangM + ":" + AufgangS) | |
Untergangh = math.floor(Untergang) | |
UntergangH = "0" + str(Untergangh) if Untergangh < 10 else str(Untergangh) | |
Untergangm = math.floor(((Untergang-Untergangh)*100)*3/5) | |
UntergangM = "0" + str(Untergangm) if Untergangm < 10 else str(Untergangm) | |
Untergangs = math.floor(((((((Untergang-Untergangh)*100)*3/5)-Untergangm)*100)*3/5)) | |
UntergangS = "0" + str(Untergangs) if Untergangs < 10 else str(Untergangs) | |
Untergang = datetime.time.fromisoformat(UntergangH + ":" + UntergangM + ":" + UntergangS) | |
return Aufgang, Untergang | |
def main(): | |
lat, lon = get_user_location() | |
now = datetime.datetime.now().time() | |
sunrise, sunset = get_sun(lat, lon) | |
print("dark" if now > sunset or now < sunrise else "light") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment