C++ Utilities  4.17.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
timespan.cpp
Go to the documentation of this file.
1 #include "./timespan.h"
2 
3 #include "../conversion/stringconversion.h"
4 
5 #include <cmath>
6 #include <iomanip>
7 #include <sstream>
8 #include <vector>
9 
10 using namespace std;
11 using namespace ChronoUtilities;
12 using namespace ConversionUtilities;
13 
36 TimeSpan TimeSpan::fromString(const char *str, char separator)
37 {
38  if (!*str) {
39  return TimeSpan();
40  }
41 
42  vector<double> parts;
43  size_t partsSize = 1;
44  for (const char *i = str; *i; ++i) {
45  *i == separator && ++partsSize;
46  }
47  parts.reserve(partsSize);
48 
49  for (const char *i = str;;) {
50  if (*i == separator) {
51  parts.emplace_back(stringToNumber<double>(string(str, i)));
52  str = ++i;
53  } else if (*i == '\0') {
54  parts.emplace_back(stringToNumber<double>(string(str, i)));
55  break;
56  } else {
57  ++i;
58  }
59  }
60 
61  switch (parts.size()) {
62  case 1:
63  return TimeSpan::fromSeconds(parts.front());
64  case 2:
65  return TimeSpan::fromMinutes(parts.front()) + TimeSpan::fromSeconds(parts[1]);
66  case 3:
67  return TimeSpan::fromHours(parts.front()) + TimeSpan::fromMinutes(parts[1]) + TimeSpan::fromSeconds(parts[2]);
68  default:
69  return TimeSpan::fromDays(parts.front()) + TimeSpan::fromHours(parts[1]) + TimeSpan::fromMinutes(parts[2]) + TimeSpan::fromSeconds(parts[3]);
70  }
71 }
72 
79 string TimeSpan::toString(TimeSpanOutputFormat format, bool fullSeconds) const
80 {
81  string result;
82  toString(result, format, fullSeconds);
83  return result;
84 }
85 
94 void TimeSpan::toString(string &result, TimeSpanOutputFormat format, bool fullSeconds) const
95 {
96  stringstream s(stringstream::in | stringstream::out);
97  TimeSpan positive(m_ticks);
98  if (positive.isNegative()) {
99  s << '-';
100  positive.m_ticks = -positive.m_ticks;
101  }
102  switch (format) {
103  case TimeSpanOutputFormat::Normal:
104  s << setfill('0') << setw(2) << floor(positive.totalHours()) << ":" << setw(2) << positive.minutes() << ":" << setw(2) << positive.seconds();
105  if (!fullSeconds) {
106  const int milli(positive.milliseconds());
107  const int micro(positive.microseconds());
108  const int nano(positive.nanoseconds());
109  if (milli || micro || nano) {
110  s << '.' << setw(3) << milli;
111  if (micro || nano) {
112  s << setw(3) << micro;
113  if (nano) {
114  s << nano / TimeSpan::nanosecondsPerTick;
115  }
116  }
117  }
118  }
119  break;
120  case TimeSpanOutputFormat::WithMeasures:
121  if (isNull()) {
122  result = "0 s";
123  return;
124  } else {
125  if (!fullSeconds && positive.totalMilliseconds() < 1.0) {
126  s << setprecision(2) << positive.totalMicroseconds() << " µs";
127  } else {
128  bool needWhitespace = false;
129  if (const int days = positive.days()) {
130  needWhitespace = true;
131  s << days << " d";
132  }
133  if (const int hours = positive.hours()) {
134  if (needWhitespace)
135  s << ' ';
136  needWhitespace = true;
137  s << hours << " h";
138  }
139  if (const int minutes = positive.minutes()) {
140  if (needWhitespace)
141  s << ' ';
142  needWhitespace = true;
143  s << minutes << " min";
144  }
145  if (const int seconds = positive.seconds()) {
146  if (needWhitespace)
147  s << ' ';
148  needWhitespace = true;
149  s << seconds << " s";
150  }
151  if (!fullSeconds) {
152  if (const int milliseconds = positive.milliseconds()) {
153  if (needWhitespace)
154  s << ' ';
155  needWhitespace = true;
156  s << milliseconds << " ms";
157  }
158  if (const int microseconds = positive.microseconds()) {
159  if (needWhitespace)
160  s << ' ';
161  needWhitespace = true;
162  s << microseconds << " µs";
163  }
164  if (const int nanoseconds = positive.nanoseconds()) {
165  if (needWhitespace)
166  s << ' ';
167  s << nanoseconds << " ns";
168  }
169  }
170  }
171  }
172  break;
173  case TimeSpanOutputFormat::TotalSeconds:
174  if (fullSeconds) {
175  s << setprecision(0);
176  } else {
177  s << setprecision(10);
178  }
179  s << positive.totalSeconds();
180  break;
181  }
182  result = s.str();
183 }
constexpr int nanoseconds() const
Returns the nanoseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:254
TimeSpanOutputFormat
Specifies the output format.
Definition: timespan.h:22
constexpr double totalMilliseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional milliseconds.
Definition: timespan.h:212
constexpr bool isNegative() const
Returns ture if the time interval represented by the current TimeSpan class is negative.
Definition: timespan.h:400
Contains classes providing a means for handling date and time information.
Definition: datetime.h:12
constexpr int days() const
Returns the days component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:302
Represents a time interval.
Definition: timespan.h:28
constexpr int hours() const
Returns the hours component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:294
constexpr double totalMicroseconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional microseconds.
Definition: timespan.h:204
constexpr double totalHours() const
Returns the value of the current TimeSpan class expressed in whole and fractional hours.
Definition: timespan.h:236
constexpr int i
constexpr int minutes() const
Returns the minutes component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:286
Contains several functions providing conversions between different data types.
constexpr int milliseconds() const
Returns the miliseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:270
constexpr double totalSeconds() const
Returns the value of the current TimeSpan class expressed in whole and fractional seconds.
Definition: timespan.h:220
constexpr int seconds() const
Returns the seconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:278
constexpr int microseconds() const
Returns the microseconds component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:262