#! /usr/bin/python3
import math

def generate_full_time_offset_table(longitude_deg, is_summer_time_auto=True, filename="gesetzliche_zeittabelle.txt"):
    # Offset für den Längengrad: (15° - Standort_Länge) * 4 Minuten pro Grad
    longitude_offset_min = (15.0 - longitude_deg) * 4.0
    
    days_in_months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    month_names = ["Januar", "Februar", "März", "April", "Mai", "Juni", 
                   "Juli", "August", "September", "Oktober", "November", "Dezember"]
    
    day_counter = 1
    
    with open(filename, "w", encoding="utf-8") as f:
        f.write("===========================================================================\n")
        f.write(f" UMRECHNUNGSTABELLE: SONNENUHR (WOZ) -> GESETZLICHE UHRZEIT (MEZ/MESZ)\n")
        f.write(f" Standort-Länge: {longitude_deg}° Ost (Standort-Korrektur: {longitude_offset_min:+.1f} Min)\n")
        f.write("===========================================================================\n\n")
        f.write("Datum       | ZGL-Wert | Sommerzeit? | Gesamt-Offset | Sonnenuhr 12:00 WOZ ist:\n")
        f.write("------------+----------+-------------+---------------+-------------------------\n")
        
        for m_idx, num_days in enumerate(days_in_months):
            for day_of_month in range(1, num_days + 1):
                # ZGL Berechnung
                B = math.radians((360 / 365) * (day_counter - 81))
                eot = 9.87 * math.sin(2 * B) - 7.53 * math.cos(B) - 1.5 * math.sin(B)
                
                # Sommerzeit-Regel (Grob: Ende März bis Ende Oktober)
                is_dst = False
                if is_summer_time_auto:
                    if (m_idx > 2 and m_idx < 9) or (m_idx == 2 and day_of_month >= 25) or (m_idx == 9 and day_of_month < 25):
                        is_dst = True
                
                dst_offset = 60.0 if is_dst else 0.0
                
                # Gesamte Abweichung zur Wahren Ortszeit (WOZ):
                # Gesetzliche Zeit = WOZ - ZGL + Standort_Offset + Sommerzeit
                total_offset_min = round(-eot + longitude_offset_min + dst_offset)
                
                # Formatierung für 12:00 Uhr Beispiel
                base_time_min = 12 * 60 + total_offset_min
                h = int(base_time_min // 60)
                m = int(base_time_min % 60)
                time_str = f"{h:02d}:{m:02d} {'MESZ' if is_dst else 'MEZ '}"
                
                date_str = f"{day_of_month:02d}. {month_names[m_idx]:<9}"
                dst_str = "Ja (MESZ)  " if is_dst else "Nein (MEZ) "
                
                f.write(f"{date_str} | {eot:+5.1f} min | {dst_str} | {total_offset_min:+4d} Minuten | {time_str}\n")
                day_counter += 1
            f.write("------------+----------+-------------+---------------+-------------------------\n")

    print(f"Datei erfolgreich erstellt: {filename}")

# Beispielaufruf für München (11.635° Ost)
generate_full_time_offset_table(longitude_deg=11.635)
