Entwickelt von mir als Bestandteil einer eigenen Astromonmie Bibliothek 1991- 1995 Geprüft mit qwen3 und GPT-OSS-20B. Hier die CGP Antwort! Hardware Nvidia RTX 3060 TI Intel I7-12700K Token ca. 8010 LM Studio im Modus Developer. Es haben sich ein paar Zwischenschritte gezeigt. Version 3.23.0 Arbeitsspeicher: 192 GB Modell GPT-OSS-20B The file implements a small C library that converts between **calendar dates** (Gregorian or Julian) and the **Julian Day Number** (JDN), calculates day‑of‑year, weekday, time differences, and the Julian Ephemeris Date (JDE). -- ## 1. Header guard & includes ```c #ifndef JULIAN_C #define JULIAN_C #include #include #include "julian.h" ``` * `#ifndef … #define …` prevents double inclusion. * Only the standard library is used – no external dependencies. --- ## 2. Internal helper prototypes ```c static int gregorian_kalendar( int y ); static int test_gregorian_julian( int y , int m , double d ); static int test_schaltjahr( int y , int julian_gregorian ); ``` These functions are **file‑local** (`static`). They encapsulate calendar rules (Gregorian vs. Julian, leap‑year tests) so that the public API stays clean. --- ## 3. Core algorithms | Function | Purpose | Key formulae / remarks | |----------|---------|------------------------| | `julian_date` | Converts a calendar date to a JDN | `berechne_kd_aus_jd` | Inverse of `julian_date` – JD → Y M D | Uses the standard reverse algorithm (Meeus §7). Adds 0.5 to align with day boundaries. | | `berechne_tag_d_jahres` | Day number within a year (1‑365/366) | Applies simple month‑based formula; adjusts for leap years (`k = 1` if leap, `2` otherwise). | | `berechne_wochentag` | Weekday index from JD | `(jd + 1.5) % 7`. | | `berechne_zeitabstand` | Difference in days between two dates | Computes JDN for each date and subtracts; checks that the first is not after the second. | | `berechne_jd0` | JDN of January 1 (start of year) | `berechne_jde` | Julian Ephemeris Date | Adds ΔT (seconds) divided by 86 400 to the JD. | --- ## 4. Calendar‑switch helper – `test_gregorian_julian` ```c static int test_gregorian_julian( int y , int m , double d ) { /* Return: 0 = Julian, 1 = Gregorian, 2 = illegal (gap) */ if ( y == 1582 && m == 10 ) if ( d > 4.0 && d < 15.0 ) return 2; // missing days if ( y >= 1582 ) { if ( y == 1582 ) { if ( m == 10 && d >= 15.0 ) return 1; if ( m > 10 ) return 1; if ( m == 10 && d <= 4.0 ) return 0; if ( m < 10 ) return 0; } /* all later years are Gregorian */ return 1; } /* years before 1582 → Julian */ return 0; } ``` * Handles the historical switch‑over in October 1582. * Returns **2** for dates that fall inside the ten‑day gap, which is treated as an error by all callers. --- ## 5. Leap‑year helper – `test_schaltjahr` ```c static int test_schaltjahr( int y , int julian_gregorian ) { /* Return: 0 = leap year, 1 = common year */ if (!julian_gregorian) // Julian calendar return (y % 4); // remainder → not a leap year // Gregorian calendar rules int leap = y % 4; if (!leap) { // divisible by 4 int cent = y % 100; // century check if (!cent) { // divisible by 100 leap = y % 400; // divisible by 400? return (leap == 0) ? 0 : 1; } return 0; // leap year } return 1; // not a leap year } ``` * Note the **inverted logic**: `0` means *leap*, `1` means *not*. * This is intentional because callers test with `if (schaltjahr)` to detect non‑leap years. --- ## 6. Leap‑year correction for Gregorian dates – `gregorian_kalendar` ```c static int gregorian_kalendar( int y ) { int a = y / 100; return 2 - a + (a / 4); } ``` * Computes the number of leap‑year adjustments needed when converting a Gregorian year to JD. --- ## 7. Practical usage snippets ```c /* Convert 15 Oct 1582 to JD */ double jd; int rc = julian_date(&jd, 1582, 10, 15.0); // rc == 0 printf("JD: %.5f\n", jd); /* Day of year for 29 Feb 2000 (leap year) */ int day_of_year = berechne_tag_d_jahres(2000, 2, 29.0); printf("Day %d\n", day_of_year); // 60 /* Julian Ephemeris Date for 1 Jan 2023 with ΔT = 67 s */ double jde; berechne_jde(&jde, 2023, 1, 1.0, 67.0); printf("JDE: %.6f\n", jde); ``` --- ## 8. Error handling | Function | Possible return values | |----------|------------------------| | `julian_date`, `berechne_jde` | `0` = success, `1` = Feb 29 on a non‑leap year, `2` = date in the 4–15 Oct 1582 gap. | | `berechne_kd_aus_jd` | `0` = success, `1` = internal error (invalid JD). | | `berechne_tag_d_jahres`, `berechne_zeitabstand` | `0` = success, non‑zero indicates an error (e.g., wrong date order). | All callers check the return code before using the output. --- ## 9. Summary * **Julian.c** is a self‑contained implementation of standard astronomical date conversions. * It correctly handles the Julian ↔ Gregorian transition, leap‑year rules for both calendars, and provides helper routines for common tasks (weekday, day‑of‑year, time difference, ephemeris dates). * You can use it as a drop‑in library in any C or C++ project that needs reliable calendar math.