| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | /** @file | ||
| 30 | * @brief Time and Timecounter | ||
| 31 | * @ingroup gatecore_cpp | ||
| 32 | */ | ||
| 33 | |||
| 34 | #ifndef GATE_TIMES_HPP_INCLUDED | ||
| 35 | #define GATE_TIMES_HPP_INCLUDED | ||
| 36 | |||
| 37 | #include "gate/gate_core_api.hpp" | ||
| 38 | #include "gate/gatetypes.hpp" | ||
| 39 | #include "gate/strings.hpp" | ||
| 40 | #include "gate/times.h" | ||
| 41 | #include "gate/numbers.hpp" | ||
| 42 | #include "gate/exceptions.hpp" | ||
| 43 | |||
| 44 | namespace gate | ||
| 45 | { | ||
| 46 | typedef ::gate_timestamp_t timestamp_t; | ||
| 47 | typedef ::gate_timecounter_t timecounter_t; | ||
| 48 | |||
| 49 | |||
| 50 | namespace time | ||
| 51 | { | ||
| 52 | typedef int64_t duration_value_t; | ||
| 53 | |||
| 54 | namespace details | ||
| 55 | { | ||
| 56 | struct TypeAttributeHour { static duration_value_t const microsecondsFactor = 3600000000LL; }; | ||
| 57 | struct TypeAttributeMinute { static duration_value_t const microsecondsFactor = 60000000LL; }; | ||
| 58 | struct TypeAttributeSecond { static duration_value_t const microsecondsFactor = 1000000LL; }; | ||
| 59 | struct TypeAttributeMillisecond { static duration_value_t const microsecondsFactor = 1000LL; }; | ||
| 60 | struct TypeAttributeMicrosecond { static duration_value_t const microsecondsFactor = 1LL; }; | ||
| 61 | |||
| 62 | } // details | ||
| 63 | |||
| 64 | |||
| 65 | typedef Number<duration_value_t, details::TypeAttributeHour> Hours; | ||
| 66 | typedef Number<duration_value_t, details::TypeAttributeMinute> Minutes; | ||
| 67 | typedef Number<duration_value_t, details::TypeAttributeSecond> Seconds; | ||
| 68 | typedef Number<duration_value_t, details::TypeAttributeMillisecond> Milliseconds; | ||
| 69 | typedef Number<duration_value_t, details::TypeAttributeMicrosecond> Microseconds; | ||
| 70 | |||
| 71 | namespace details | ||
| 72 | { | ||
| 73 | template<class TO, class FROM> struct DurationConverter | ||
| 74 | { | ||
| 75 | 19 | static TO convert(FROM const& from) | |
| 76 | { | ||
| 77 | 19 | return TO(from.value() * FROM::attrib_t::microsecondsFactor / TO::attrib_t::microsecondsFactor); | |
| 78 | } | ||
| 79 | }; | ||
| 80 | } // details | ||
| 81 | |||
| 82 | 10 | template<class TO, class FROM> TO duration_cast(FROM const& from) | |
| 83 | { | ||
| 84 | 10 | return details::DurationConverter<TO, FROM>::convert(from); | |
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | struct Time; | ||
| 90 | struct DateTime; | ||
| 91 | |||
| 92 | |||
| 93 | /// @brief System time class (timestamp + UTC bias) | ||
| 94 | struct GATE_CORE_CPP_API Time : public gate_time_t | ||
| 95 | { | ||
| 96 | typedef gate_time_t base_t; | ||
| 97 | |||
| 98 | static Time now(); /**< Creates time from current system time */ | ||
| 99 | static Result<Time> tryParse(String const& text); /**< Creates time by parsing ISO8601 time string */ | ||
| 100 | static Time parse(String const& text); /**< Creates time by parsing ISO8601 time string */ | ||
| 101 | static Time fromUnix(int64_t unixSeconds); /**< Creates time from an UNIX timestamp */ | ||
| 102 | static Time fromTimestamp(gate_timestamp_t source); /**< Creates time from GATE timestamp */ | ||
| 103 | |||
| 104 | Time(timestamp_t microsince1601 = 0, int32_t biasminutes = 0); /**< Initializes Time object */ | ||
| 105 | Time(base_t const& src); | ||
| 106 | |||
| 107 | DateTime toDateTime() const; /**< Converts time to gregorian date-time */ | ||
| 108 | String toString() const; /**< Prints time as string */ | ||
| 109 | String toString(String const& format) const; /**< Prints time as formated string */ | ||
| 110 | int64_t toUnix() const; /**< Converts time into UNIX timestamp */ | ||
| 111 | |||
| 112 | Time add(int64_t microseconds) const; /**< Adds microseconds to current time and returns new Time object */ | ||
| 113 | int64_t diff(Time const& t) const; /**< Returns difference between current and given time in microseconds */ | ||
| 114 | bool_t empty() const; | ||
| 115 | |||
| 116 | time::Microseconds operator-(Time const& t) const; | ||
| 117 | bool operator!() const; | ||
| 118 | |||
| 119 | ✗ | template<class A> Time operator+(Number<time::duration_value_t, A> const& duration) | |
| 120 | { | ||
| 121 | ✗ | return this->add(duration.value() * A::microsecondsFactor); | |
| 122 | } | ||
| 123 | template<class A> Time operator-(Number<time::duration_value_t, A> const& duration) | ||
| 124 | { | ||
| 125 | return this->add(-duration.value() * A::microsecondsFactor); | ||
| 126 | } | ||
| 127 | |||
| 128 | template<class A> Time& operator+=(Number<time::duration_value_t, A> const& duration) | ||
| 129 | { | ||
| 130 | return *this = (*this + duration); | ||
| 131 | } | ||
| 132 | template<class A> Time& operator-=(Number<time::duration_value_t, A> const& duration) | ||
| 133 | { | ||
| 134 | return *this = (*this - duration); | ||
| 135 | } | ||
| 136 | |||
| 137 | bool operator==(gate_timestamp_t const& src) const; | ||
| 138 | bool operator!=(gate_timestamp_t const& src) const; | ||
| 139 | bool operator< (gate_timestamp_t const& src) const; | ||
| 140 | bool operator<=(gate_timestamp_t const& src) const; | ||
| 141 | bool operator> (gate_timestamp_t const& src) const; | ||
| 142 | bool operator>=(gate_timestamp_t const& src) const; | ||
| 143 | |||
| 144 | bool operator==(Time const& src) const; | ||
| 145 | bool operator!=(Time const& src) const; | ||
| 146 | bool operator< (Time const& src) const; | ||
| 147 | bool operator<=(Time const& src) const; | ||
| 148 | bool operator> (Time const& src) const; | ||
| 149 | bool operator>=(Time const& src) const; | ||
| 150 | }; | ||
| 151 | |||
| 152 | |||
| 153 | /// @brief Gregorian Date class (year-month-day) | ||
| 154 | struct GATE_CORE_CPP_API Date : public gate_date_t | ||
| 155 | { | ||
| 156 | enum MonthEnum | ||
| 157 | { | ||
| 158 | Jan = GATE_DATE_JAN, | ||
| 159 | Feb = GATE_DATE_FEB, | ||
| 160 | Mar = GATE_DATE_MAR, | ||
| 161 | Apr = GATE_DATE_APR, | ||
| 162 | May = GATE_DATE_MAY, | ||
| 163 | Jun = GATE_DATE_JUN, | ||
| 164 | Jul = GATE_DATE_JUL, | ||
| 165 | Aug = GATE_DATE_AUG, | ||
| 166 | Sep = GATE_DATE_SEP, | ||
| 167 | Oct = GATE_DATE_OCT, | ||
| 168 | Nov = GATE_DATE_NOV, | ||
| 169 | Dec = GATE_DATE_DEC, | ||
| 170 | |||
| 171 | January = Jan, | ||
| 172 | February = Feb, | ||
| 173 | March = Mar, | ||
| 174 | April = Apr, | ||
| 175 | June = Jun, | ||
| 176 | July = Jul, | ||
| 177 | August = Aug, | ||
| 178 | September = Sep, | ||
| 179 | October = Oct, | ||
| 180 | November = Nov, | ||
| 181 | December = Dec | ||
| 182 | }; | ||
| 183 | |||
| 184 | Date(uint16_t years = 1601, uint8_t months = 1, uint8_t days = 1); /**< Initializes date */ | ||
| 185 | Date(uint16_t years, MonthEnum months, uint8_t days = 1); /**< Initializes date */ | ||
| 186 | Date(gate_date_t const& date); | ||
| 187 | |||
| 188 | int compare(Date const& that) const; | ||
| 189 | |||
| 190 | bool_t operator< (Date const& that) const; | ||
| 191 | bool_t operator<=(Date const& that) const; | ||
| 192 | bool_t operator> (Date const& that) const; | ||
| 193 | bool_t operator>=(Date const& that) const; | ||
| 194 | bool_t operator==(Date const& that) const; | ||
| 195 | bool_t operator!=(Date const& that) const; | ||
| 196 | }; | ||
| 197 | |||
| 198 | |||
| 199 | /// @brief Daytime class (hour-minute-second-microsecond) | ||
| 200 | struct GATE_CORE_CPP_API DayTime : public gate_daytime_t | ||
| 201 | { | ||
| 202 | DayTime(uint8_t hours = 0, uint8_t minutes = 0, uint8_t seconds = 0, uint32_t microseconds = 0); | ||
| 203 | DayTime(gate_daytime_t const& dtime); | ||
| 204 | |||
| 205 | int compare(DayTime const& that) const; | ||
| 206 | |||
| 207 | bool_t operator< (DayTime const& that) const; | ||
| 208 | bool_t operator<=(DayTime const& that) const; | ||
| 209 | bool_t operator> (DayTime const& that) const; | ||
| 210 | bool_t operator>=(DayTime const& that) const; | ||
| 211 | bool_t operator==(DayTime const& that) const; | ||
| 212 | bool_t operator!=(DayTime const& that) const; | ||
| 213 | }; | ||
| 214 | |||
| 215 | |||
| 216 | |||
| 217 | /// @brief Date-time class (Date + DayTime: year-month-day-hour-minute-second-microsecond) | ||
| 218 | struct GATE_CORE_CPP_API DateTime : public gate_datetime_t | ||
| 219 | { | ||
| 220 | DateTime(uint16_t years = 1601, uint8_t months = 1, uint8_t days = 1, | ||
| 221 | uint8_t hours = 0, uint8_t minutes = 0, uint8_t seconds = 0, uint32_t microseconds = 0); | ||
| 222 | DateTime(Date const& date, DayTime const& dt = DayTime()); | ||
| 223 | |||
| 224 | DateTime(gate_time_t const& time); | ||
| 225 | DateTime(gate_timestamp_t const& timestamp, bool addSystemBias = false); | ||
| 226 | DateTime(String const& text); | ||
| 227 | |||
| 228 | bool_t empty() const; | ||
| 229 | |||
| 230 | Date getDate() const; | ||
| 231 | DayTime getDayTime() const; | ||
| 232 | |||
| 233 | Time toTime() const; /**< Converts date-time to timestamp */ | ||
| 234 | String toString(String const& format = String()) const;/**< Prints date-time as formated string */ | ||
| 235 | |||
| 236 | DateTime add(int64_t microseconds) const; /**< Creates new date-time by adding microseconds to current value */ | ||
| 237 | int64_t diff(DateTime const& dt) const; /**< Returns difference in microseconds between current and given date-time value */ | ||
| 238 | |||
| 239 | int compare(DateTime const& that) const; /**< compares dates, returns -1 if *this < that, +1 if *this > that, 0 if equal */ | ||
| 240 | |||
| 241 | time::Microseconds operator-(DateTime const& dt) const; | ||
| 242 | bool operator!() const; | ||
| 243 | |||
| 244 | template<class A> DateTime operator+(Number<time::duration_value_t, A> const& duration) | ||
| 245 | { | ||
| 246 | return this->add(duration.value() * A::microsecondsFactor); | ||
| 247 | } | ||
| 248 | template<class A> DateTime operator-(Number<time::duration_value_t, A> const& duration) | ||
| 249 | { | ||
| 250 | return this->add(-duration.value() * A::microsecondsFactor); | ||
| 251 | } | ||
| 252 | |||
| 253 | template<class A> DateTime& operator+=(Number<time::duration_value_t, A> const& duration) | ||
| 254 | { | ||
| 255 | return *this = (*this + duration); | ||
| 256 | } | ||
| 257 | template<class A> DateTime& operator-=(Number<time::duration_value_t, A> const& duration) | ||
| 258 | { | ||
| 259 | return *this = (*this - duration); | ||
| 260 | } | ||
| 261 | |||
| 262 | bool operator<(DateTime const& that) const; | ||
| 263 | bool operator<=(DateTime const& that) const; | ||
| 264 | bool operator>(DateTime const& that) const; | ||
| 265 | bool operator>=(DateTime const& that) const; | ||
| 266 | bool operator==(DateTime const& that) const; | ||
| 267 | bool operator!=(DateTime const& that) const; | ||
| 268 | }; | ||
| 269 | |||
| 270 | |||
| 271 | /** | ||
| 272 | * @brief Monotonic time counter | ||
| 273 | */ | ||
| 274 | struct GATE_CORE_CPP_API TimeCounter | ||
| 275 | { | ||
| 276 | public: | ||
| 277 | TimeCounter() noexcept; | ||
| 278 | TimeCounter(gate_timecounter_t src) noexcept; | ||
| 279 | TimeCounter(TimeCounter const& src) noexcept; | ||
| 280 | TimeCounter& operator=(TimeCounter const& src) noexcept; | ||
| 281 | ~TimeCounter() noexcept; | ||
| 282 | |||
| 283 | static TimeCounter now(); /**< creates current system's timecounter value */ | ||
| 284 | |||
| 285 | TimeCounter add(int64_t microseconds) const noexcept; /**< Returns new timecounter by adding microseconds to the current timecounter */ | ||
| 286 | int64_t diff(gate_timecounter_t other) const noexcept; /**< Returns difference in microseconds between current and given timecounter */ | ||
| 287 | int64_t diff(TimeCounter const& other) const noexcept; /**< Returns difference in microseconds between current and given timecounter */ | ||
| 288 | void swap(TimeCounter& other) noexcept; | ||
| 289 | bool_t empty() const noexcept; | ||
| 290 | |||
| 291 | bool operator==(gate_timecounter_t that) const noexcept; | ||
| 292 | bool operator==(TimeCounter const& that) const noexcept; | ||
| 293 | |||
| 294 | bool operator!=(gate_timecounter_t that) const noexcept; | ||
| 295 | bool operator!=(TimeCounter const& that) const noexcept; | ||
| 296 | |||
| 297 | bool operator< (gate_timecounter_t that) const noexcept; | ||
| 298 | bool operator< (TimeCounter const& that) const noexcept; | ||
| 299 | |||
| 300 | bool operator> (gate_timecounter_t that) const noexcept; | ||
| 301 | bool operator> (TimeCounter const& that) const noexcept; | ||
| 302 | |||
| 303 | bool operator<=(gate_timecounter_t that) const noexcept; | ||
| 304 | bool operator<=(TimeCounter const& that) const noexcept; | ||
| 305 | |||
| 306 | bool operator>=(gate_timecounter_t that) const noexcept; | ||
| 307 | bool operator>=(TimeCounter const& that) const noexcept; | ||
| 308 | |||
| 309 | time::Microseconds operator-(TimeCounter const& that) const noexcept; | ||
| 310 | bool operator!() const noexcept; | ||
| 311 | |||
| 312 | 2 | template<class A> TimeCounter operator+(Number<time::duration_value_t, A> const& duration) const noexcept | |
| 313 | { | ||
| 314 | 2 | return this->add(duration.value() * A::microsecondsFactor); | |
| 315 | } | ||
| 316 | template<class A> TimeCounter operator-(Number<time::duration_value_t, A> const& duration) const noexcept | ||
| 317 | { | ||
| 318 | return this->add(-duration.value() * A::microsecondsFactor); | ||
| 319 | } | ||
| 320 | |||
| 321 | template<class A> TimeCounter& operator+=(Number<time::duration_value_t, A> const& duration) noexcept | ||
| 322 | { | ||
| 323 | return *this = (*this + duration); | ||
| 324 | } | ||
| 325 | template<class A> TimeCounter& operator-=(Number<time::duration_value_t, A> const& duration) noexcept | ||
| 326 | { | ||
| 327 | return *this = (*this - duration); | ||
| 328 | } | ||
| 329 | |||
| 330 | gate_timecounter_t const* c_impl() const noexcept; | ||
| 331 | |||
| 332 | private: | ||
| 333 | gate_timecounter_t tc; | ||
| 334 | }; | ||
| 335 | |||
| 336 | |||
| 337 | } // end of namespace gate | ||
| 338 | |||
| 339 | #endif //GATE_TIMES_HPP_INCLUDED | ||
| 340 |