mit 131000 + ein paar Tokens.. das war etwas zuviel... Geschwindigkeit, sehr langsam. Ca. 10 min für diesen Text, aber mit Vorschlag einer MFC Win32/64 Anwendung! Hardware I7-12700KF GPU RTX 3060 TI, 8GB RAM RAM 192 GB (PC) Modell GPT-OSS-20B Below is a line‑by‑line “walk‑through” of the key parts, followed by an overall picture of how the pieces fit together and some practical tips for using it in your own code. --- ## 1. Header guard & includes ```c #ifndef JULIAN_C #define JULIAN_C #include #include #include "julian.h" ``` * `#ifndef … #define … #endif` protects against multiple inclusion. * The implementation relies only on the C standard library (`floor`, integer division). * `"julian.h"` (not shown) contains the public prototypes; all other functions are *static*, i.e. internal to this file. --- ## 2. Static helper declarations ```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 ); ``` * `gregorian_kalendar` → number of leap‑year corrections for a Gregorian year. * `test_gregorian_julian` → decides whether the supplied date belongs to the Julian or Gregorian calendar and flags the illegal 10‑day gap (4 Oct 1582 – 15 Oct 1582). * `test_schaltjahr` → returns **0** for a leap year, **1** otherwise. The rule is different for Julian vs. Gregorian calendars. --- ## 3. Core routines | Function | What it does | Key algorithmic idea | |----------|--------------|----------------------| | `julian_date(double *jd, int y, int m, double d)` | Converts a calendar date to a Julian Day Number (JDN). | `berechne_kd_aus_jd(double jd, int *y, int *m, double *d)` | Inverse of the above – converts a JD back into Y‑M‑D. | `berechne_tag_d_jahres(int y, int m, double d)` | Day number within the year (1 – 365/366). | Simple arithmetic with an adjustment for leap years (`k` variable). | | `berechne_wochentag(double jd)` | Returns weekday index (0‑6) for a given JD. | Uses `(jd + 1.5) % 7`. | | `berechne_zeitabstand(...)` | Difference in days between two calendar dates. | Computes JDNs for both dates and subtracts. | | `berechne_jd0(double *jd0, int y, int m, double d)` | JDN of the start of a year (i.e., 1 Jan). | `berechne_jde(...)` | Julian Ephemeris Date = JD + ΔT/86400. | Adds the time‑correction ΔT in days. | --- ## 4. Helper functions – how they work ### 4.1 `test_gregorian_julian` ```c static int test_gregorian_julian( int y , int m , double d ) { /* 0 = Julian, 1 = Gregorian, 2 = illegal (gap) */ if (y == 1582 && m == 10) if (d > 4.0 && d < 15.0) return 2; // gap if (y >= 1582) { /* 1582 itself */ if (y == 1582) { if (m == 10 && d >= 15.0) return 1; // Gregorian if (m > 10) return 1; if (m == 10 && d <= 4.0) return 0; // Julian if (m < 10) return 0; } /* all later years are Gregorian */ return 1; } /* years before 1582 → Julian */ return 0; } ``` * The “gap” is the historical switch‑over period when Europe skipped 10 days. * After 15 Oct 1582 the calendar is Gregorian; before that it’s Julian. ### 4.2 `test_schaltjahr` ```c static int test_schaltjahr( int y , int julian_gregorian ) { if (!julian_gregorian) /* Julian */ return (y % 4); /* 0 → leap, else common */ /* Gregorian rules */ int leap = y % 4; if (!leap) { /* divisible by 4 */ int cent = y % 100; /* century? */ if (!cent) { /* divisible by 100 */ int quad = y % 400; /* divisible by 400? */ return (quad == 0) ? 0 : 1; } return 0; /* leap, not a centurial year */ } return 1; /* common year */ } ``` * Returns **0** for a leap year, **1** otherwise. * Note the inverted logic: “no remainder → leap”, hence the final `return 0` or `return 1`. ### 4.3 `gregorian_kalendar` ```c static int gregorian_kalendar( int y ) { int a = y / 100; return 2 - a + (a / 4); } ``` * Computes the number of leap‑year corrections that must be added to the Julian Day Number when using the Gregorian calendar. --- ## 5. Putting it all together ### Example: Convert 1 Mar 2023 to JD and back ```c double jd; int y, m; double d; julian_date(&jd, 2023, 3, 1.0); // 2459944.5 berechne_kd_aus_jd(jd, &y, &m, &d); // y=2023, m=3, d=1.0 ``` ### Example: Calculate the day of the week for 15 Oct 1582 ```c int weekday = berechne_wochentag(2299160.5); // 0=Monday … 6=Sunday ``` ### Example: Time difference between two dates ```c double diff; berechne_zeitabstand(2021,12,31.0, 2022,1,1.0, &diff); // 1.0 day ``` --- ## 6. Things to watch out for, (diese Probleme wurden von mir dann in den "Front-Ends", damals MS DOS 6.22, beachtet. Entwickelt mit Zortech C++ 3.0, aber in purem C geschrieben | Issue | Why it matters | How to handle | |-------|----------------|---------------| | **Floating‑point rounding** | JDN is often a half‑day offset (0.5). Small errors can change the weekday or day‑of‑year calculation. | Use `floor()`/`ceil()` consistently as in the source; avoid casting mid‑formula. | | **ΔT accuracy** | The ΔT value used in `berechne_jde` must come from a reliable ephemeris table (e.g., NASA’s DE‑430). | Pass ΔT in seconds, not days. | | **Year 0 / astronomical year** | The library assumes the proleptic Gregorian calendar; there is no year 0 in the civil calendar. | If you need “astronomical” year numbering (including year 0), adjust the input accordingly before calling `julian_date`. | | **Error codes** | Functions return 1 or 2 for specific problems (Feb 29 on a non‑leap year, illegal date). | Check the return value; propagate errors to the caller. | --- ## 7. How to integrate into an MFC/Win32 project * Add `julian.c` and its header (`julian.h`) to your Visual Studio project. * Include `"julian.h"` where you need the routines. * Because all functions are **externally visible** (no `static` on them), they can be called from any translation unit. ```c #include "julian.h" void ShowJDForToday() { time_t t = time(NULL); struct tm *tm = localtime(&t); double jd; julian_date(&jd, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday + (tm->tm_hour/24.0) + (tm->tm_min/1440.0) + (tm->tm_sec/86400.0)); CString msg; msg.Format(_T("Today JD = %.5f"), jd); AfxMessageBox(msg); } ``` --- ## 8. Summary * **julian.c** implements a compact, self‑contained set of algorithms for Julian date calculations. * The public API is small: convert to/from JD, compute weekdays, day‑of‑year, time differences, and Julian Ephemeris Dates. * All internal logic correctly handles the Julian/Gregorian switch, leap‑year rules, and the 10‑day gap in October 1582. Feel free to drop this file into any C/C++ project that needs reliable calendar conversions—no external dependencies beyond the standard library!