MeVisLab Toolbox 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 #if ML_DEPRECATED_SINCE(3,5,0)
225  // Test for zero date/time.
226  // Returns NULL if date is zero. Returned pointer is not intended to be referenced.
227  [[deprecated]] operator void *() const; // Return NULL if date is zero
228  [[deprecated]] bool operator !() const; // Return true if date is zero
229 #endif
230 
231  bool isValid() const;
232 
234 
235 
236  //------------------------------------------------------
239  //------------------------------------------------------
240 
241  // Adds given number of days (subtract if days < 0).
243 
244  // Adds given number of days (subtract if days < 0).
245  // Fractions of a day are allowed.
247 
248  // Adds given number of seconds (subtract if seconds < 0).
250 
251  // Computes time passed since time point \p dateTime, giving the result
252  // in days and fractions of a day.
253  DateTimeFloatType daysSince(const DateTime &dateTime) const;
254 
255  // Computes time passed since time point \p dateTime, giving the result
256  // in seconds and fractions of a second.
257  DateTimeFloatType secondsSince(const DateTime &dateTime) const;
258 
260 
261 
262  //------------------------------------------------------
265  //------------------------------------------------------
266 
267  bool operator==(const DateTime &dt) const;
268  bool operator!=(const DateTime &dt) const;
269  bool operator< (const DateTime &dt) const;
270  bool operator<=(const DateTime &dt) const;
271  bool operator>=(const DateTime &dt) const;
272  bool operator> (const DateTime &dt) const;
273 
275 
276 
277  //------------------------------------------------------
280  //------------------------------------------------------
281 
282  // Tests date/time specification for validity.
283  static bool validYMD(DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV);
284  static bool validHMS(DateTimeIntType hourV, DateTimeIntType minuteV, DateTimeFloatType secondV);
285 
286  // Returns number of days for given month.
288 
289  // Returns number of days for given year.
291 
292  // Returns \c true if given year is a leap year.
293  static bool isLeapYear(DateTimeIntType yearV);
294 
295  // Returns number of days for current month.
297 
298  // Returns number of days for current year.
300 
301  // Returns \c true if current year is a leap year.
302  bool isLeapYear() const;
303 
305 
306 private:
307 
308  void normalize();
309 
310  std::optional<DateTimeIntType> _ldays;
311  std::optional<DateTimeFloatType> _lseconds;
312 
313 
314 #if ML_DEPRECATED_SINCE(3,5,0)
315 
318 
319 public:
320 
323  typedef MLint32 DTIType;
326  typedef MLdouble DTFType;
329  inline ML_DEPRECATED DateTimeIntType linDays() const { return linearDays(); }
332  inline ML_DEPRECATED DateTimeFloatType linSeconds() const { return linearSeconds(); }
333 
335 
336 #endif
337 };
338 
339 ML_UTILS_END_NAMESPACE
340 
341 
342 //-----------------------------------------------------------------------------------
343 // Stream output for std::ostream
344 //-----------------------------------------------------------------------------------
345 namespace std {
346 
347 //-----------------------------------------------------------------------------------
348 // Stream output for std::ostream
349 //-----------------------------------------------------------------------------------
352  inline ostream& operator<<(ostream& s, const ML_UTILS_NAMESPACE::DateTime &dt)
353  {
354  s << dt.year() << ":"
355  << dt.month() << ":"
356  << dt.day() << ":"
357  << dt.hour() << ":"
358  << dt.minute() << ":"
359  << dt.second();
360  return s;
361  }
362 }
363 
364 #endif // __mlDateTime_H
365 
366 
367 
368 
369 
#define ML_DEPRECATED
Definition: CSOGroup.h:371
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:223
signed int MLint32
Definition: mlTypeDefs.h:167
#define ML_UTILS_EXPORT
Defines platform dependent DLL export macro for mlUtils.
Definition: mlUtilities.h:20
std::pair< Date, Time > DateTime
DateTime.
Definition: DCMTree_Lib.h:201
bool operator!(const FloatingPointVector< T, size, DataContainer > &a)
Returns whether all components are 0.