Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8044485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:14:42+00:00 2026-06-05T05:14:42+00:00

I have found a function to get milliseconds since the Mac was started: U32

  • 0

I have found a function to get milliseconds since the Mac was started:

U32 Platform::getRealMilliseconds()
{
   // Duration is a S32 value.
   // if negative, it is in microseconds.
   // if positive, it is in milliseconds.
   Duration durTime = AbsoluteToDuration(UpTime());
   U32 ret;
   if( durTime < 0 )
      ret = durTime / -1000;
   else 
      ret = durTime;

   return ret;
}

The problem is that after ~20 days AbsoluteToDuration returns INT_MAX all the time until the Mac is rebooted.

I have tried to use method below, it worked, but looks like gettimeofday takes more time and slows down the game a bit:

timeval tim;
gettimeofday(&tim, NULL);
U32 ret = ((tim.tv_sec) * 1000 + tim.tv_usec/1000.0) + 0.5;

Is there a better way to get number of milliseconds elapsed since some epoch (preferably since the app started)?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:14:44+00:00Added an answer on June 5, 2026 at 5:14 am

    Your real problem is that you are trying to fit an uptime-in-milliseconds value into a 32-bit integer. If you do that your value will always wrap back to zero (or saturate) in 49 days or less, no matter how you obtain the value.

    One possible solution would be to track time values with a 64-bit integer instead; that way the day of reckoning gets postponed for a few hundred years and so you don’t have to worry about the problem. Here’s a MacOS/X implementation of that:

    uint64_t GetTimeInMillisecondsSinceBoot()
    {
       return UnsignedWideToUInt64(AbsoluteToNanoseconds(UpTime()))/1000000;
    }
    

    … or if you don’t want to return a 64-bit time value, the next-best thing would be to record the current time-in-milliseconds value when your program starts, and then always subtract that value from the values you return. That way things won’t break until your own program has been running for at least 49 days, which I suppose is unlikely for a game.

    uint32_t GetTimeInMillisecondsSinceProgramStart()
    {
       static uint64_t _firstTimeMillis = GetTimeInMillisecondsSinceBoot();
       uint64_t nowMillis = GetTimeInMillisecondsSinceBoot();
       return (uint32_t) (nowMillis-_firstTimeMillis);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found a function which implement a switch statement --> File = open('/file.txt','r')
I'm learning JQuery and have found the factory function $() with its selectors quite
I have done various tests and I have found that the jQuery validate function
I have a function that returns an iterator if an object is found. Now
I found a very interesting/strange thing about MAX() function in SQL. I have column
Given that you have found a process that you wish to execute a function
I found this function that does an AWESOME job (IMHO): http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curl /** * Get
I have found the following JS on the web. It is a function to
I have found the function dhist() in the ggplot2 package that implements the variable
I just have found out that there is a function called func_get_arg in PHP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.