libUTL++
Duration.h
1 #pragma once
2 
4 
5 #include <libutl/Float.h>
6 
8 
9 UTL_NS_BEGIN;
10 
12 
13 class Time;
14 
16 
38 
40 class Duration : public Float
41 {
44 
45 public:
50  Duration(double seconds)
51  {
52  Number<double>::set(seconds);
53  }
54 
59  Duration(const String& str)
60  {
61  set(str);
62  }
63 
71  Duration(uint_t days, uint_t hours, uint_t minutes, double seconds)
72  {
73  set(days, hours, minutes, seconds);
74  }
75 
76  virtual String toString() const;
77 
79  uint_t
80  days() const
81  {
82  return (uint_t)(_n / (24 * 60 * 60));
83  }
84 
86  uint_t
87  hours() const
88  {
89  double res = fmod(_n, 24 * 60 * 60);
90  res /= (60 * 60);
91  return (uint_t)res;
92  }
93 
95  uint_t
96  minutes() const
97  {
98  double res = fmod(_n, 60 * 60);
99  res /= 60;
100  return (uint_t)res;
101  }
102 
104  double
105  seconds() const
106  {
107  return fmod(_n, 60);
108  }
109 
111  virtual Number<double>& set(const String& str);
112 
120  void set(uint_t days, uint_t hours, uint_t minutes, double seconds);
121 
127  void setUnit(uint_t unit, uint_t num = 1);
128 
134  Duration operator+(const Duration& rhs) const
135  {
136  return (double)*this + (double)rhs;
137  }
138 
144  Duration operator-(const Duration& rhs) const
145  {
146  return (double)*this - (double)rhs;
147  }
148 
150  static Duration
151  days(double numDays)
152  {
153  return Duration(numDays * 24.0 * 60.0 * 60.0);
154  }
155 
157  static Duration
158  hours(double numHours)
159  {
160  return Duration(numHours * 60.0 * 60.0);
161  }
162 
164  static Duration
165  minutes(double numMinutes)
166  {
167  return Duration(numMinutes * 60.0);
168  }
169 
171  static Duration
172  seconds(uint64_t numSeconds)
173  {
174  return Duration(numSeconds);
175  }
176 
177 public:
179  static const Duration oneDay;
181  static const Duration oneHour;
183  static const Duration oneHalfHour;
185  static const Duration oneMinute;
187  static const Duration oneSecond;
188 };
189 
191 
192 UTL_NS_END;
Duration(uint_t days, uint_t hours, uint_t minutes, double seconds)
Constructor.
Definition: Duration.h:71
#define UTL_CLASS_DEFID
Default init() and deInit() (which are merely place-holders).
Definition: macros.h:532
String toString(const FwdIt &begin, const FwdIt &end, const String &sep, bool key=false)
Obtain a string representation of a sequence (via Object::toString()).
uint_t minutes() const
Get the number of minutes.
Definition: Duration.h:96
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
double seconds() const
Get the number of seconds.
Definition: Duration.h:105
Numeric value.
Definition: Number.h:23
Character string.
Definition: String.h:31
uint_t hours() const
Get the number of hours.
Definition: Duration.h:87
Duration operator+(const Duration &rhs) const
Add the given duration to self and return the result.
Definition: Duration.h:134
Duration(const String &str)
Constructor.
Definition: Duration.h:59
static const Duration oneDay
Duration of one day.
Definition: Duration.h:179
unsigned long uint64_t
Unsigned 64-bit integer.
Definition: types.h:154
Duration of time.
Definition: Duration.h:40
Duration(double seconds)
Constructor.
Definition: Duration.h:50
Floating point number.
Definition: Float.h:23
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
uint_t days() const
Get the number of days.
Definition: Duration.h:80
static Duration seconds(uint64_t numSeconds)
Get duration in number of seconds.
Definition: Duration.h:172
static Duration days(double numDays)
Get duration in number of days.
Definition: Duration.h:151
static Duration hours(double numHours)
Get duration in number of hours.
Definition: Duration.h:158
static const Duration oneHour
Duration of one hour.
Definition: Duration.h:181
static Duration minutes(double numMinutes)
Get duration in number of minutes.
Definition: Duration.h:165
static const Duration oneMinute
Duration of one minute.
Definition: Duration.h:185
static const Duration oneHalfHour
Duration of half an hour.
Definition: Duration.h:183
static const Duration oneSecond
Duration of one second.
Definition: Duration.h:187
Duration operator-(const Duration &rhs) const
Subtract the given duration from self and return the result.
Definition: Duration.h:144