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