diff options
author | Konstantin Varlamov <var-const@users.noreply.github.com> | 2018-03-29 20:00:13 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-29 20:00:13 -0400 |
commit | 744f9daf43c06d920966773c5d6607377b6a6230 (patch) | |
tree | a5c741b5154e717df41b21d49cbc0d65ad5daefa /Firestore/core/include | |
parent | 54fe9ba7fcaeb47b14ecc98e96535789517fb351 (diff) |
C++ migration: refactor Timestamp logic checking for overflow. (#1000)
Rewrite manual checks using `<chrono>` min()/max() functions and
duration_cast. This is cleaner and avoids including <limits>.
Diffstat (limited to 'Firestore/core/include')
-rw-r--r-- | Firestore/core/include/firebase/firestore/timestamp.h | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/Firestore/core/include/firebase/firestore/timestamp.h b/Firestore/core/include/firebase/firestore/timestamp.h index 096d121..2ee1c46 100644 --- a/Firestore/core/include/firebase/firestore/timestamp.h +++ b/Firestore/core/include/firebase/firestore/timestamp.h @@ -22,7 +22,6 @@ #if !defined(_STLPORT_VERSION) #include <chrono> // NOLINT(build/c++11) #endif // !defined(_STLPORT_VERSION) -#include <limits> #include <string> namespace firebase { @@ -191,13 +190,13 @@ std::chrono::time_point<Clock, Duration> Timestamp::ToTimePoint() const { using TimePoint = chr::time_point<Clock, Duration>; // Saturate on overflow - const auto max_value = std::numeric_limits<typename Duration::rep>::max(); - if (seconds_ > 0 && max_value / Duration::period::den <= seconds_) { - return TimePoint{Duration(max_value)}; + const auto max_seconds = chr::duration_cast<chr::seconds>(Duration::max()); + if (seconds_ > 0 && max_seconds.count() <= seconds_) { + return TimePoint{Duration::max()}; } - const auto min_value = std::numeric_limits<typename Duration::rep>::min(); - if (seconds_ < 0 && min_value / Duration::period::den >= seconds_) { - return TimePoint{Duration(min_value)}; + const auto min_seconds = chr::duration_cast<chr::seconds>(Duration::min()); + if (seconds_ < 0 && min_seconds.count() >= seconds_) { + return TimePoint{Duration::min()}; } const auto seconds = chr::duration_cast<Duration>(chr::seconds(seconds_)); |