Eazy Datetime Cpp
time_delta.h
1 #ifndef _MY_TIME_DELTA_
2 #define _MY_TIME_DELTA_
3 
4 #include <string>
5 #include <sstream>
6 #include <vector>
7 #include <cmath>
8 
9 #include "datetime_exceptions.h"
10 #include "datetime_constants.h"
11 
12 namespace EZ
13 {
18  class TimeDelta
19  {
20  // 累計秒
21 
22  long long m_totalSeconds = 0;
23  long long m_totalDays = 0;
24 
25  // 累計秒を分解したもの
26  long long m_seconds = 0;
27  long long m_minutes = 0;
28  long long m_hours = 0;
29  // long long m_days = 0;
30  // 月の日数は一定でない。年の日数も一定でない(閏年があるため)
31  // long long m_months = 0;
32  // long long m_years = 0;
33 
34  public:
35  TimeDelta()
36  {
37  }
38 
45  TimeDelta(const long long &totalSeconds)
46  : m_totalSeconds(totalSeconds)
47  {
48  parseTotalSeconds();
49  }
50 
51  TimeDelta(const TimeDelta &origin)
52  {
53  m_totalSeconds = origin.totalSeconds();
54  parseTotalSeconds();
55  }
56 
67  TimeDelta(const long long &days, const long long &hours, const long long &minutes, const long long &seconds)
68  {
69  set(days, hours, minutes, seconds);
70  }
71 
72  ~TimeDelta()
73  {
74  }
75 
85  void set(const long long &days, const long long &hours, const long long &minutes, const long long &seconds)
86  {
87  m_totalSeconds = ((days * 24 + hours) * 60 + minutes) * 60 + seconds;
88  parseTotalSeconds();
89  }
94  long long totalSeconds() const
95  {
96  return m_totalSeconds;
97  }
102  long long totalMinutes() const
103  {
104  return m_totalSeconds / 60;
105  }
110  long long totalHours() const
111  {
112  return m_totalSeconds / 3600;
113  }
118  long long totalDays() const
119  {
120  return m_totalDays;
121  }
126  long long totalWeeks() const
127  {
128  return m_totalDays / 7;
129  }
135  std::vector<long long> toVector() const
136  {
137  return {m_totalDays, m_hours, m_minutes, m_seconds};
138  }
144  std::string str() const
145  {
146  std::stringstream ss;
147  ss << "TimeDelta("
148  << "days=" << m_totalDays
149  << ", hours=" << m_hours
150  << ", minutes=" << m_minutes
151  << ", seconds=" << m_seconds
152  << ")";
153  return ss.str();
154  }
155 
156  TimeDelta operator+() const
157  {
158  return TimeDelta(m_totalSeconds);
159  }
160  TimeDelta operator-() const
161  {
162  return TimeDelta(-m_totalSeconds);
163  }
168  TimeDelta operator+(const TimeDelta &right) const
169  {
170  return m_totalSeconds + right.totalSeconds();
171  }
176  TimeDelta operator+(const long long &right) const
177  {
178  long long retSeconds = m_totalSeconds + right;
179  return TimeDelta(retSeconds);
180  }
181 
182  TimeDelta operator-(const TimeDelta &right) const
183  {
184  return operator+(-right.totalSeconds());
185  }
186  TimeDelta operator-(const long long &right) const
187  {
188  return operator+(-right);
189  }
194  TimeDelta operator*(const double &right) const
195  {
196  long long retSeconds = double(m_totalSeconds) * right;
197  return TimeDelta(retSeconds);
198  }
203  double operator/(const TimeDelta &right) const
204  {
205  if (std::abs(right.totalSeconds()) < DatetimeConstants::DOUBLE_EPSILON)
206  {
207  throw DatetimeException("ERROR: Could not divide by nearly ZERO value.");
208  }
209  return double(m_totalSeconds) / right.totalSeconds();
210  }
215  TimeDelta operator/(const double &right) const
216  {
217  if (std::abs(right) < DatetimeConstants::DOUBLE_EPSILON)
218  {
219  throw DatetimeException("ERROR: Could not divide by nearly ZERO value.");
220  }
221  long long retSeconds = double(m_totalSeconds) / right;
222  return TimeDelta(retSeconds);
223  }
224 
225  TimeDelta &operator+=(const long long &right)
226  {
227  m_totalSeconds += right;
228  parseTotalSeconds();
229  return *this;
230  }
231  TimeDelta &operator+=(const TimeDelta &right)
232  {
233  return operator+=(right.totalSeconds());
234  }
235 
236  TimeDelta &operator-=(const long long &right)
237  {
238  return operator+=(-right);
239  }
240  TimeDelta &operator-=(const TimeDelta &right)
241  {
242  return operator+=(-right.totalSeconds());
243  }
244 
245  TimeDelta &operator*=(const double &right)
246  {
247  m_totalSeconds = double(m_totalSeconds) * right;
248  parseTotalSeconds();
249  return *this;
250  }
251 
252  TimeDelta &operator/=(const double &right)
253  {
254  return operator*=(1.0 / right);
255  }
256 
257  private:
258  void parseTotalSeconds()
259  {
260  long long remainedSecondsOfDay = m_totalSeconds % (24 * 3600);
261  m_hours = remainedSecondsOfDay / 3600;
262  m_minutes = remainedSecondsOfDay % 3600 / 60;
263  m_seconds = remainedSecondsOfDay % 3600 % 60;
264  m_totalDays = m_totalSeconds / (24 * 3600);
265  }
266  };
267 
272  bool operator==(const TimeDelta &left, const TimeDelta &right)
273  {
274  return left.totalSeconds() == right.totalSeconds();
275  }
276 
277  bool operator==(const TimeDelta &left, const long long &right)
278  {
279  return left.totalSeconds() == right;
280  }
281 
282  bool operator==(const long long &left, const TimeDelta &right)
283  {
284  return left == right.totalSeconds();
285  }
286 
287  bool operator!=(const TimeDelta &left, const TimeDelta &right)
288  {
289  return !operator==(left, right);
290  }
291 
292  bool operator!=(const TimeDelta &left, const long long &right)
293  {
294  return !operator==(left, right);
295  }
296 
297  bool operator!=(const long long &left, const TimeDelta &right)
298  {
299  return !operator==(left, right);
300  }
301 
302  bool operator>(const TimeDelta &left, const long long &right)
303  {
304  return left.totalSeconds() > right;
305  }
306 
307  bool operator>(const long long &left, const TimeDelta &right)
308  {
309  return left > right.totalSeconds();
310  }
311 
312  bool operator<(const TimeDelta &left, const long long &right)
313  {
314  return left.totalSeconds() < right;
315  }
316 
317  bool operator<(const long long &left, const TimeDelta &right)
318  {
319  return left < right.totalSeconds();
320  }
321 
322  bool operator>=(const TimeDelta &left, const long long &right)
323  {
324  return left == right || left > right;
325  }
326 
327  bool operator>=(const long long &left, const TimeDelta &right)
328  {
329  return left == right || left > right;
330  }
331 
332  bool operator<=(const TimeDelta &left, const long long &right)
333  {
334  return left == right || left < right;
335  }
336 
337  bool operator<=(const long long &left, const TimeDelta &right)
338  {
339  return left == right || left < right;
340  }
341 
342  std::ostream &operator<<(std::ostream &stream, const TimeDelta &timeDelta)
343  {
344  stream << timeDelta.str();
345  return stream;
346  }
347 
348  const TimeDelta operator*(const double &left, const TimeDelta right)
349  {
350  return TimeDelta(double(right.totalSeconds()) * left);
351  }
352 }
353 #endif
Definition: datetime_exceptions.h:9
TimeDelta object.
Definition: time_delta.h:19
TimeDelta(const long long &totalSeconds)
Definition: time_delta.h:45
TimeDelta operator*(const double &right) const
Definition: time_delta.h:194
double operator/(const TimeDelta &right) const
Definition: time_delta.h:203
long long totalDays() const
Definition: time_delta.h:118
std::vector< long long > toVector() const
Definition: time_delta.h:135
TimeDelta operator/(const double &right) const
Definition: time_delta.h:215
TimeDelta operator+(const long long &right) const
Definition: time_delta.h:176
long long totalHours() const
Definition: time_delta.h:110
long long totalWeeks() const
Definition: time_delta.h:126
void set(const long long &days, const long long &hours, const long long &minutes, const long long &seconds)
Definition: time_delta.h:85
std::string str() const
Definition: time_delta.h:144
long long totalMinutes() const
Definition: time_delta.h:102
TimeDelta operator+(const TimeDelta &right) const
Definition: time_delta.h:168
long long totalSeconds() const
Definition: time_delta.h:94
TimeDelta(const long long &days, const long long &hours, const long long &minutes, const long long &seconds)
Definition: time_delta.h:67