C++ Utilities  4.9.2
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 noMilliseconds) const
67 {
68  string result;
69  toString(result, format, noMilliseconds);
70  return result;
71 }
72 
81 void TimeSpan::toString(string &result, TimeSpanOutputFormat format, bool noMilliseconds) 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  << " ";
93  break;
94  case TimeSpanOutputFormat::WithMeasures:
95  if (isNull()) {
96  s << "0 s ";
97  } else {
98  if (positive.totalMilliseconds() < 1.0) {
99  s << setprecision(2) << (m_ticks / 10.0) << " µs ";
100  } else {
101  if (const int days = positive.days()) {
102  s << days << " d ";
103  }
104  if (const int hours = positive.hours()) {
105  s << hours << " h ";
106  }
107  if (const int minutes = positive.minutes()) {
108  s << minutes << " min ";
109  }
110  if (const int seconds = positive.seconds()) {
111  s << seconds << " s ";
112  }
113  if (!noMilliseconds) {
114  if (const int milliseconds = positive.milliseconds()) {
115  s << milliseconds << " ms ";
116  }
117  }
118  }
119  }
120  break;
121  }
122  result = s.str().substr(0, static_cast<string::size_type>(s.tellp()) - 1);
123 }
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:184
constexpr bool isNegative() const
Returns ture if the time interval represented by the current TimeSpan class is negative.
Definition: timespan.h:354
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:256
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:248
STL namespace.
constexpr double totalHours() const
Gets the value of the current TimeSpan class expressed in whole and fractional hours.
Definition: timespan.h:208
constexpr int minutes() const
Gets the minutes component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:240
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:224
constexpr int seconds() const
Gets the seconds component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:232