Skip to content

Commit

Permalink
GH-1062 Add a safe_add that does not overflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed May 5, 2023
1 parent 4e176a1 commit 086f1fb
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions libraries/libfc/include/fc/time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ namespace fc {
std::string to_iso_string()const;
static time_point from_iso_string( const std::string& s );

// protect against overflow
constexpr time_point& safe_add( const microseconds& m ) {
if (m.count() > 0 && elapsed > fc::microseconds::maximum() - m) {
elapsed = microseconds::maximum();
} else { // does not guard against underflow
elapsed += m;
}
return *this;
}

constexpr const microseconds& time_since_epoch()const { return elapsed; }
constexpr uint32_t sec_since_epoch()const { return elapsed.count() / 1000000; }
constexpr bool operator > ( const time_point& t )const { return elapsed._count > t.elapsed._count; }
Expand Down

0 comments on commit 086f1fb

Please sign in to comment.