From f5f56e1a1c4d9e9496fcb9d81131066a964ccd23 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 14:15:43 +0200 Subject: Adding upstream version 2.4.1. Signed-off-by: Daniel Baumann --- src/lib/util/chrono_time_utils.cc | 102 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/lib/util/chrono_time_utils.cc (limited to 'src/lib/util/chrono_time_utils.cc') diff --git a/src/lib/util/chrono_time_utils.cc b/src/lib/util/chrono_time_utils.cc new file mode 100644 index 0000000..6c76112 --- /dev/null +++ b/src/lib/util/chrono_time_utils.cc @@ -0,0 +1,102 @@ +// Copyright (C) 2015-2020 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include + +#include +#include +#include + +using namespace std::chrono; + +namespace isc { +namespace util { + +std::string +clockToText(std::chrono::system_clock::time_point t, size_t fsecs_precision) { + time_t tt = system_clock::to_time_t(t); + struct tm tm; + localtime_r(&tt, &tm); + std::stringstream s; + s << (tm.tm_year + 1900) + << "-" << std::setw(2) << std::setfill('0') << (tm.tm_mon + 1) + << "-" << std::setw(2) << std::setfill('0') << tm.tm_mday + << " " << std::setw(2) << std::setfill('0') << tm.tm_hour + << ":" << std::setw(2) << std::setfill('0') << tm.tm_min + << ":" << std::setw(2) << std::setfill('0') << tm.tm_sec; + + // If the requested precision is less than the maximum native precision + // we will divide the fractional seconds value by 10^(max - requested) + if (fsecs_precision) { + system_clock::duration dur = t - system_clock::from_time_t(tt); + microseconds frac = duration_cast(dur); + auto fsecs = frac.count(); + size_t width = MAX_FSECS_PRECISION; + if (fsecs_precision < width) { + for (auto i = 0; i < width - fsecs_precision; ++i) { + fsecs /= 10; + } + + width = fsecs_precision; + } + + s << "." << std::setw(width) + << std::setfill('0') + << fsecs; + } + + return (s.str()); +} + +template std::string +durationToText(Duration dur, size_t fsecs_precision) { + seconds unfrac = duration_cast(dur); + auto secs = unfrac.count(); + std::stringstream s; + auto hours = secs / 3600; + secs -= hours * 3600; + s << std::setw(2) << std::setfill('0') << hours; + auto mins = secs / 60; + secs -= mins * 60; + s << ":" << std::setw(2) << std::setfill('0') << mins + << ":" << std::setw(2) << std::setfill('0') << secs; + + // If the requested precision is less than the maximum native precision + // we will divide the fractional seconds value by 10^(max - requested) + if (fsecs_precision) { + microseconds frac = duration_cast(dur); + frac -= duration_cast(unfrac); + auto fsecs = frac.count(); + size_t width = MAX_FSECS_PRECISION; + if (fsecs_precision < width) { + for (auto i = 0; i < width - fsecs_precision; ++i) { + fsecs /= 10; + } + + width = fsecs_precision; + } + + s << "." << std::setw(width) + << std::setfill('0') + << fsecs; + } + + return (s.str()); +} + +// Instantiate for standard clocks. +template std::string +durationToText(system_clock::duration dur, + size_t fsecs_precision); + +#if !CHRONO_SAME_DURATION +template std::string +durationToText(steady_clock::duration dur, + size_t fsecs_precision); +#endif + +} // end of isc::util namespace +} // end of isc namespace -- cgit v1.2.3