Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 52 additions & 13 deletions src/orrery/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <chrono>
#include <cmath>
#include <cstddef>
#include <cstdio>

namespace orrery {
Expand Down Expand Up @@ -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<std::size_t>(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;
Expand Down
4 changes: 3 additions & 1 deletion src/orrery/time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
13 changes: 13 additions & 0 deletions tests/test_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading