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 bool isValid() const;
157
159 bool isNull() const;
160
162 void clear();
163
164 /* @brief Returns a string representation of the DateTime object.
165 @param[in] format "yyyy-MM-ddThh:mm:ss" corresponds to ISO 8601 and should be preferred.
166 */
167 String toString(const std::string& format = "yyyy-MM-ddThh:mm:ss") const;
168
169 /* @brief Creates a DateTime object from string representation.
170 @param[in] format "yyyy-MM-ddThh:mm:ss" corresponds to ISO 8601 and should be preferred.
171 */
172 static DateTime fromString(const std::string& date, const std::string& format = "yyyy-MM-ddThh:mm:ss");
173
179 String get() const;
180
194 void set(const String& date);
195
196 private:
197 std::unique_ptr<QDateTime> dt_; // use PImpl, to avoid costly #include
198 };
199
200} // namespace OpenMS
201
202// Hash function specialization for DateTime
203namespace std
204{
205 template<>
206 struct hash<OpenMS::DateTime>
207 {
208 std::size_t operator()(const OpenMS::DateTime& dt) const noexcept
209 {
210 // Hash the date/time components including milliseconds to match operator==
211 // (which compares the underlying QDateTime including milliseconds)
212 // Use toString with millisecond format and convert to std::string for hashing
213 std::string datetime_str = dt.toString("yyyy-MM-ddThh:mm:ss.zzz");
214 return OpenMS::fnv1a_hash_string(datetime_str);
215 }
216 };
217} // 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:208