fix: Fix the ledger_index timezone issue (#1522)

Fix unittest failures
This commit is contained in:
cyan317
2024-07-09 13:14:22 +01:00
committed by GitHub
parent d536433d64
commit 094ed8f299
8 changed files with 167 additions and 14 deletions

View File

@@ -22,6 +22,7 @@
#include "rpc/Errors.hpp"
#include "rpc/RPCHelpers.hpp"
#include "rpc/common/Types.hpp"
#include "util/TimeUtils.hpp"
#include <boost/json/object.hpp>
#include <boost/json/value.hpp>
@@ -65,10 +66,8 @@ TimeFormatValidator::verify(boost::json::value const& value, std::string_view ke
if (not value.as_object().at(key).is_string())
return Error{Status{RippledError::rpcINVALID_PARAMS}};
std::tm time = {};
std::stringstream stream(value_to<std::string>(value.as_object().at(key)));
stream >> std::get_time(&time, format_.c_str());
if (stream.fail())
auto const ret = util::SystemTpFromUTCStr(value_to<std::string>(value.as_object().at(key)), format_);
if (!ret)
return Error{Status{RippledError::rpcINVALID_PARAMS}};
return {};

View File

@@ -22,19 +22,16 @@
#include "rpc/Errors.hpp"
#include "rpc/JS.hpp"
#include "rpc/common/Types.hpp"
#include "util/TimeUtils.hpp"
#include <boost/json/conversion.hpp>
#include <boost/json/object.hpp>
#include <boost/json/value.hpp>
#include <xrpl/basics/chrono.h>
#include <xrpl/basics/strHex.h>
#include <xrpl/protocol/jss.h>
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <ctime>
#include <iomanip>
#include <ranges>
#include <sstream>
#include <string>
@@ -61,18 +58,16 @@ LedgerIndexHandler::process(LedgerIndexHandler::Input input, Context const& ctx)
return fillOutputByIndex(maxIndex);
auto const convertISOTimeStrToTicks = [](std::string const& isoTimeStr) {
std::tm time = {};
std::stringstream ss(isoTimeStr);
ss >> std::get_time(&time, DATE_FORMAT);
return std::chrono::system_clock::from_time_t(std::mktime(&time)).time_since_epoch().count();
auto const systemTime = util::SystemTpFromUTCStr(isoTimeStr, DATE_FORMAT);
// systemTime must be valid after validation passed
return systemTime->time_since_epoch().count();
};
auto const ticks = convertISOTimeStrToTicks(*input.date);
auto const earlierThan = [&](std::uint32_t ledgerIndex) {
auto const header = sharedPtrBackend_->fetchLedgerBySequence(ledgerIndex, ctx.yield);
auto const ledgerTime =
std::chrono::system_clock::time_point{header->closeTime.time_since_epoch() + ripple::epoch_offset};
auto const ledgerTime = util::SystemTpFromLedgerCloseTime(header->closeTime);
return ticks < ledgerTime.time_since_epoch().count();
};