Frozen uptime on iOS / iPhone

Does anyone know why I am having a weird uptime with the following method?

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

For the first minutes, everything seems fine, but when I return to the hours of work of the hours or days of waiting, the uptime remains the same: 30min or 1 hour 34 ... it seems to freeze at a random moment. Mostly on iPhone 4 (rarely on Simulator or iPad)

It may be related to my way of displaying 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 of recording the results on the iPad and iPhone, I see that this is uptime, the time is too slow, the more we expect, the more it becomes obvious that it is late

+6
source share
3 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.

+9
source

This method is the time that the system has been awake from the time it boots (not real-time), and iOS devices usually spend a lot of time. That is why it does not increase as much as you expect.

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

+5
source

See how the upwin command works. The source is here .

The part you want to read is the pr_header () function, especially around these lines:

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

Source: https://habr.com/ru/post/888485/


All Articles