OpenMS
Loading...
Searching...
No Matches
DateTime.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Timo Sachsenberg $
6// $Authors: Nico Pfeifer $
7// --------------------------------------------------------------------------
8
9#pragma once
10
14#include <OpenMS/OpenMSConfig.h>
15
16#include <functional>
17#include <memory> // unique_ptr
18#include <string>
19
20// foward declarations
21class QDateTime;
22
23namespace OpenMS
24{
25
34 class OPENMS_DLLAPI DateTime
35 {
36public:
37
44
46 DateTime(const DateTime& date);
47
49 DateTime(DateTime&&) noexcept;
50
52 DateTime& operator=(const DateTime& source);
53
55 DateTime& operator=(DateTime&&) & noexcept;
56
59
61 bool operator==(const DateTime& rhs) const;
62
64 bool operator!=(const DateTime& rhs) const;
65
67 bool operator<(const DateTime& rhs) const;
68
76 void setDate(const String& date);
77
85 void setTime(const String& date);
86
94 void setDate(UInt month, UInt day, UInt year);
95
103 void setTime(UInt hour, UInt minute, UInt second);
104
112 void set(UInt month, UInt day, UInt year, UInt hour, UInt minute, UInt second);
113
119 void get(UInt& month, UInt& day, UInt& year, UInt& hour, UInt& minute, UInt& second) const;
120
126 void getDate(UInt& month, UInt& day, UInt& year) const;
127
133 String getDate() const;
134
140 void getTime(UInt& hour, UInt& minute, UInt& second) const;
141
142 // add @param[in] s seconds to date time
143 DateTime& addSecs(int s);
144
150 String getTime() const;
151
153 static DateTime now();
154
156 static DateTime nowUTC();
157
159 bool isValid() const;
160
162 bool isNull() const;
163
165 void clear();
166
167 /* @brief Returns a string representation of the DateTime object.
168 @param[in] format "yyyy-MM-ddThh:mm:ss" corresponds to ISO 8601 and should be preferred.
169 */
170 String toString(const std::string& format = "yyyy-MM-ddThh:mm:ss") const;
171
172 /* @brief Creates a DateTime object from string representation.
173 @param[in] format "yyyy-MM-ddThh:mm:ss" corresponds to ISO 8601 and should be preferred.
174 */
175 static DateTime fromString(const std::string& date, const std::string& format = "yyyy-MM-ddThh:mm:ss");
176
182 String get() const;
183
197 void set(const String& date);
198
199 private:
200 std::unique_ptr<QDateTime> dt_; // use PImpl, to avoid costly #include
201 };
202
203} // namespace OpenMS
204
205// Hash function specialization for DateTime
206namespace std
207{
208 template<>
209 struct hash<OpenMS::DateTime>
210 {
211 std::size_t operator()(const OpenMS::DateTime& dt) const noexcept
212 {
213 // Hash the date/time components including milliseconds to match operator==
214 // (which compares the underlying QDateTime including milliseconds)
215 // Use toString with millisecond format and convert to std::string for hashing
216 std::string datetime_str = dt.toString("yyyy-MM-ddThh:mm:ss.zzz");
217 return OpenMS::fnv1a_hash_string(datetime_str);
218 }
219 };
220} // namespace std
DateTime Class.
Definition DateTime.h:35
DateTime()
Default constructor.
DateTime(DateTime &&) noexcept
Move constructor.
DateTime(const DateTime &date)
Copy constructor.
A more convenient string class.
Definition String.h:34
unsigned int UInt
Unsigned integer type.
Definition Types.h:64
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::size_t fnv1a_hash_string(const std::string &s) noexcept
FNV-1a hash for a string.
Definition HashUtils.h:70
STL namespace.
std::size_t operator()(const OpenMS::DateTime &dt) const noexcept
Definition DateTime.h:211