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 <string>
18
19namespace OpenMS
20{
21
30 class OPENMS_DLLAPI DateTime
31 {
32public:
33
40
42 DateTime(const DateTime& date) = default;
43
45 DateTime(DateTime&&) noexcept = default;
46
48 DateTime& operator=(const DateTime& source) = default;
49
51 DateTime& operator=(DateTime&&) & noexcept = default;
52
54 ~DateTime() = default;
55
57 bool operator==(const DateTime& rhs) const;
58
60 bool operator!=(const DateTime& rhs) const;
61
63 bool operator<(const DateTime& rhs) const;
64
72 void setDate(const String& date);
73
81 void setTime(const String& date);
82
90 void setDate(UInt month, UInt day, UInt year);
91
99 void setTime(UInt hour, UInt minute, UInt second);
100
108 void set(UInt month, UInt day, UInt year, UInt hour, UInt minute, UInt second);
109
115 void get(UInt& month, UInt& day, UInt& year, UInt& hour, UInt& minute, UInt& second) const;
116
122 void getDate(UInt& month, UInt& day, UInt& year) const;
123
129 String getDate() const;
130
136 void getTime(UInt& hour, UInt& minute, UInt& second) const;
137
138 // add @param[in] s seconds to date time
139 DateTime& addSecs(int s);
140
146 String getTime() const;
147
149 static DateTime now();
150
152 static DateTime nowUTC();
153
155 bool isValid() const;
156
158 bool isNull() const;
159
161 void clear();
162
163 /* @brief Returns a string representation of the DateTime object.
164 @param[in] format "yyyy-MM-ddThh:mm:ss" corresponds to ISO 8601 and should be preferred.
165 */
166 String toString(const std::string& format = "yyyy-MM-ddThh:mm:ss") const;
167
168 /* @brief Creates a DateTime object from string representation.
169 @param[in] format "yyyy-MM-ddThh:mm:ss" corresponds to ISO 8601 and should be preferred.
170 */
171 static DateTime fromString(const std::string& date, const std::string& format = "yyyy-MM-ddThh:mm:ss");
172
178 String get() const;
179
193 void set(const String& date);
194
195 private:
196 struct Fields {
197 int year = 0, month = 0, day = 0;
198 int hour = 0, minute = 0, second = 0, millisecond = 0;
199 bool valid = false;
200 } fields_;
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 std::string datetime_str = dt.toString("yyyy-MM-ddThh:mm:ss.zzz");
215 return OpenMS::fnv1a_hash_string(datetime_str);
216 }
217 };
218} // namespace std
DateTime Class.
Definition DateTime.h:31
DateTime()
Default constructor.
DateTime(DateTime &&) noexcept=default
Move constructor.
DateTime(const DateTime &date)=default
Copy constructor.
Definition DateTime.h:196
A more convenient string class.
Definition String.h:32
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