Translating Time to a float

Nibbie
Posts: 4
Joined: 2008.12
Post: #1
I am nearly finished with my app, but to get there, I need to preform a fairly complicated action.

First, I need to find the current time, then save it when the app is exited.
(Time1)

On reopening the app, I need the new current time to be read.
(Time2)

Then I need a way, using Time2, to find how long it's been since Time1.

The last part of my process is to use that difference in time to apply changes to the app.
The changes are simple, just a few floats right now.

Is there any way to do this simply, or am I looking at an all-nighter? WackoBored
Quote this message in a reply
Member
Posts: 45
Joined: 2008.04
Post: #2
NSDate gives you the time and time differences, NSUserDefaults saves/loads
Quote this message in a reply
Member
Posts: 67
Joined: 2006.06
Post: #3
did you get it? Its pretty simple, just make an NSDate object and save it with NSUserDefaults. On launching the app load the user defaults make a new NSDate and subtract.

The machine does not run without the coin.
Quote this message in a reply
Member
Posts: 241
Joined: 2008.07
Post: #4
If you need accurate timing, I suggest using mach. It works very nicely and is quite accurate and efficient.
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2008.12
Post: #5
bmantzey Wrote:If you need accurate timing, I suggest using mach. It works very nicely and is quite accurate and efficient.

Huh
Could you explain a little more, I'm not familiar with mach.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #6
Code:
#include <mach/mach.h>
#include <mach/mach_time.h>

double getDoubleTime(void)
{
    mach_timebase_info_data_t base;
    mach_timebase_info(&base);

    uint64_t nanos = (mach_absolute_time()*base.numer)/base.denom;
    return (double)nanos*1.0e-9;
}

That's how I did it.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #7
And here's my version:

Code:
double GetCurrentTime(void)
{
    static uint64_t        timebase = 0;
    uint64_t            time, nanos;
    double                seconds;
    
    // calculate the time base for this platform on the first time through
    if (timebase == 0)
    {
        mach_timebase_info_data_t    timebaseInfo;
        mach_timebase_info(&timebaseInfo);
        timebase = timebaseInfo.numer / timebaseInfo.denom;
    }
    
    time = mach_absolute_time();
    nanos = time * timebase;
    seconds = (double)nanos / 1000000000.0;
    return seconds;
}

Since you only need to get the time base once, it might save a teeny amount of overhead. Also, I think skorche's multiply by 1.0e-9 might be faster than my divide. I got the mach time information at Apple which I used to butcher my version together with.
Quote this message in a reply
Member
Posts: 283
Joined: 2006.05
Post: #8
If you're measuring the time between app exit and launch, I doubt you need the accuracy of mach time. Also, if the user restarts the phone between these events, I think mach time would wouldn't work (NSDate would be fine).
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #9
Wow... Blush Totally didn't read the first post. Don't mind me and all my mach code, listen to maxamile.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Translating your game into different languages and uploading them EndeavorBros 4 2,557 Aug 27, 2009 08:14 AM
Last Post: ThemsAllTook