MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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
27
28
29//----------------------------------------------------------------------------------
32
140//----------------------------------------------------------------------------------
142{
143
144public:
149
150
151 //------------------------------------------------------
154 //------------------------------------------------------
155
156 DateTime() = default;
157 DateTime(const DateTime&) = default;
158 DateTime& operator=(const DateTime&) = default;
159 DateTime(DateTime&&) = default;
161
164 DateTime(const char* dtString, const char* format = nullptr);
166
168
169
170 //------------------------------------------------------
173 //------------------------------------------------------
174
175 // All set methods return \c true if the given date is valid.
176 // Invalid dates are converted to valid dates, so the
177 // return value may be ignored.
178
179 // Sets both date and time portion.
182 [[nodiscard]] bool set(const char* dtString, const char* format = nullptr);
184
185 // Sets date and time portion independently.
188
189 // Sets to current date/time.
191
193
194
195 //------------------------------------------------------
198 //------------------------------------------------------
199
201 DateTimeIntType* minuteV = nullptr, DateTimeFloatType* secondV = nullptr) const;
202 void get(char* dtString, const char* format = nullptr) const;
203
206
213
214 // Returns days within year in the range 0..364 (or 365 for a leap year).
216
217 // Returns linear day and seconds counters.
219 {
220 return _ldays.value_or(0);
221 }
223 {
224 return _lseconds.value_or(0);
225 }
226
227 bool isValid() const;
228
230
231
232 //------------------------------------------------------
235 //------------------------------------------------------
236
237 // Adds given number of days (subtract if days < 0).
239
240 // Adds given number of days (subtract if days < 0).
241 // Fractions of a day are allowed.
243
244 // Adds given number of seconds (subtract if seconds < 0).
246
247 // Computes time passed since time point \p dateTime, giving the result
248 // in days and fractions of a day.
250
251 // Computes time passed since time point \p dateTime, giving the result
252 // in seconds and fractions of a second.
254
256
257
258 //------------------------------------------------------
261 //------------------------------------------------------
262
263 bool operator==(const DateTime& dt) const;
264 bool operator!=(const DateTime& dt) const;
265 bool operator<(const DateTime& dt) const;
266 bool operator<=(const DateTime& dt) const;
267 bool operator>=(const DateTime& dt) const;
268 bool operator>(const DateTime& dt) const;
269
271
272
273 //------------------------------------------------------
276 //------------------------------------------------------
277
278 // Tests date/time specification for validity.
281
282 // Returns the number of days for a given month.
284
285 // Returns the number of days for a given year.
287
288 // Returns \c true if the given year is a leap year.
290
291 // Returns the number of days for the current month.
293
294 // Returns the number of days for the current year.
296
297 // Returns \c true if the current year is a leap year.
298 bool isLeapYear() const;
299
301
302private:
303 void normalize();
304
305 std::optional<DateTimeIntType> _ldays;
306 std::optional<DateTimeFloatType> _lseconds;
307};
308
310
311
312//-----------------------------------------------------------------------------------
313// Stream output for std::ostream
314//-----------------------------------------------------------------------------------
315namespace std
316{
317
318 //-----------------------------------------------------------------------------------
319 // Stream output for std::ostream
320 //-----------------------------------------------------------------------------------
323 inline ostream& operator<<(ostream& s, const ML_UTILS_NAMESPACE::DateTime& dt)
324 {
325 s << dt.year() << ":" << dt.month() << ":" << dt.day() << ":" << dt.hour() << ":" << dt.minute() << ":"
326 << dt.second();
327 return s;
328 }
329} // namespace std
330
331#endif // __mlDateTime_H
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
Specifies data type for seconds and fractions of other time units.
Definition mlDateTime.h:148
DateTime & now()
bool setYMD(DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV)
DateTime(DateTimeIntType linearDaysV, DateTimeFloatType linearSecondsV)
bool operator>=(const DateTime &dt) const
bool operator<(const DateTime &dt) const
DateTimeIntType linearDays() const
Definition mlDateTime.h:218
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
Specifies data type for year, month, day, hour, and minute type as MLint32.
Definition mlDateTime.h:146
DateTimeIntType month() const
bool set(const char *dtString, const char *format=nullptr)
DateTimeIntType minute() const
bool isValid() const
DateTime & operator=(DateTime &&)=default
static DateTimeIntType daysOfMonth(DateTimeIntType monthV, DateTimeIntType yearV)
bool operator>(const DateTime &dt) const
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
void addSeconds(DateTimeFloatType secondsV)
bool operator==(const DateTime &dt) const
DateTimeIntType dayInYear() const
void addDays(DateTimeIntType daysV)
DateTime & operator=(const DateTime &)=default
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)
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
double MLdouble
Definition mlTypeDefs.h:217
signed int MLint32
Definition mlTypeDefs.h:161
#define ML_UTILS_EXPORT
Definition mlUtilities.h:18
STL namespace.
MLEXPORT std::ostream & operator<<(std::ostream &s, const ml::Field &v)
Overloads the operator '<<' for stream output of Field objects.