OTB  9.0.0
Orfeo Toolbox
otbDateTime.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbDateTime_h
22 #define otbDateTime_h
23 
24 #include "OTBMetadataExport.h"
25 
26 #include <cmath>
27 #include <boost/operators.hpp>
28 #include <chrono>
29 #include <string>
30 
31 namespace otb
32 {
33 namespace MetaData
34 {
35 
36 namespace details
37 {
38 using InternalDurationType = std::chrono::nanoseconds;
39 using InternalTimePointType = std::chrono::time_point<std::chrono::system_clock, InternalDurationType>;
40 
41 // the number of second's fractions per tick of InternalDurationType
42 constexpr double internalPeriod = static_cast<double>(InternalDurationType::period::num) / InternalDurationType::period::den;
43 
44 // Standard format used when parsing dates
45 const std::string timePointFormat = "%Y-%m-%dT%H:%M:%S";
46 
47 // Add some operators not defined in boost
48 
49 // division by a scalar and ratio
50 template <typename T, typename R> struct dividable
51 {
52  typedef R scalar_type;
53  friend T operator/(T lhs, scalar_type const& rhs)
54  {
55  lhs /= rhs;
56  return lhs;
57  }
58  friend scalar_type operator/(T const& lhs, T const& rhs)
59  {
60  return Ratio(lhs, rhs);
61  }
62 };
63 
64 template <typename U, typename V> struct substractable_asym
65 {
66  friend U operator-(V const& lhs, V const& rhs)
67  {
68  return V::template diff<U,V>(lhs, rhs);
69  }
70 };
71 
72 template <typename T> struct streamable
73 {
74  friend std::ostream & operator<<(std::ostream & os, const T & v)
75  {
76  return v.Display(os);
77  }
78  friend std::istream & operator>>(std::istream & is, T & v)
79  {
80  return v.Read(is);
81  }
82 };
83 } // namespace details
84 
85 
86 class Duration;
87 
94 class OTBMetadata_EXPORT TimePoint : private details::streamable<TimePoint>,
95  private details::substractable_asym<Duration, TimePoint>,
96  private boost::equality_comparable<TimePoint>,
97  private boost::less_than_comparable<TimePoint>
98 {
99 public:
101 
102  TimePoint(): m_Time(InternalTimePointType::min()) {}
103  TimePoint(InternalTimePointType const& t): m_Time(t) {}
104 
105  std::ostream & Display(std::ostream & os) const;
106  std::istream & Read (std::istream & is, const std::string & format = details::timePointFormat);
107 
108  template <typename U, typename V> static U diff(V const& lhs, V const& rhs)
109  {
110  U const res(lhs.m_Time - rhs.m_Time);
111  return res;
112  }
113 
114  friend bool operator < (TimePoint const& lhs, TimePoint const& rhs)
115  {
116  return lhs.m_Time < rhs.m_Time;
117  }
118 
119  friend bool operator == (TimePoint const& lhs, TimePoint const& rhs)
120  {
121  return lhs.m_Time == rhs.m_Time;
122  }
123 
125  double GetJulianDay() const;
126 
128  double GetModifiedJulianDay() const;
129 
131  int GetYear() const;
132 
134  unsigned int GetMonth() const;
135 
137  unsigned int GetDay() const;
138 
140  unsigned int GetHour() const;
141 
143  unsigned int GetMinute() const;
144 
146  double GetSecond() const;
147 
148  friend TimePoint& operator+=(TimePoint & u, Duration const& v);
149  friend TimePoint& operator-=(TimePoint & u, Duration const& v);
150 
151 private:
153 
154 };
155 
162 class OTBMetadata_EXPORT Duration : private details::streamable<Duration>,
163  private boost::addable<Duration>,
164  private boost::subtractable<Duration>,
165  private boost::multipliable2<Duration, double>,
166  private details::dividable<Duration, double>,
167  private boost::equality_comparable<Duration>,
168  private boost::less_than_comparable<Duration>,
169  private boost::addable<TimePoint, Duration>,
170  private boost::subtractable<TimePoint, Duration>
171 {
172 public:
174 
175  Duration() = default;
176  Duration(InternalDurationType const& d): m_Duration(d) {}
177 
178  static Duration Seconds(double s)
179  {
180  return Duration(InternalDurationType(static_cast<InternalDurationType::rep>(std::round(s / details::internalPeriod))));
181  }
182 
183  static Duration Milliseconds(double ms)
184  {
185  return Duration(InternalDurationType(static_cast<InternalDurationType::rep>(std::round(ms / details::internalPeriod * 1e-3))));
186  }
187 
188  static Duration Microseconds(double us)
189  {
190  return Duration(InternalDurationType(static_cast<InternalDurationType::rep>(std::round(us / details::internalPeriod * 1e-6))));
191  }
192 
193  static Duration Nanoseconds(double ns)
194  {
195  return Duration(InternalDurationType(static_cast<InternalDurationType::rep>(std::round(ns / details::internalPeriod * 1e-9))));
196  }
197 
198  double TotalSeconds() const;
199 
200  InternalDurationType::rep NumberOfTicks() const;
201 
202  friend Duration& operator+=(Duration & u, Duration const& v)
203  {
204  u.m_Duration += v.m_Duration;
205  return u;
206  }
207 
208  friend Duration& operator-=(Duration & u, Duration const& v)
209  {
210  u.m_Duration -= v.m_Duration;
211  return u;
212  }
213 
214  friend Duration& operator*=(Duration & u, double v)
215  {
216  // note: don't use std::chrono::duration::operator* cause it multiplies the input by an integer value.
217  u.m_Duration = InternalDurationType(static_cast<InternalDurationType::rep>(std::round(u.m_Duration.count() * v)));
218  return u;
219  }
220 
221  friend Duration& operator/=(Duration & u, double v)
222  {
223  // note: don't use std::chrono::duration::operator/ cause it divides the input by an integer value.
224  u.m_Duration = InternalDurationType(static_cast<InternalDurationType::rep>(std::round(u.m_Duration.count() / v)));
225  return u;
226  }
227 
228  friend bool operator < (Duration const& lhs, Duration const& rhs)
229  {
230  return lhs.m_Duration < rhs.m_Duration;
231  }
232 
233  friend bool operator == (Duration const& lhs, Duration const& rhs)
234  {
235  return lhs.m_Duration == rhs.m_Duration;
236  }
237 
238  friend TimePoint& operator+=(TimePoint & u, Duration const& v)
239  {
240  u.m_Time += v.m_Duration;
241  return u;
242  }
243 
244  friend TimePoint& operator-=(TimePoint & u, Duration const& v)
245  {
246  u.m_Time -= v.m_Duration;
247  return u;
248  }
249 
250  std::ostream & Display(std::ostream & os) const;
251  std::istream & Read (std::istream & is);
252 
253  friend OTBMetadata_EXPORT Duration Abs(Duration d);
254 
255 private:
257 };
258 
259 OTBMetadata_EXPORT double Ratio(const Duration & lhs, const Duration & rhs);
260 OTBMetadata_EXPORT TimePoint ReadFormattedDate(const std::string & dateStr, const std::string & format = details::timePointFormat);
261 
262 } // namespace otb
263 } // namespace MetaData
264 
265 #endif // otbDateTime_h
otb::MetaData::Duration::m_Duration
InternalDurationType m_Duration
Definition: otbDateTime.h:256
otb::MetaData::details::internalPeriod
constexpr double internalPeriod
Definition: otbDateTime.h:42
otb::MetaData::details::dividable::operator/
friend scalar_type operator/(T const &lhs, T const &rhs)
Definition: otbDateTime.h:58
otb::MetaData::TimePoint::TimePoint
TimePoint(InternalTimePointType const &t)
Definition: otbDateTime.h:103
otb::MetaData::Duration::operator-=
friend TimePoint & operator-=(TimePoint &u, Duration const &v)
Definition: otbDateTime.h:244
otb::MetaData::details::streamable::operator>>
friend std::istream & operator>>(std::istream &is, T &v)
Definition: otbDateTime.h:78
otb::MetaData::details::substractable_asym::operator-
friend U operator-(V const &lhs, V const &rhs)
Definition: otbDateTime.h:66
otb::MetaData::Duration::Seconds
static Duration Seconds(double s)
Definition: otbDateTime.h:178
otb::MetaData::Duration::Milliseconds
static Duration Milliseconds(double ms)
Definition: otbDateTime.h:183
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::MetaData::TimePoint::TimePoint
TimePoint()
Definition: otbDateTime.h:102
otb::operator==
constexpr bool operator==(extents< StaticExtentsL... > const &lhs, extents< StaticExtentsR... > const &rhs)
Definition: otbExtents.h:150
otb::MetaData::details::InternalTimePointType
std::chrono::time_point< std::chrono::system_clock, InternalDurationType > InternalTimePointType
Definition: otbDateTime.h:39
otb::MetaData::Duration::operator*=
friend Duration & operator*=(Duration &u, double v)
Definition: otbDateTime.h:214
otb::MetaData::details::timePointFormat
const std::string timePointFormat
Definition: otbDateTime.h:45
otb::MetaData::TimePoint::InternalTimePointType
details::InternalTimePointType InternalTimePointType
Definition: otbDateTime.h:100
otb::MetaData::TimePoint::m_Time
InternalTimePointType m_Time
Definition: otbDateTime.h:152
otb::MetaData::Duration::Microseconds
static Duration Microseconds(double us)
Definition: otbDateTime.h:188
otb::MetaData::Duration::Duration
Duration(InternalDurationType const &d)
Definition: otbDateTime.h:176
otb::MetaData::Duration::InternalDurationType
details::InternalDurationType InternalDurationType
Definition: otbDateTime.h:173
otb::MetaData::Duration::operator+=
friend Duration & operator+=(Duration &u, Duration const &v)
Definition: otbDateTime.h:202
otb::MetaData::TimePoint::diff
static U diff(V const &lhs, V const &rhs)
Definition: otbDateTime.h:108
otb::Wrapper::XML::Read
OTBApplicationEngine_EXPORT int Read(const std::string &filename, Application::Pointer application)
otb::MetaData::Duration::operator-=
friend Duration & operator-=(Duration &u, Duration const &v)
Definition: otbDateTime.h:208
otb::MetaData::details::InternalDurationType
std::chrono::nanoseconds InternalDurationType
Definition: otbDateTime.h:38
otb::MetaData::ReadFormattedDate
OTBMetadata_EXPORT TimePoint ReadFormattedDate(const std::string &dateStr, const std::string &format=details::timePointFormat)
operator<
bool operator<(std::tm x, std::tm y)
Compare 2 dates.
otb::MetaData::details::streamable::operator<<
friend std::ostream & operator<<(std::ostream &os, const T &v)
Definition: otbDateTime.h:74
otb::MetaData::Duration::operator/=
friend Duration & operator/=(Duration &u, double v)
Definition: otbDateTime.h:221
otb::MetaData::Duration::Nanoseconds
static Duration Nanoseconds(double ns)
Definition: otbDateTime.h:193
otb::MetaData::Duration::operator+=
friend TimePoint & operator+=(TimePoint &u, Duration const &v)
Definition: otbDateTime.h:238
otb::MetaData::Ratio
OTBMetadata_EXPORT double Ratio(const Duration &lhs, const Duration &rhs)
otb::MetaData::TimePoint
Represents a point in Time.
Definition: otbDateTime.h:94
otb::MetaData::details::streamable
Definition: otbDateTime.h:72
otb::MetaData::details::substractable_asym
Definition: otbDateTime.h:64
otb::MetaData::details::dividable
Definition: otbDateTime.h:50
otb::MetaData::Duration
Represents a duration.
Definition: otbDateTime.h:162
otb::MetaData::details::dividable::scalar_type
R scalar_type
Definition: otbDateTime.h:52
otb::MetaData::details::dividable::operator/
friend T operator/(T lhs, scalar_type const &rhs)
Definition: otbDateTime.h:53