Author Topic: gettimeofday correct implementation  (Read 2950 times)

alekmaul (OP)

  • *
  • Posts: 330
    • Portabledev
gettimeofday correct implementation
« on: April 18, 2010, 04:15:52 pm »
hi all,
i search to implement a correct function to get sec/?sec regarding current time.
I did this function, do you think it is correct because (wolf 3D...) the ported game seems really slow with such function :/
Quote
struct timeval
{
  long tv_sec;                  // Seconds
  long tv_usec;                 // Microseconds
};

int gettimeofday(struct timeval *tv, void *tzp)
{
  long tt = OSTimeGet();
  tv->tv_sec = tt/100;
  tv->tv_usec = tt*1000;
  return 0;
}

flatmush

  • *
  • Posts: 283
Re: gettimeofday correct implementation
« Reply #1 on: April 18, 2010, 04:24:48 pm »
Should be this instead:

Code: [Select]
struct timeval
{
  long tv_sec;                  // Seconds
  long tv_usec;                 // Microseconds
};

int gettimeofday(struct timeval *tv, void *tzp)
{
  long tt = OSTimeGet();
  tv->tv_sec = tt/100;
  tv->tv_usec = tt * 10000;
  return 0;
}

OSTimeGet returns 100th's of a second, so multiply by 10 to get milliseconds, then multiply by 1000 to get microseconds, therefore the multiply should be 10,000.

EDIT: Also I'm not sure if usec should be in the range 0-999999 or not, in which case you need to change that line to:
Code: [Select]
tv->tv_usec = ((tt * 10000) % 1000000);
« Last Edit: April 18, 2010, 04:36:38 pm by flatmush »

alekmaul (OP)

  • *
  • Posts: 330
    • Portabledev
Re: gettimeofday correct implementation
« Reply #2 on: April 19, 2010, 05:24:16 am »
you're right, it's better, thanks a lot ;)

 

Post a new topic
Post a new topic