From 6fffa10c2b8d593e17ed314861ccf04032cc54b9 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Tue, 10 Nov 2026 21:53:30 -0700 Subject: [PATCH 1/2] Parse ISO dates strictly: fixed-width digits, plain decimal seconds parseIsoDate read each field with sscanf %d / %lf, so it took "2026-1-5", signed fields, a space before the hour, and seconds written "1e1" or "0x10" -- none of which its header documents. Each field is now read as exactly its width in ASCII digits, and seconds as two digits with an optional decimal fraction. Text after an embedded NUL is rejected too. Closes #51 Co-Authored-By: Claude Opus 5.5 --- src/orrery/time.cpp | 65 ++++++++++++++++++++++++++++++++++++--------- src/orrery/time.hpp | 4 ++- tests/test_time.cpp | 13 +++++++++ 3 files changed, 68 insertions(+), 14 deletions(-) diff --git a/src/orrery/time.cpp b/src/orrery/time.cpp index 6564fd6..fa2b063 100644 --- a/src/orrery/time.cpp +++ b/src/orrery/time.cpp @@ -2,6 +2,7 @@ #include #include +#include #include namespace orrery { @@ -75,30 +76,68 @@ double tdbToUtc(double jdTdb) { return utc; } +namespace { + +bool isDigit(char ch) { return ch >= '0' && ch <= '9'; } + +// Reads exactly `width` ASCII digits at s and advances past them. No sign, no +// whitespace and no shorter field: "2026-1-5" and "T 6:30" are not dates here. +bool readDigits(const char*& s, int width, int& value) { + int v = 0; + for (int i = 0; i < width; ++i) { + if (!isDigit(s[i])) return false; + v = v * 10 + (s[i] - '0'); + } + s += width; + value = v; + return true; +} + +bool expect(const char*& s, char ch) { + if (*s != ch) return false; + ++s; + return true; +} + +} // namespace + bool parseIsoDate(const std::string& text, CalendarDate& out) { CalendarDate c; c.hour = c.minute = 0; c.second = 0; - char sep = 0; - int consumed = 0; const char* s = text.c_str(); - if (std::sscanf(s, "%d-%d-%d%n", &c.year, &c.month, &c.day, &consumed) != 3) return false; - s += consumed; + if (!readDigits(s, 4, c.year) || !expect(s, '-') || !readDigits(s, 2, c.month) || !expect(s, '-') || + !readDigits(s, 2, c.day)) + return false; if (*s == 'T' || *s == ' ') { - sep = *s++; - int n = 0; - if (std::sscanf(s, "%d:%d%n", &c.hour, &c.minute, &n) != 2) return false; - s += n; + ++s; + if (!readDigits(s, 2, c.hour) || !expect(s, ':') || !readDigits(s, 2, c.minute)) return false; if (*s == ':') { ++s; - if (std::sscanf(s, "%lf%n", &c.second, &n) != 1) return false; - s += n; + // Seconds are two digits with an optional decimal fraction: no exponent, + // no hex float, no "nan" — only what the header documents. + int whole = 0; + if (!readDigits(s, 2, whole)) return false; + c.second = whole; + if (*s == '.') { + ++s; + if (!isDigit(*s)) return false; + // Up to 15 digits are kept exactly and divided once, so "15.3" reads + // as the double nearest 15.3; further digits are below a femtosecond. + double numerator = 0.0, denominator = 1.0; + for (int kept = 0; isDigit(*s); ++s) { + if (kept++ < 15) { + numerator = numerator * 10.0 + (*s - '0'); + denominator *= 10.0; + } + } + c.second += numerator / denominator; + } } } - (void)sep; if (*s == 'Z') ++s; - if (*s != '\0') return false; - // %lf reads "nan", so the seconds' test is one a NaN fails. + // An embedded NUL ends c_str() early, so compare the length too. + if (*s != '\0' || static_cast(s - text.c_str()) != text.size()) return false; if (c.month < 1 || c.month > 12 || c.day < 1 || c.day > 31 || c.hour < 0 || c.hour > 23 || c.minute < 0 || c.minute > 59 || !(c.second >= 0 && c.second < 61)) return false; diff --git a/src/orrery/time.hpp b/src/orrery/time.hpp index f8f891c..38d8331 100644 --- a/src/orrery/time.hpp +++ b/src/orrery/time.hpp @@ -30,7 +30,9 @@ double utcToTdb(double jdUtc); double tdbToUtc(double jdTdb); // "2026-10-18", "2026-10-18T06:30", "2026-10-18T06:30:15" or with a space; -// a trailing 'Z' is allowed. Returns false on anything else. +// seconds may carry a decimal fraction ("06:30:15.5") and a trailing 'Z' is +// allowed. Every field has its fixed width in ASCII digits (no sign, no spaces, +// no exponent). Returns false on anything else, and leaves `out` unchanged. bool parseIsoDate(const std::string& text, CalendarDate& out); std::string formatIsoDate(const CalendarDate& c, bool withSeconds = true); diff --git a/tests/test_time.cpp b/tests/test_time.cpp index fac92c1..d96a6cb 100644 --- a/tests/test_time.cpp +++ b/tests/test_time.cpp @@ -106,6 +106,19 @@ int main() { CHECK_MSG(!parseIsoDate(bad, c), bad); } + SECTION("ISO date parsing: every field has its fixed width in digits"); + for (const char* bad : {"2026-1-5", "2026-10-8", "226-10-18", "02026-10-18", "2026-+1-18", "2026--1-18", + "+2026-10-18", " 2026-10-18", "2026-10-18T 6:30", "2026-10-18T6:30", "2026-10-18T06:3", + "2026-10-18T06:30:1", "2026-10-18T06:30:1e1", "2026-10-18T06:30:0x10", + "2026-10-18T06:30:15.", "2026-10-18T06:30:15.5e1", "2026-10-18T06:30:+5", + "2026-10-18T06:30:15 ", "2026-10-18T06:30:15.5Z "}) { + CHECK_MSG(!parseIsoDate(bad, c), bad); + } + CHECK(!parseIsoDate(std::string("2026-10-18\0junk", 15), c)); // text past an embedded NUL is not ignored + CHECK(parseIsoDate("2026-10-18T06:30:15.25Z", c) && c.second == 15.25); + CHECK(parseIsoDate("2026-10-18T06:30:15.3", c) && c.second == 15.3); + CHECK(parseIsoDate("2026-10-18T06:30:59.999", c) && c.second == 59.999); + SECTION("ISO date formatting"); CHECK(formatIsoDate({2026, 10, 18, 6, 5, 4.9}) == "2026-10-18 06:05:04"); CHECK(formatIsoDate({2026, 10, 18, 6, 5, 4.9}, false) == "2026-10-18 06:05"); From 8b31af956c9b7da4a21a391f92a24d87947d5572 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Tue, 10 Nov 2026 21:58:47 -0700 Subject: [PATCH 2/2] README: test_time also covers fixed-width and plain-decimal date fields Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9cdb08e..fb1a2c4 100644 --- a/README.md +++ b/README.md @@ -312,7 +312,7 @@ The other tests: - `test_ephemeris` also checks the catalogue's queries on the fetched data: finding a body by any case or by part of its name, the date range a day inside DE440s's coverage with the system evaluating at both ends, osculating orbits (none for the Sun or a spacecraft; Earth's and the Moon's from their DE states; a table body's from its table), poles (unit vectors, none for spacecraft, comets or Eris; Earth's 23.44° from ecliptic north at J2000) and the axial tilt the HUD prints (Earth 23.44°, and Venus 177.36° and Uranus 97.77° from their IAU poles reversed, since they turn backwards; none for Eris or a spacecraft), and body frames whose +Z is the pole and whose prime meridian comes back after one sidereal day. - `test_kepler` covers Kepler's equation (elliptic and hyperbolic) to 1e-12, circular and eccentric orbits (by mean and by eccentric anomaly), the state ↔ elements round trip, and table interpolation that is exact against a synthetic Phobos-like orbit. - `test_element_table` covers the moon and dwarf-planet element tables: loading a catalogue CSV by column name and sorting it by epoch, the error for a table with no rows, linear a, e and i between samples, node and argument of periapsis taking the shorter arc across 0/360, the mean longitude unwrapped by the whole turns the first sample predicts, and two-body propagation (flagged as extrapolated) before the first sample, after the last and around a single sample. -- `test_time` covers Julian Dates (including the Gregorian start and the proleptic days before it), calendar dates rounding to the millisecond, every leap-second step on its date and with its value, UTC to TDB and back on both sides of each step, ISO parsing (the accepted forms, and rejecting impossible dates, trailing text and seconds that are not a finite number), date formatting and the system clock. +- `test_time` covers Julian Dates (including the Gregorian start and the proleptic days before it), calendar dates rounding to the millisecond, every leap-second step on its date and with its value, UTC to TDB and back on both sides of each step, ISO parsing (the accepted forms, and rejecting impossible dates, trailing text, fields not written as their fixed width in digits, and seconds that are not a plain decimal number), date formatting and the system clock. - `test_scale` checks that TRUE mode leaves positions untouched and multiplies radii by exactly the stated factor, and that COMPRESSED mode keeps direction and order. It also checks unit conversions (au, obliquity). - `test_rotation` checks the body frames built from the IAU rotation elements: each frame is right-handed and orthonormal with +Z on the pole, W = 0 puts the prime meridian on the ascending node, and Earth's pole is 23.44° from ecliptic north with its prime meridian back in place after one sidereal day. - `test_csv` covers the catalogue's CSV reader: comment and blank lines, CRLF line ends, empty cells, the fallback for an empty number, and errors that name the file for a ragged row, a missing header, an unknown column or a cell that is not a number.