C++ Utilities  4.10.0
Common C++ classes and routines used by my applications 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 
23 TimeSpan TimeSpan::fromString(const char *str, char separator)
24 {
25  if (!*str) {
26  return TimeSpan();
27  }
28 
29  vector<double> parts;
30  size_t partsSize = 1;
31  for (const char *i = str; *i; ++i) {
32  *i == separator && ++partsSize;
33  }
34  parts.reserve(partsSize);
35 
36  for (const char *i = str;;) {
37  if (*i == separator) {
38  parts.emplace_back(stringToNumber<double>(string(str, i)));
39  str = ++i;
40  } else if (*i == '\0') {
41  parts.emplace_back(stringToNumber<double>(string(str, i)));
42  break;
43  } else {
44  ++i;
45  }
46  }
47 
48  switch (parts.size()) {
49  case 1:
50  return TimeSpan::fromSeconds(parts.front());
51  case 2:
52  return TimeSpan::fromMinutes(parts.front()) + TimeSpan::fromSeconds(parts[1]);
53  case 3:
54  return TimeSpan::fromHours(parts.front()) + TimeSpan::fromMinutes(parts[1]) + TimeSpan::fromSeconds(parts[2]);
55  default:
56  return TimeSpan::fromDays(parts.front()) + TimeSpan::fromHours(parts[1]) + TimeSpan::fromMinutes(parts[2]) + TimeSpan::fromSeconds(parts[3]);
57  }
58 }
59 
66 string TimeSpan::toString(TimeSpanOutputFormat format, bool fullSeconds) const
67 {
68  string result;
69  toString(result, format, fullSeconds);
70  return result;
71 }
72 
81 void TimeSpan::toString(string &result, TimeSpanOutputFormat format, bool fullSeconds) const
82 {
83  stringstream s(stringstream::in | stringstream::out);
84  TimeSpan positive(m_ticks);
85  if (positive.isNegative()) {
86  s << '-';
87  positive.m_ticks = -positive.m_ticks;
88  }
89  switch (format) {
90  case TimeSpanOutputFormat::Normal:
91  s << setfill('0') << setw(2) << floor(positive.totalHours()) << ":" << setw(2) << positive.minutes() << ":" << setw(2) << positive.seconds();
92  if (!fullSeconds) {
93  const int milli(positive.milliseconds());
94  const int micro(positive.microseconds());
95  const int nano(positive.nanoseconds());
96  if (milli || micro || nano) {
97  s << '.' << setw(3) << milli;
98  if (micro || nano) {
99  s << setw(3) << micro;
100  if (nano) {
101  s << nano / TimeSpan::nanosecondsPerTick;
102  }
103  }
104  }
105  }
106  break;
107  case TimeSpanOutputFormat::WithMeasures:
108  if (isNull()) {
109  result = "0 s";
110  return;
111  } else {
112  if (!fullSeconds && positive.totalMilliseconds() < 1.0) {
113  s << setprecision(2) << positive.totalMicroseconds() << " µs";
114  } else {
115  bool needWhitespace = false;
116  if (const int days = positive.days()) {
117  needWhitespace = true;
118  s << days << " d";
119  }
120  if (const int hours = positive.hours()) {
121  if (needWhitespace)
122  s << ' ';
123  needWhitespace = true;
124  s << hours << " h";
125  }
126  if (const int minutes = positive.minutes()) {
127  if (needWhitespace)
128  s << ' ';
129  needWhitespace = true;
130  s << minutes << " min";
131  }
132  if (const int seconds = positive.seconds()) {
133  if (needWhitespace)
134  s << ' ';
135  needWhitespace = true;
136  s << seconds << " s";
137  }
138  if (!fullSeconds) {
139  if (const int milliseconds = positive.milliseconds()) {
140  if (needWhitespace)
141  s << ' ';
142  needWhitespace = true;
143  s << milliseconds << " ms";
144  }
145  if (const int microseconds = positive.microseconds()) {
146  if (needWhitespace)
147  s << ' ';
148  needWhitespace = true;
149  s << microseconds << " µs";
150  }
151  if (const int nanoseconds = positive.nanoseconds()) {
152  if (needWhitespace)
153  s << ' ';
154  s << nanoseconds << " ns";
155  }
156  }
157  }
158  }
159  break;
160  }
161  result = s.str();
162 }
constexpr int nanoseconds() const
Gets the nanoseconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:239
TimeSpanOutputFormat
Specifies the output format.
Definition: timespan.h:22
constexpr double totalMilliseconds() const
Gets the value of the current TimeSpan class expressed in whole and fractional milliseconds.
Definition: timespan.h:197
constexpr bool isNegative() const
Returns ture if the time interval represented by the current TimeSpan class is negative.
Definition: timespan.h:385
Contains classes providing a means for handling date and time information.
Definition: datetime.h:12
constexpr int days() const
Gets the days component of the time interval represented by the current TimeSpan class.
Definition: timespan.h:287
Represents a time interval.
Definition: timespan.h:27
constexpr int hours() const
Gets the hours component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:279
constexpr double totalMicroseconds() const
Gets the value of the current TimeSpan class expressed in whole and fractional microseconds.
Definition: timespan.h:189
STL namespace.
constexpr double totalHours() const
Gets the value of the current TimeSpan class expressed in whole and fractional hours.
Definition: timespan.h:221
constexpr int minutes() const
Gets the minutes component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:271
Contains several functions providing conversions between different data types.
constexpr int milliseconds() const
Gets the miliseconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:255
constexpr int seconds() const
Gets the seconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:263
constexpr int microseconds() const
Gets the microseconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:247