From 1ff0fee1e8b00bd8585911191267834f745ecc09 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 11 Sep 2025 21:15:40 +0200 Subject: [PATCH] Add support for specifying the format string for the `openspace.time.SPICE()` function --- include/openspace/util/spicemanager.h | 3 +++ include/openspace/util/time.h | 7 +++++++ src/util/spicemanager.cpp | 18 +++++++++++++++++- src/util/time.cpp | 10 ++++++++++ src/util/time_lua.inl | 13 +++++++++---- 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/include/openspace/util/spicemanager.h b/include/openspace/util/spicemanager.h index 3128e5c4c4..e7c71f30ef 100644 --- a/include/openspace/util/spicemanager.h +++ b/include/openspace/util/spicemanager.h @@ -613,6 +613,9 @@ public: std::string dateFromEphemerisTime(double ephemerisTime, const char* format); + void dateFromEphemerisTime(double ephemerisTime, char* outBuf, int bufferSize, + const std::string& format = "YYYY MON DDTHR:MN:SC.### ::RND") const; + /** * Returns the \p position of a \p target body relative to an \p observer in a * specific \p referenceFrame, optionally corrected for \p lightTime (planetary diff --git a/include/openspace/util/time.h b/include/openspace/util/time.h index e6fe1b581c..c2a4fd651f 100644 --- a/include/openspace/util/time.h +++ b/include/openspace/util/time.h @@ -136,6 +136,13 @@ public: */ std::string_view UTC() const; + /** + * Returns the current time as a formatted date string. The date string can be + * formatted using the SPICE picture parameters as described in + * https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/timout_c.html + */ + std::string_view string(const std::string& format) const; + /** * Returns the current time as a ISO 8601 formatted, i.e YYYY-MM-DDThh:mm:ssZ. * diff --git a/src/util/spicemanager.cpp b/src/util/spicemanager.cpp index b53fa40b9c..c8f1d3c3e4 100644 --- a/src/util/spicemanager.cpp +++ b/src/util/spicemanager.cpp @@ -637,10 +637,26 @@ std::string SpiceManager::dateFromEphemerisTime(double ephemerisTime, const char et2utc_c(ephemerisTime, "C", SecondsPrecision, BufferSize, Buffer.data()); } - return std::string(Buffer.data()); } +void SpiceManager::dateFromEphemerisTime(double ephemerisTime, char* outBuf, + int bufferSize, const std::string& format) const +{ + timout_c(ephemerisTime, format.c_str(), bufferSize, outBuf); + if (failed_c()) { + throwSpiceError(std::format( + "Error converting ephemeris time '{}' to date with format '{}'", + ephemerisTime, format + )); + } + if (outBuf[0] == '*') { + // The conversion failed and we need to use et2utc + constexpr int SecondsPrecision = 3; + et2utc_c(ephemerisTime, "C", SecondsPrecision, bufferSize, outBuf); + } +} + glm::dvec3 SpiceManager::targetPosition(const std::string& target, const std::string& observer, const std::string& referenceFrame, diff --git a/src/util/time.cpp b/src/util/time.cpp index 5ae23792e6..3dcf06894f 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -121,6 +121,16 @@ std::string_view Time::UTC() const { return std::string_view(b); } +std::string_view Time::string(const std::string& format) const { + char* b = reinterpret_cast( + global::memoryManager->TemporaryMemory.allocate(128) + ); + std::memset(b, 0, 128); + + SpiceManager::ref().dateFromEphemerisTime(_time, b, 128, format.c_str()); + return std::string_view(b); +} + std::string_view Time::ISO8601() const { ZoneScoped; diff --git a/src/util/time_lua.inl b/src/util/time_lua.inl index 2dfb3a4daa..f8a6a00904 100644 --- a/src/util/time_lua.inl +++ b/src/util/time_lua.inl @@ -349,13 +349,18 @@ namespace { /** - * Returns the current time as an date string of the form - * (YYYY MON DDTHR:MN:SC.### ::RND) as returned by SPICE. + * Returns the current time as an date string. The format of the returned string can be + * adjusted by providing the format picture. The default picture that is used will be + * (YYYY MON DDTHR:MN:SC.### ::RND). See + * https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/timout_c.html + * for documentation on how the format string can be formatted * * \return The current time, in the format used by SPICE (YYYY MON DDTHR:MN:SC.### ::RND) */ -[[codegen::luawrap("SPICE")]] std::string currentTimeSpice() { - return std::string(openspace::global::timeManager->time().UTC()); +[[codegen::luawrap("SPICE")]] std::string currentTimeSpice( + std::string format = "YYYY MON DDTHR:MN:SC.### ::RND") +{ + return std::string(openspace::global::timeManager->time().string(format)); } /**