iOS 计算 设备开机时间 与 腾讯手机助手核对过

2018-06-11  本文已影响61人  JonesCxy
// 不准确
    NSProcessInfo * processInfo = [NSProcessInfo processInfo];
     //Get the uptime of the system
    NSTimeInterval UptimeInterval = [processInfo systemUptime];
    NSLog(@"----%f",UptimeInterval);
    NSDate *updateDate = [NSDate dateWithTimeIntervalSinceNow:-UptimeInterval];
    NSString *updateDateString = [NSDate jk_stringWithDate:updateDate format:@"YYYY-MM-dd HH:mm:ss"];
// 计算开机时间
+(NSTimeInterval) bootTime{
    struct timeval t;
    size_t len=sizeof(struct timeval);
    if(sysctlbyname("kern.boottime",&t,&len,0,0)!=0)
        return 0.0;
    return  t.tv_sec+t.tv_usec/USEC_PER_SEC;
}
// 类型转化
+ (NSString *)timeIntervalToFormat:(NSTimeInterval)localTimeInterval{
    time_t timeInterval = (time_t)localTimeInterval;
    struct tm *time = localtime(&timeInterval);
    NSString *timeStr = [NSString stringWithFormat:@"%d-%02d-%02d %02d:%02d:%02d",time->tm_year + 1900,time->tm_mon + 1,time->tm_mday,time->tm_hour,time->tm_min, time->tm_sec];
    return timeStr;
}

``
上一篇 下一篇

猜你喜欢

热点阅读