8#ifndef INCLUDED_BSLS_TIMEINTERVAL
9#define INCLUDED_BSLS_TIMEINTERVAL
168#include <bsls_assert.h>
175#if BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
177#include <type_traits>
183#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
190#if BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
191#define BSLS_TIMEINTERVAL_PROVIDES_CHRONO_CONVERSIONS
197#ifdef BSLS_TIMEINTERVAL_PROVIDES_CHRONO_CONVERSIONS
210template <
class REP,
class PER>
225 std::is_floating_point<REP>::value
226 || std::chrono::treat_as_floating_point<REP>::value;
249template <
class REP,
class PERIOD>
305 k_MILLISECS_PER_SEC = 1000,
307 k_MICROSECS_PER_SEC = 1000000,
309 k_NANOSECS_PER_MICROSEC = 1000,
311 k_NANOSECS_PER_MILLISEC = 1000000,
313 k_NANOSECS_PER_SEC = 1000000000,
315 k_SECONDS_PER_MINUTE = 60,
317 k_SECONDS_PER_HOUR = 60 * k_SECONDS_PER_MINUTE,
319 k_SECONDS_PER_DAY = 24 * k_SECONDS_PER_HOUR
346#ifdef BSLS_TIMEINTERVAL_PROVIDES_CHRONO_CONVERSIONS
351 template <
class REP,
class PERIOD>
352 static bool isValid(
const std::chrono::duration<REP, PERIOD>& duration);
393#ifdef BSLS_TIMEINTERVAL_PROVIDES_CHRONO_CONVERSIONS
406 template <
class REP_TYPE,
class PERIOD_TYPE>
408 const std::chrono::duration<REP_TYPE, PERIOD_TYPE>& duration,
411 PERIOD_TYPE>::k_IMPLICIT_CONVERSION_ENABLED,
413 template <
class REP_TYPE,
class PERIOD_TYPE>
415 const std::chrono::duration<REP_TYPE, PERIOD_TYPE>& duration,
418 PERIOD_TYPE>::k_EXPLICIT_CONVERSION_ENABLED,
603#ifdef BSLS_TIMEINTERVAL_PROVIDES_CHRONO_CONVERSIONS
615 template <
class REP_TYPE,
class PERIOD_TYPE>
617 addDuration(
const std::chrono::duration<REP_TYPE, PERIOD_TYPE>& duration,
618 typename std::enable_if<
620 k_IMPLICIT_CONVERSION_ENABLED,
660 template <
class STREAM>
664#ifdef BSLS_TIMEINTERVAL_PROVIDES_CHRONO_CONVERSIONS
670 template <
class DURATION_TYPE>
673 int>::type * = 0)
const;
732#ifdef BSLS_TIMEINTERVAL_PROVIDES_CHRONO_CONVERSIONS
740 template <
class DURATION_TYPE>
742 typename std::enable_if<
766 template <
class STREAM>
782 std::ostream&
print(std::ostream& stream,
784 int spacesPerLevel = 4)
const;
786#ifndef BDE_OPENSOURCE_PUBLICATION
796#ifndef BDE_OMIT_INTERNAL_DEPRECATED
808 template <
class STREAM>
886#if defined(BSLS_COMPILERFEATURES_SUPPORT_INLINE_NAMESPACE) && \
887 defined(BSLS_COMPILERFEATURES_SUPPORT_USER_DEFINED_LITERALS)
888inline namespace literals {
889inline namespace TimeIntervalLiterals {
897TimeInterval operator ""_h(
unsigned long long int hours);
905TimeInterval operator ""_min(
unsigned long long int minutes);
913TimeInterval operator ""_s(
unsigned long long int seconds);
919TimeInterval operator ""_ms(
unsigned long long int milliseconds);
925TimeInterval operator ""_us(
unsigned long long int microseconds);
931TimeInterval operator ""_ns(
unsigned long long int nanoseconds);
954 return (lhs > 0 && LLONG_MAX - lhs >= rhs) ||
955 (lhs <= 0 && LLONG_MIN - lhs <= rhs);
972#ifdef BSLS_TIMEINTERVAL_PROVIDES_CHRONO_CONVERSIONS
974template <
class REP,
class PERIOD>
977 std::chrono::duration<long double> minValue =
978 std::chrono::duration<long double>(LLONG_MIN);
980 std::chrono::duration<long double> maxValue =
981 std::chrono::duration<long double>(LLONG_MAX);
983 const std::chrono::duration<long double> safeDuration =
984 std::chrono::duration_cast<std::chrono::duration<long double> >(
987 return (safeDuration >= minValue && safeDuration <= maxValue);
1011#ifdef BSLS_TIMEINTERVAL_PROVIDES_CHRONO_CONVERSIONS
1012template <
class REP_TYPE,
class PERIOD_TYPE>
1016 const std::chrono::duration<REP_TYPE, PERIOD_TYPE>& duration,
1019 PERIOD_TYPE>::k_IMPLICIT_CONVERSION_ENABLED,
1022 BSLS_ASSERT((isValid<REP_TYPE, PERIOD_TYPE>(duration)));
1023 using SecondsRatio = std::ratio<1>;
1024 using TimeIntervalSeconds =
1025 std::chrono::duration<bsls::Types::Int64, SecondsRatio>;
1026 using TimeIntervalNanoseconds = std::chrono::duration<int, std::nano>;
1029 std::chrono::duration_cast<TimeIntervalSeconds>(duration).count();
1030 const int k_NANOSECONDS =
1031 std::chrono::duration_cast<TimeIntervalNanoseconds>(
1032 duration - TimeIntervalSeconds(k_SECONDS)).count();
1036template <
class REP_TYPE,
class PERIOD_TYPE>
1040 const std::chrono::duration<REP_TYPE, PERIOD_TYPE>& duration,
1043 PERIOD_TYPE>::k_EXPLICIT_CONVERSION_ENABLED,
1046 BSLS_ASSERT((isValid<REP_TYPE, PERIOD_TYPE>(duration)));
1048 std::chrono::duration_cast<std::chrono::seconds>(duration).count();
1049 const int k_NANOSECONDS =
static_cast<int>(
1050 std::chrono::duration_cast<std::chrono::nanoseconds>(
1051 duration - std::chrono::seconds(k_SECONDS)).count());
1067 return addInterval(rhs.d_seconds, rhs.d_nanoseconds);
1082 return addInterval(-rhs.d_seconds, -rhs.d_nanoseconds);
1098 LLONG_MIN / k_SECONDS_PER_DAY <= days);
1107 LLONG_MIN / k_SECONDS_PER_HOUR <= hours);
1109 return addSeconds(hours * k_SECONDS_PER_HOUR);
1116 LLONG_MIN / k_SECONDS_PER_MINUTE <= minutes);
1118 return addSeconds(minutes * k_SECONDS_PER_MINUTE);
1127 if (d_seconds > 0 && d_nanoseconds < 0) {
1129 d_nanoseconds += k_NANOSECS_PER_SEC;
1131 else if (d_seconds < 0 && d_nanoseconds > 0) {
1133 d_nanoseconds -= k_NANOSECS_PER_SEC;
1142 return addInterval( milliseconds / k_MILLISECS_PER_SEC,
1143 static_cast<int>((milliseconds % k_MILLISECS_PER_SEC) *
1144 k_NANOSECS_PER_MILLISEC));
1150 return addInterval( microseconds / k_MICROSECS_PER_SEC,
1151 static_cast<int>((microseconds % k_MICROSECS_PER_SEC) *
1152 k_NANOSECS_PER_MICROSEC));
1159 static_cast<int>(
nanoseconds % k_NANOSECS_PER_SEC));
1168 LLONG_MIN / k_SECONDS_PER_DAY <= days);
1177 LLONG_MIN / k_SECONDS_PER_HOUR <= hours);
1186 LLONG_MIN / k_SECONDS_PER_MINUTE <= minutes);
1202 static_cast<int>((milliseconds % k_MILLISECS_PER_SEC) *
1203 k_NANOSECS_PER_MILLISEC));
1210 static_cast<int>((microseconds % k_MICROSECS_PER_SEC) *
1211 k_NANOSECS_PER_MICROSEC));
1219 static_cast<int>(
nanoseconds % k_NANOSECS_PER_SEC));
1222#ifdef BSLS_TIMEINTERVAL_PROVIDES_CHRONO_CONVERSIONS
1223template <
class REP_TYPE,
class PERIOD_TYPE>
1227 const std::chrono::duration<REP_TYPE, PERIOD_TYPE>& duration,
1230 PERIOD_TYPE>::k_IMPLICIT_CONVERSION_ENABLED,
1233 BSLS_ASSERT((isValid<REP_TYPE, PERIOD_TYPE>(duration)));
1236 std::chrono::duration_cast<std::chrono::seconds>(duration).count();
1237 const int k_NANOSECONDS =
static_cast<int>(
1238 std::chrono::duration_cast<std::chrono::nanoseconds>(
1239 duration - std::chrono::seconds(k_SECONDS)).count());
1256 d_nanoseconds =
static_cast<int>(
nanoseconds % k_NANOSECS_PER_SEC);
1262 if (d_seconds > 0 && d_nanoseconds < 0) {
1264 d_nanoseconds += k_NANOSECS_PER_SEC;
1266 else if (d_seconds < 0 && d_nanoseconds > 0) {
1268 d_nanoseconds -= k_NANOSECS_PER_SEC;
1288template <
class STREAM>
1307 stream.invalidate();
1311 stream.invalidate();
1319#ifdef BSLS_TIMEINTERVAL_PROVIDES_CHRONO_CONVERSIONS
1320template <
class DURATION_TYPE>
1325 using SecondsRatio = std::ratio<1>;
1326 using TimeIntervalSeconds =
1327 std::chrono::duration<bsls::Types::Int64, SecondsRatio>;
1328 using TimeIntervalNanoseconds = std::chrono::duration<int, std::nano>;
1329 using Period =
typename DURATION_TYPE::period;
1330 using LongDoubleTo = std::chrono::duration<long double, Period>;
1332 const LongDoubleTo MIN_VALUE =
1333 std::chrono::duration_cast<LongDoubleTo>(DURATION_TYPE::min());
1335 const LongDoubleTo MAX_VALUE =
1336 std::chrono::duration_cast<LongDoubleTo>(DURATION_TYPE::max());
1338 const LongDoubleTo value = std::chrono::duration_cast<LongDoubleTo>(
1339 TimeIntervalSeconds(
seconds()))
1340 + std::chrono::duration_cast<LongDoubleTo>(
1343 return (MIN_VALUE <= value && value <= MAX_VALUE);
1350 return d_nanoseconds;
1362 return d_seconds / k_SECONDS_PER_DAY;
1368 return d_seconds / k_SECONDS_PER_HOUR;
1374 return d_seconds / k_SECONDS_PER_MINUTE;
1387 LLONG_MIN / k_MILLISECS_PER_SEC <= d_seconds);
1389 d_nanoseconds / k_NANOSECS_PER_MILLISEC));
1392 return d_seconds * k_MILLISECS_PER_SEC
1393 + d_nanoseconds / k_NANOSECS_PER_MILLISEC;
1400 LLONG_MIN / k_MICROSECS_PER_SEC <= d_seconds);
1402 d_nanoseconds / k_NANOSECS_PER_MICROSEC));
1404 return d_seconds * k_MICROSECS_PER_SEC
1405 + d_nanoseconds / k_NANOSECS_PER_MICROSEC;
1412 LLONG_MIN / k_NANOSECS_PER_SEC <= d_seconds);
1416 return d_seconds * k_NANOSECS_PER_SEC + d_nanoseconds;
1419#ifdef BSLS_TIMEINTERVAL_PROVIDES_CHRONO_CONVERSIONS
1420template <
class DURATION_TYPE>
1423typename std::enable_if<
1426 DURATION_TYPE>::type
1429 using SecondsRatio = std::ratio<1>;
1430 using TimeIntervalSeconds =
1431 std::chrono::duration<bsls::Types::Int64, SecondsRatio>;
1432 using TimeIntervalNanoseconds = std::chrono::duration<int, std::nano>;
1436 return (std::chrono::duration_cast<DURATION_TYPE>(TimeIntervalSeconds(
1438 + std::chrono::duration_cast<DURATION_TYPE>(TimeIntervalNanoseconds(
1446 return static_cast<double>(d_seconds) + d_nanoseconds /
1447 static_cast<double>(k_NANOSECS_PER_SEC);
1452template <
class STREAM>
1458 stream.putInt64(d_seconds);
1459 stream.putInt32(d_nanoseconds);
1462 stream.invalidate();
1470#ifndef BDE_OPENSOURCE_PUBLICATION
1480#ifndef BDE_OMIT_INTERNAL_DEPRECATED
1487template <
class STREAM>
1491 return print(stream, 0, -1);
1501 const TimeInterval& rhs)
1503 TimeInterval result(lhs);
1504 return result.addInterval(rhs.seconds(), rhs.nanoseconds());
1510 return lhs + TimeInterval(rhs);
1516 return TimeInterval(lhs) + rhs;
1521 const TimeInterval& rhs)
1526 TimeInterval result(lhs);
1527 result.addInterval(-rhs.seconds(), -rhs.nanoseconds());
1534 return lhs - TimeInterval(rhs);
1540 return TimeInterval(lhs) - rhs;
1548 return TimeInterval(-rhs.seconds(), -rhs.nanoseconds());
1554 return lhs.seconds() == rhs.seconds()
1555 && lhs.nanoseconds() == rhs.nanoseconds();
1561 return lhs == TimeInterval(rhs);
1567 return TimeInterval(lhs) == rhs;
1573 return lhs.seconds() != rhs.seconds()
1574 || lhs.nanoseconds() != rhs.nanoseconds();
1580 return lhs != TimeInterval(rhs);
1586 return TimeInterval(lhs) != rhs;
1590bool bsls::operator< (
const TimeInterval& lhs,
const TimeInterval& rhs)
1592 return lhs.seconds() < rhs.seconds()
1593 || (lhs.seconds() == rhs.seconds()
1594 && lhs.nanoseconds() < rhs.nanoseconds());
1600 return lhs < TimeInterval(rhs);
1606 return TimeInterval(lhs) < rhs;
1612 return lhs.seconds() < rhs.seconds()
1613 || (lhs.seconds() == rhs.seconds()
1614 && lhs.nanoseconds() <= rhs.nanoseconds());
1620 return lhs <= TimeInterval(rhs);
1626 return TimeInterval(lhs) <= rhs;
1630bool bsls::operator> (
const TimeInterval& lhs,
const TimeInterval& rhs)
1632 return lhs.seconds() > rhs.seconds()
1633 || (lhs.seconds() == rhs.seconds()
1634 && lhs.nanoseconds() > rhs.nanoseconds());
1640 return lhs > TimeInterval(rhs);
1646 return TimeInterval(lhs) > rhs;
1652 return lhs.seconds() > rhs.seconds()
1653 || (lhs.seconds() == rhs.seconds()
1654 && lhs.nanoseconds() >= rhs.nanoseconds());
1660 return lhs >= TimeInterval(rhs);
1666 return TimeInterval(lhs) >= rhs;
1669#if defined(BSLS_COMPILERFEATURES_SUPPORT_INLINE_NAMESPACE) && \
1670 defined(BSLS_COMPILERFEATURES_SUPPORT_USER_DEFINED_LITERALS)
1674 unsigned long long int hours)
1682 unsigned long long int minutes)
1690 unsigned long long int seconds)
1698 unsigned long long int milliseconds)
1703 return TimeInterval(milliseconds / k_MILLISECS_PER_SEC,
1704 static_cast<int>((milliseconds % k_MILLISECS_PER_SEC) *
1705 k_NANOSECS_PER_MILLISEC));
1710 unsigned long long int microseconds)
1715 return TimeInterval(microseconds / k_MICROSECS_PER_SEC,
1716 static_cast<int>((microseconds % k_MICROSECS_PER_SEC) *
1717 k_NANOSECS_PER_MICROSEC));
1722 unsigned long long int nanoseconds)
1726 return TimeInterval(nanoseconds / k_NANOSECS_PER_SEC,
1727 static_cast<int>(nanoseconds % k_NANOSECS_PER_SEC));
1753#undef BSLS_TIMEINTERVAL_PROVIDES_CHRONO_CONVERSIONS
Definition bsls_timeinterval.h:301
BSLS_KEYWORD_CONSTEXPR bsls::Types::Int64 totalMinutes() const
Definition bsls_timeinterval.h:1372
BSLS_KEYWORD_CONSTEXPR TimeInterval()
Definition bsls_timeinterval.h:993
BSLS_KEYWORD_CONSTEXPR_CPP14 void setTotalHours(bsls::Types::Int64 hours)
Definition bsls_timeinterval.h:1174
TimeInterval & addMicroseconds(bsls::Types::Int64 microseconds)
Definition bsls_timeinterval.h:1148
BSLS_KEYWORD_CONSTEXPR_CPP14 void setTotalDays(bsls::Types::Int64 days)
Definition bsls_timeinterval.h:1165
BSLS_KEYWORD_CONSTEXPR int nanoseconds() const
Definition bsls_timeinterval.h:1348
TimeInterval & operator-=(const TimeInterval &rhs)
Definition bsls_timeinterval.h:1078
TimeInterval & addNanoseconds(bsls::Types::Int64 nanoseconds)
Definition bsls_timeinterval.h:1156
BSLS_KEYWORD_CONSTEXPR_CPP14 std::enable_if< TimeInterval_IsDuration< DURATION_TYPE >::value &&!TimeInterval_RepTraits< typenameDURATION_TYPE::rep >::k_IS_FLOAT, DURATION_TYPE >::type asDuration() const
Definition bsls_timeinterval.h:1427
TimeInterval & addInterval(bsls::Types::Int64 seconds, int nanoseconds=0)
BSLS_KEYWORD_CONSTEXPR_CPP14 void setTotalNanoseconds(bsls::Types::Int64 nanoseconds)
Definition bsls_timeinterval.h:1216
BSLS_KEYWORD_CONSTEXPR_CPP14 void setTotalSeconds(bsls::Types::Int64 seconds)
Definition bsls_timeinterval.h:1192
BSLS_KEYWORD_CONSTEXPR bsls::Types::Int64 totalSeconds() const
Definition bsls_timeinterval.h:1378
TimeInterval & operator=(const TimeInterval &rhs)=default
TimeInterval & addMilliseconds(bsls::Types::Int64 milliseconds)
Definition bsls_timeinterval.h:1140
STREAM & bdexStreamIn(STREAM &stream, int version)
Definition bsls_timeinterval.h:1289
BSLS_KEYWORD_CONSTEXPR_CPP14 void setTotalMinutes(bsls::Types::Int64 minutes)
Definition bsls_timeinterval.h:1183
BSLS_KEYWORD_CONSTEXPR_CPP14 TimeInterval & addHours(bsls::Types::Int64 hours)
Definition bsls_timeinterval.h:1104
BSLS_KEYWORD_CONSTEXPR_CPP14 void setTotalMilliseconds(bsls::Types::Int64 milliseconds)
Definition bsls_timeinterval.h:1199
TimeInterval & operator+=(const TimeInterval &rhs)
Definition bsls_timeinterval.h:1065
BSLS_KEYWORD_CONSTEXPR_CPP14 bsls::Types::Int64 totalMicroseconds() const
Definition bsls_timeinterval.h:1397
bool isInDurationRange(typename std::enable_if< TimeInterval_IsDuration< DURATION_TYPE >::value, int >::type *=0) const
Definition bsls_timeinterval.h:1321
BSLS_KEYWORD_CONSTEXPR_CPP14 TimeInterval & addDuration(const std::chrono::duration< REP_TYPE, PERIOD_TYPE > &duration, typename std::enable_if< TimeInterval_DurationTraits< REP_TYPE, PERIOD_TYPE >::k_IMPLICIT_CONVERSION_ENABLED, int >::type *=0)
Definition bsls_timeinterval.h:1226
static BSLS_KEYWORD_CONSTEXPR bool isValid(bsls::Types::Int64 seconds, int nanoseconds)
Definition bsls_timeinterval.h:966
double totalSecondsAsDouble() const
Definition bsls_timeinterval.h:1444
STREAM & streamOut(STREAM &stream) const
Definition bsls_timeinterval.h:1489
STREAM & bdexStreamOut(STREAM &stream, int version) const
Definition bsls_timeinterval.h:1453
std::ostream & print(std::ostream &stream, int level=0, int spacesPerLevel=4) const
BSLS_KEYWORD_CONSTEXPR_CPP14 void setIntervalRaw(bsls::Types::Int64 seconds, int nanoseconds=0)
Definition bsls_timeinterval.h:1274
TimeInterval(double seconds)
static int maxSupportedBdexVersion()
Definition bsls_timeinterval.h:1474
BSLS_KEYWORD_CONSTEXPR_CPP14 bsls::Types::Int64 totalNanoseconds() const
Definition bsls_timeinterval.h:1409
BSLS_KEYWORD_CONSTEXPR bsls::Types::Int64 totalDays() const
Definition bsls_timeinterval.h:1360
BSLS_KEYWORD_CONSTEXPR bsls::Types::Int64 seconds() const
Definition bsls_timeinterval.h:1354
BSLS_KEYWORD_CONSTEXPR bsls::Types::Int64 totalHours() const
Definition bsls_timeinterval.h:1366
TimeInterval(const TimeInterval &original)=default
BSLS_KEYWORD_CONSTEXPR_CPP14 TimeInterval & addSeconds(bsls::Types::Int64 seconds)
Definition bsls_timeinterval.h:1122
BSLS_KEYWORD_CONSTEXPR_CPP14 TimeInterval & addMinutes(bsls::Types::Int64 minutes)
Definition bsls_timeinterval.h:1113
static int maxSupportedVersion()
Definition bsls_timeinterval.h:1482
BSLS_KEYWORD_CONSTEXPR_CPP14 bsls::Types::Int64 totalMilliseconds() const
Definition bsls_timeinterval.h:1384
BSLS_KEYWORD_CONSTEXPR_CPP14 TimeInterval & addDays(bsls::Types::Int64 days)
Definition bsls_timeinterval.h:1095
BSLS_KEYWORD_CONSTEXPR_CPP14 void setTotalMicroseconds(bsls::Types::Int64 microseconds)
Definition bsls_timeinterval.h:1207
BSLS_KEYWORD_CONSTEXPR_CPP14 void setInterval(bsls::Types::Int64 seconds, int nanoseconds=0)
Definition bsls_timeinterval.h:1245
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_CONSTEXPR_CPP14
Definition bsls_keyword.h:595
#define BSLS_KEYWORD_CONSTEXPR
Definition bsls_keyword.h:588
#define BSLS_PRECONDITIONS_END()
Definition bsls_preconditions.h:131
#define BSLS_PRECONDITIONS_BEGIN()
Definition bsls_preconditions.h:130
Definition bdlt_iso8601util.h:691
TimeInterval operator+(const TimeInterval &lhs, const TimeInterval &rhs)
bool operator>=(const TimeInterval &lhs, const TimeInterval &rhs)
TimeInterval operator-(const TimeInterval &lhs, const TimeInterval &rhs)
bool operator==(const TimeInterval &lhs, const TimeInterval &rhs)
bool operator>(const TimeInterval &lhs, const TimeInterval &rhs)
bool operator!=(const TimeInterval &lhs, const TimeInterval &rhs)
bool operator<=(const TimeInterval &lhs, const TimeInterval &rhs)
std::ostream & operator<<(std::ostream &stream, const TimeInterval &timeInterval)
bool operator<(const TimeInterval &lhs, const TimeInterval &rhs)
Definition bdldfp_decimal.h:5188
Definition bsls_timeinterval.h:250
static const bool k_IMPLICIT_CONVERSION_ENABLED
Definition bsls_timeinterval.h:273
static const bool k_EXPLICIT_CONVERSION_ENABLED
Definition bsls_timeinterval.h:284
static const bool k_IS_IMPLICIT
Definition bsls_timeinterval.h:263
static const bool k_IS_FLOAT
Definition bsls_timeinterval.h:257
Definition bsls_timeinterval.h:206
Definition bsls_timeinterval.h:222
static const bool k_IS_FLOAT
Definition bsls_timeinterval.h:224
long long Int64
Definition bsls_types.h:132