三者都是会受调时间的影响
1
#include <sys/sysctl.h>
struct timeval boottime;
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
size_t size = sizeof(boottime);
if(sysctl(mib,2,&boottime,&size,NULL,0) != -1 && boottime.tv_sec != 0)
{
struct tm * tm;
time_t time_sec ;
time_sec = boottime.tv_sec;
tm = localtime(&time_sec);
CCLog("the time str is %s",CCString::createWithFormat("%d.%d.%d %d:%d:%d",(tm->tm_year + 1900),(tm->tm_mon + 1),tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec)->getCString());
}
2
#import <Foundation/Foundation.h>
unsigned int nRunTime = [[NSProcessInfo processInfo] systemUptime];// 自最后一次开机至现在的运行时间
timeval now;
gettimeofday(&now, NULL);
unsigned int nStartTime = now.tv_sec - nRunTime;
time_t time_sec;
time_sec = nStartTime;
struct tm * tm;
tm = localtime(&time_sec);
CCLog("the str is %s",CCString::createWithFormat("%d.%d.%d %d:%d:%d",(tm->tm_year + 1900),(tm->tm_mon + 1),tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec)->getCString());
// 不推荐使用这种写法,因为官方对systemUptime的解释是The amount of time the system has been awake since the last time it was restarted.所以如果你是1:00重启(关机时间和开机时间都是1:00)的,则使用该方法获取的是自1:00到现在的时间;但如果你是1:00关机的,3:00开机的,则使用该方法获取的是自1:00到现在的时间,即自关机到现在的时间
3
#include <sys/sysctl.h>
-(NSTimeInterval) systemUptime
{
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;
}
// 返回的是上一次开机到现在经过的秒数
参考
https://www.jianshu.com/p/82475b5a7e19
iphone获取手机最近一次开机时间
最新推荐文章于 2022-09-03 11:07:27 发布