Kea 2.7.5
chrono_time_utils.cc
Go to the documentation of this file.
1// Copyright (C) 2015-2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
10#include <sstream>
11#include <iomanip>
12
13using namespace std::chrono;
14
15namespace isc {
16namespace util {
17
18std::string
19clockToText(std::chrono::system_clock::time_point t, size_t fsecs_precision) {
20 time_t tt = system_clock::to_time_t(t);
21 struct tm tm;
22 localtime_r(&tt, &tm);
23 std::stringstream s;
24 s << (tm.tm_year + 1900)
25 << "-" << std::setw(2) << std::setfill('0') << (tm.tm_mon + 1)
26 << "-" << std::setw(2) << std::setfill('0') << tm.tm_mday
27 << " " << std::setw(2) << std::setfill('0') << tm.tm_hour
28 << ":" << std::setw(2) << std::setfill('0') << tm.tm_min
29 << ":" << std::setw(2) << std::setfill('0') << tm.tm_sec;
30
31 // If the requested precision is less than the maximum native precision
32 // we will divide the fractional seconds value by 10^(max - requested)
33 if (fsecs_precision) {
34 system_clock::duration dur = t - system_clock::from_time_t(tt);
35 microseconds frac = duration_cast<microseconds>(dur);
36 auto fsecs = frac.count();
37 size_t width = MAX_FSECS_PRECISION;
38 if (fsecs_precision < width) {
39 for (size_t i = 0; i < width - fsecs_precision; ++i) {
40 fsecs /= 10;
41 }
42
43 width = fsecs_precision;
44 }
45
46 s << "." << std::setw(width)
47 << std::setfill('0')
48 << fsecs;
49 }
50
51 return (s.str());
52}
53
54template<typename Duration> std::string
55durationToText(Duration dur, size_t fsecs_precision) {
56 seconds unfrac = duration_cast<seconds>(dur);
57 auto secs = unfrac.count();
58 std::stringstream s;
59 auto hours = secs / 3600;
60 secs -= hours * 3600;
61 s << std::setw(2) << std::setfill('0') << hours;
62 auto mins = secs / 60;
63 secs -= mins * 60;
64 s << ":" << std::setw(2) << std::setfill('0') << mins
65 << ":" << std::setw(2) << std::setfill('0') << secs;
66
67 // If the requested precision is less than the maximum native precision
68 // we will divide the fractional seconds value by 10^(max - requested)
69 if (fsecs_precision) {
70 microseconds frac = duration_cast<microseconds>(dur);
71 frac -= duration_cast<microseconds>(unfrac);
72 auto fsecs = frac.count();
73 size_t width = MAX_FSECS_PRECISION;
74 if (fsecs_precision < width) {
75 for (size_t i = 0; i < width - fsecs_precision; ++i) {
76 fsecs /= 10;
77 }
78
79 width = fsecs_precision;
80 }
81
82 s << "." << std::setw(width)
83 << std::setfill('0')
84 << fsecs;
85 }
86
87 return (s.str());
88}
89
90// Instantiate for standard clocks.
91template std::string
92durationToText<system_clock::duration>(system_clock::duration dur,
93 size_t fsecs_precision);
94
95#if !CHRONO_SAME_DURATION
96template std::string
97durationToText<steady_clock::duration>(steady_clock::duration dur,
98 size_t fsecs_precision);
99#endif
100
101} // end of isc::util namespace
102} // end of isc namespace
template std::string durationToText< steady_clock::duration >(steady_clock::duration dur, size_t fsecs_precision)
const size_t MAX_FSECS_PRECISION
The number of digits of fractional seconds supplied by the underlying class, boost::posix_time.
std::string durationToText(boost::posix_time::time_duration dur, size_t fsecs_precision=MAX_FSECS_PRECISION)
Converts StatsDuration to text.
std::string clockToText(std::chrono::system_clock::time_point t, size_t fsecs_precision)
Converts chrono time point structure to text.
template std::string durationToText< system_clock::duration >(system_clock::duration dur, size_t fsecs_precision)
Defines the logger used by the top-level component of kea-lfc.