Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Frozen uptime on iOS / iPhone

Would someone know why I'm experiencing weird uptime with the following method?

NSProcessInfo *processInfo = [NSProcessInfo processInfo];
NSTimeInterval systemUptime = [processInfo systemUptime];

For first minutes, everything seems fine, but when I come back on the app hours or days laters the uptime is still the same : 30min, or 1h34... it seems to freeze at a random moment. Mostly on iPhone 4 (rarely on Simulator or iPad)

It could be linked to my way of showing it:

+ (NSTimeInterval)uptime:(NSNumber **)days hours:(NSNumber **)hours mins:(NSNumber **)mins
{
    NSProcessInfo *processInfo = [NSProcessInfo processInfo];
        //START UPTIME///////
    NSTimeInterval systemUptime = [processInfo systemUptime];
        // Get the system calendar
    NSCalendar *sysCalendar = [NSCalendar currentCalendar];
        // Create the NSDates
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:(0-systemUptime)]; 
    unsigned int unitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
    NSDateComponents *c = [sysCalendar components:unitFlags fromDate:date toDate:[NSDate date]  options:0]; 
        //NSString *uptimeString = [NSString stringWithFormat:@"%dd %dh %dmin", [c day],[c hour],[c minute]];
    *days = [NSNumber numberWithInt:[c day]];
    *hours = [NSNumber numberWithInt:[c hour]];
    *mins = [NSNumber numberWithInt:[c minute]];
    [date release];
        //END UPTIME////////

    return systemUptime;
}

And later in the code:

NSNumber *uptimeDays, *uptimeHours, *uptimeMins;
[CJGDevice uptime:&uptimeDays hours:&uptimeHours mins:&uptimeMins];
NSString *uptimeString = [NSString stringWithFormat:@"%@d %@h %@min",
                          [uptimeDays stringValue],
                          [uptimeHours stringValue],
                          [uptimeMins stringValue]];

EDIT: after 3 days recording the results on iPad and iPhone I can see that this uptime is wrong, the time is running too slowly, the more we wait the more it's obvious that it's late

like image 968
chriscatfr Avatar asked May 18 '11 23:05

chriscatfr


Video Answer


2 Answers

This method does the trick:

- (time_t)uptime
{
    struct timeval boottime;
    int mib[2] = {CTL_KERN, KERN_BOOTTIME};
    size_t size = sizeof(boottime);
    time_t now;
    time_t uptime = -1;

    (void)time(&now);    

    if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0) 
    {
        uptime = now - boottime.tv_sec;
    }
    return uptime;
}

Thanks to Alastair Stuart for the link.

like image 91
Martin Wickman Avatar answered Sep 22 '22 01:09

Martin Wickman


That method is the amount of time the system has been awake since boot (not real-world clock time) and iOS devices typically spend a lot of time sleeping. That's why it's not increasing as much as you expect.

Source: http://developer.apple.com/library/ios/releasenotes/Cocoa/Foundation.html

like image 29
nobody Avatar answered Sep 18 '22 01:09

nobody