Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFCCCC
int do_work(int seconds_to_work) {
  time_t start;
  start = time();

  if (start == (time_t)(-1)) {
    /* Handle error */
  }
  while (time() < start + second_to_work) {
    do_some_work();
  }
}

...

Code Block
bgColor#ccccff
int do_work(int seconds_to_work) {
  time_t start,;
  time_t current;
  start = time();
  current = start;

  if (start == (time_t)(-1)) {
    /* Handle error */
  }
  while (timedifftime(current, start) < start + secondseconds_to_work) {
    current = time();
    if (current == (time_t)(-1)) {
       /* Handle error */
    }
    if (difftime(current, start) >= seconds_to_work)
      break;
    do_some_work();
  }
}

Note that this loop may still not exit, as the range of time_t may not be able to represent two times seconds_to_work apart.

...

Wiki Markup
\[[Kettlewell 02|AA. C References#Kettlewell 02]\] Section 4.1, "time_t"
Wiki Markup

\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]] Section 7.23, "Date and time <time.h>"