Skip to content

Commit

Permalink
ofTime: fix overflow when adding more than 2 seconds
Browse files Browse the repository at this point in the history
  #changelog #utils
  • Loading branch information
arturoc committed Apr 9, 2019
1 parent 521d763 commit f7889ef
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions libs/openFrameworks/utils/ofTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ void ofTimer::waitNext(){
WaitForSingleObject(hTimer, INFINITE);
#else
auto now = ofGetCurrentTime();
auto waitNanos = nextWakeTime - now;
if(waitNanos > std::chrono::nanoseconds(0)){
if(now < nextWakeTime){
auto waitNanos = nextWakeTime - now;
timespec waittime = (ofTime() + waitNanos).getAsTimespec();
timespec remainder;
nanosleep(&waittime,&remainder);
timespec remainder = {0,0};
nanosleep(&waittime, &remainder);
}
#endif
calculateNextPeriod();
Expand Down
6 changes: 3 additions & 3 deletions libs/openFrameworks/utils/ofUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ struct ofTime{
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(duration);
ofTime t = *this;
t.nanoseconds += ns.count();
if(this->nanoseconds>NANOS_PER_SEC){
uint64_t secs = this->nanoseconds / NANOS_PER_SEC;
if(t.nanoseconds>=NANOS_PER_SEC){
uint64_t secs = t.nanoseconds / NANOS_PER_SEC;
t.nanoseconds -= NANOS_PER_SEC*secs;
t.seconds+=secs;
}
Expand All @@ -126,7 +126,7 @@ struct ofTime{
constexpr uint64_t NANOS_PER_SEC = 1000000000ll;
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(duration);
this->nanoseconds += ns.count();
if(this->nanoseconds>NANOS_PER_SEC){
if(this->nanoseconds>=NANOS_PER_SEC){
uint64_t secs = this->nanoseconds / NANOS_PER_SEC;
this->nanoseconds -= NANOS_PER_SEC*secs;
this->seconds+=secs;
Expand Down

0 comments on commit f7889ef

Please sign in to comment.