ML Reference
mlDateTime.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_DATE_TIME_H
14 #define ML_DATE_TIME_H
15 
18 
19 //ML-includes
20 #include "mlUtilsSystem.h"
21 
22 #include <ostream>
23 #include <optional>
24 
25 
26 ML_UTILS_START_NAMESPACE
27 
28 
29 //----------------------------------------------------------------------------------
32 
140 //----------------------------------------------------------------------------------
142 {
143 
144 public:
145 
150 
151 
152  //------------------------------------------------------
155  //------------------------------------------------------
156 
157  DateTime() = default;
158  DateTime(const DateTime&) = default;
159  DateTime& operator=(const DateTime&) = default;
160  DateTime(DateTime&&) = default;
161  DateTime& operator=(DateTime&&) = default;
162 
164  DateTimeIntType hourV = 0, DateTimeIntType minuteV = 0, DateTimeFloatType secondV = 0);
165  DateTime(const char* dtString, const char* format = nullptr);
166  DateTime(DateTimeIntType linearDaysV, DateTimeFloatType linearSecondsV);
167 
169 
170 
171 
172 
173  //------------------------------------------------------
176  //------------------------------------------------------
177 
178  // All set methods return \c true if given date is valid.
179  // Invalid dates are converted to valid dates, so the
180  // return value may be ignored.
181 
182  // Sets both date and time portion.
183  [[nodiscard]] bool set(DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV,
184  DateTimeIntType hourV = 0, DateTimeIntType minuteV = 0, DateTimeFloatType secondV = 0);
185  [[nodiscard]] bool set(const char *dtString, const char *format = nullptr);
186  [[nodiscard]] bool set(DateTimeIntType linDaysV, DateTimeFloatType linSecondsV);
187 
188  // Sets date and time portion independently.
189  [[nodiscard]] bool setYMD(DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV);
190  [[nodiscard]] bool setHMS(DateTimeIntType hourV, DateTimeIntType minuteV, DateTimeFloatType secondV);
191 
192  // Sets to current date/time.
194 
196 
197 
198  //------------------------------------------------------
201  //------------------------------------------------------
202 
203  void get(DateTimeIntType *yearV, DateTimeIntType *monthV, DateTimeIntType *dayV,
204  DateTimeIntType *hourV = nullptr, DateTimeIntType *minuteV = nullptr, DateTimeFloatType *secondV = nullptr) const;
205  void get(char *dtString, const char *format = nullptr) const;
206 
207  void getYMD(DateTimeIntType *yearV, DateTimeIntType *monthV, DateTimeIntType *dayV) const;
208  void getHMS(DateTimeIntType *hourV, DateTimeIntType *minuteV, DateTimeFloatType *secondV) const;
209 
216 
217  // Get day within year in the range 0..364(365)
219 
220  // Get linear day and seconds counters.
221  DateTimeIntType linearDays() const { return _ldays.value_or(0); }
222  DateTimeFloatType linearSeconds() const { return _lseconds.value_or(0); }
223 
224  bool isValid() const;
225 
227 
228 
229  //------------------------------------------------------
232  //------------------------------------------------------
233 
234  // Adds given number of days (subtract if days < 0).
236 
237  // Adds given number of days (subtract if days < 0).
238  // Fractions of a day are allowed.
240 
241  // Adds given number of seconds (subtract if seconds < 0).
243 
244  // Computes time passed since time point \p dateTime, giving the result
245  // in days and fractions of a day.
246  DateTimeFloatType daysSince(const DateTime &dateTime) const;
247 
248  // Computes time passed since time point \p dateTime, giving the result
249  // in seconds and fractions of a second.
250  DateTimeFloatType secondsSince(const DateTime &dateTime) const;
251 
253 
254 
255  //------------------------------------------------------
258  //------------------------------------------------------
259 
260  bool operator==(const DateTime &dt) const;
261  bool operator!=(const DateTime &dt) const;
262  bool operator< (const DateTime &dt) const;
263  bool operator<=(const DateTime &dt) const;
264  bool operator>=(const DateTime &dt) const;
265  bool operator> (const DateTime &dt) const;
266 
268 
269 
270  //------------------------------------------------------
273  //------------------------------------------------------
274 
275  // Tests date/time specification for validity.
276  static bool validYMD(DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV);
277  static bool validHMS(DateTimeIntType hourV, DateTimeIntType minuteV, DateTimeFloatType secondV);
278 
279  // Returns number of days for given month.
281 
282  // Returns number of days for given year.
284 
285  // Returns \c true if given year is a leap year.
286  static bool isLeapYear(DateTimeIntType yearV);
287 
288  // Returns number of days for current month.
290 
291  // Returns number of days for current year.
293 
294  // Returns \c true if current year is a leap year.
295  bool isLeapYear() const;
296 
298 
299 private:
300 
301  void normalize();
302 
303  std::optional<DateTimeIntType> _ldays;
304  std::optional<DateTimeFloatType> _lseconds;
305 };
306 
307 ML_UTILS_END_NAMESPACE
308 
309 
310 //-----------------------------------------------------------------------------------
311 // Stream output for std::ostream
312 //-----------------------------------------------------------------------------------
313 namespace std {
314 
315 //-----------------------------------------------------------------------------------
316 // Stream output for std::ostream
317 //-----------------------------------------------------------------------------------
320  inline ostream& operator<<(ostream& s, const ML_UTILS_NAMESPACE::DateTime &dt)
321  {
322  s << dt.year() << ":"
323  << dt.month() << ":"
324  << dt.day() << ":"
325  << dt.hour() << ":"
326  << dt.minute() << ":"
327  << dt.second();
328  return s;
329  }
330 }
331 
332 #endif // __mlDateTime_H
333 
334 
335 
336 
337 
Class for performing date/time arithmetic, comparisons and formatting.
Definition: mlDateTime.h:142
DateTimeIntType day() const
DateTimeIntType daysOfMonth() const
void getHMS(DateTimeIntType *hourV, DateTimeIntType *minuteV, DateTimeFloatType *secondV) const
DateTimeFloatType daysSince(const DateTime &dateTime) const
bool setHMS(DateTimeIntType hourV, DateTimeIntType minuteV, DateTimeFloatType secondV)
bool isLeapYear() const
DateTime()=default
bool operator!=(const DateTime &dt) const
DateTime(const char *dtString, const char *format=nullptr)
MLdouble DateTimeFloatType
Specify data type for seconds and fractions of other time units.
Definition: mlDateTime.h:149
DateTime & operator=(DateTime &&)=default
bool setYMD(DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV)
DateTime(DateTimeIntType linearDaysV, DateTimeFloatType linearSecondsV)
bool operator>=(const DateTime &dt) const
DateTimeIntType linearDays() const
Definition: mlDateTime.h:221
void get(DateTimeIntType *yearV, DateTimeIntType *monthV, DateTimeIntType *dayV, DateTimeIntType *hourV=nullptr, DateTimeIntType *minuteV=nullptr, DateTimeFloatType *secondV=nullptr) const
void get(char *dtString, const char *format=nullptr) const
DateTime(DateTime &&)=default
DateTime(const DateTime &)=default
DateTimeFloatType linearSeconds() const
Definition: mlDateTime.h:222
DateTimeIntType year() const
bool set(DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV, DateTimeIntType hourV=0, DateTimeIntType minuteV=0, DateTimeFloatType secondV=0)
static bool isLeapYear(DateTimeIntType yearV)
static DateTimeIntType daysOfYear(DateTimeIntType yearV)
void addDays(DateTimeFloatType daysV)
MLint32 DateTimeIntType
Specify data type for year, month, day, hour and minute type as MLint32.
Definition: mlDateTime.h:147
DateTimeIntType month() const
bool set(const char *dtString, const char *format=nullptr)
DateTimeIntType minute() const
bool isValid() const
static DateTimeIntType daysOfMonth(DateTimeIntType monthV, DateTimeIntType yearV)
DateTime & now()
static bool validYMD(DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV)
bool operator<=(const DateTime &dt) const
void getYMD(DateTimeIntType *yearV, DateTimeIntType *monthV, DateTimeIntType *dayV) const
DateTimeIntType hour() const
bool set(DateTimeIntType linDaysV, DateTimeFloatType linSecondsV)
DateTimeIntType daysOfYear() const
DateTimeFloatType secondsSince(const DateTime &dateTime) const
DateTime & operator=(const DateTime &)=default
void addSeconds(DateTimeFloatType secondsV)
bool operator==(const DateTime &dt) const
DateTimeIntType dayInYear() const
void addDays(DateTimeIntType daysV)
DateTimeFloatType second() const
static bool validHMS(DateTimeIntType hourV, DateTimeIntType minuteV, DateTimeFloatType secondV)
DateTime(DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV, DateTimeIntType hourV=0, DateTimeIntType minuteV=0, DateTimeFloatType secondV=0)
MLEXPORT std::ostream & operator<<(std::ostream &s, const ml::Field &v)
Overloads the operator "<<" for stream output of Field objects.
double MLdouble
Definition: mlTypeDefs.h:217
signed int MLint32
Definition: mlTypeDefs.h:161
#define ML_UTILS_EXPORT
Defines platform dependent DLL export macro for mlUtils.
Definition: mlUtilities.h:20