Kea  1.9.9-git
boost_time_utils.cc
Go to the documentation of this file.
1 // Copyright (C) 2015-2019 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 
13 std::string
14 isc::util::ptimeToText(boost::posix_time::ptime t, size_t fsecs_precision) {
15  boost::gregorian::date d = t.date();
16  std::stringstream s;
17  s << d.year()
18  << "-" << std::setw(2) << std::setfill('0') << d.month().as_number()
19  << "-" << std::setw(2) << std::setfill('0') << d.day()
20  << " " << durationToText(t.time_of_day(), fsecs_precision);
21  return (s.str());
22 }
23 
24 std::string
25 isc::util::durationToText(boost::posix_time::time_duration dur, size_t fsecs_precision) {
26  std::stringstream s;
27  s << std::setw(2) << std::setfill('0') << dur.hours()
28  << ":" << std::setw(2) << std::setfill('0') << dur.minutes()
29  << ":" << std::setw(2) << std::setfill('0') << dur.seconds();
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  size_t fsecs = dur.fractional_seconds();
35  size_t width = MAX_FSECS_PRECISION;
36  if (fsecs_precision < width) {
37  for (auto i = 0; i < width - fsecs_precision; ++i) {
38  fsecs /= 10;
39  }
40 
41  width = fsecs_precision;
42  }
43 
44  s << "." << std::setw(width)
45  << std::setfill('0')
46  << fsecs;
47  }
48 
49  return (s.str());
50 }
std::string durationToText(boost::posix_time::time_duration dur, size_t fsecs_precision=MAX_FSECS_PRECISION)
Converts StatsDuration to text.
std::string ptimeToText(boost::posix_time::ptime t, size_t fsecs_precision=MAX_FSECS_PRECISION)
Converts ptime structure to text.
const size_t MAX_FSECS_PRECISION
The number of digits of fractional seconds supplied by the underlying class, boost::posix_time.