1、NSDate、NSDateFormatter获取时间
获取到毫秒时间戳
NSString* dateString;
NSDateFormatter * formatter = [[NSDateFormatter alloc ] init];
[formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss.SSS"];
//年/月/日 时/分/秒.毫秒
dateString = [formatter stringFromDate:[NSDate date]];
2、NSDate、NSCalendar获取时间
获取到纳秒时间戳
NSInteger era,year,month,day,hour,minute,second,nanosecond;
// 时区、年、月、日、时、分、秒、纳秒
NSDate *date = [NSDate date];
NSCalendar *localCalendar = [NSCalendar currentCalendar];
[localCalendar getEra:&era year:&year month:&month day:&day fromDate:date];
[localCalendar getHour:&hour minute:&minute second:&second nanosecond:&nanosecond fromDate:date];
3、<sys/time.h>获取时间
获取到微秒时间
struct timeval tv;
struct timezone tz;
struct tm *p;
gettimeofday(&tv, &tz);
p = localtime(&tv.tv_sec);
//年 p->tm_year+1900
//月 p->tm_mon+1
//日 p->tm_mday
//时 p->tm_hour+1
//分 p->tm_min
//秒 p->tm_sec
//微秒 tv.tv_usec
//星期 p->wday+1
//年天 p->yday
数据结构的定义:
_STRUCT_TIMEVAL {
__darwin_time_t tv_sec; /* seconds */
__darwin_suseconds_t tv_usec; /* and microseconds */
};
struct tm {
int tm_sec; /* seconds after the minute [0-60] */
int tm_min; /* minutes after the hour [0-59] */
int tm_hour; /* hours since midnight [0-23] */
int tm_mday; /* day of the month [1-31] */
int tm_mon; /* months since January [0-11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday [0-6] */
int tm_yday; /* days since January 1 [0-365] */
int tm_isdst; /* Daylight Savings Time flag */
long tm_gmtoff; /* offset from UTC in seconds */
char *tm_zone; /* timezone abbreviation */
};
int gettimeofday(struct timeval * __restrict, void * __restrict);
struct tm *localtime(const time_t *);
struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of dst correction */
};
4、<mach/mach_time.h>
计算一个代码段运行耗时(纳秒)
mach_timebase_info_data_t info;
if (mach_timebase_info(&info) != KERN_SUCCESS) return -1.0;
uint64_t start = mach_absolute_time ();
block ();
uint64_t end = mach_absolute_time ();
uint64_t elapsed = end - start;
uint64_t nanos = elapsed * info.numer / info.denom;
//纳秒时间
5、NSDate获取1970的时间
NSTimeInterval本身是个秒级别的double类型数值,小数点后面即毫秒数,*1000.0f即可得到毫秒级别的时间差
NSTimeInterval time = [[NSDate date] timeIntervalSince1970]
输出结果示例:1584162491.226890,一定程度上来说小数点后的数据*1000之后,能够获取到us时间,不过精度没有那么高
6、NSProcessInfo
通过NSProcessInfo获取到进程启动的时间(807539.047739秒) 和 进程的其它相关信息
NSProcessInfo *processInfo = [NSProcessInfo processInfo];
NSLog(@"environment:%@",processInfo.environment);
//进程运行环境,一个字典
NSLog(@"arguments:%@",processInfo.arguments);
//获取进程开启的时候,传入的命令行参数信息,是一个参数数组
NSLog(@"hostName:%@",processInfo.hostName);
//主机名 Jerry.local
processInfo.processName = @"custom_process_name";
//设置进程名称
NSLog(@"processName:%@",processInfo.processName);
//进程名称 custom_process_name
NSLog(@"processIdentifier:%d",processInfo.processIdentifier);
//进程ID 51851
NSLog(@"globallyUniqueString:%@",processInfo.globallyUniqueString);
//全球唯一码,进程每启动一次,生成的globallyUniqueString都不一样
//2B5608D4-EF1C-437B-ACE8-73BFA9E4E85F-51851-0002DE73D5D87B88
NSLog(@"operatingSystemVersionString:%@",processInfo.operatingSystemVersionString);
//获取运行当前进程的设备的操作系统的版本号字符串,Version 10.14.5 (Build 18F203)
NSLog(@"operatingSystemVersion.majorVersion:%ld",processInfo.operatingSystemVersion.majorVersion);
//10
NSLog(@"operatingSystemVersion.minorVersion:%ld",processInfo.operatingSystemVersion.minorVersion);
//14
NSLog(@"operatingSystemVersion.patchVersion:%ld",processInfo.operatingSystemVersion.patchVersion);
//5
NSLog(@"systemUptime:%f",processInfo.systemUptime);
//获取系统启动的时间,单位是秒 807539.047739
NSLog(@"lowPowerModeEnabled:%d",processInfo.lowPowerModeEnabled);
//得到的是一个布尔值的结果:YES:开启了低电量模式,NO:未开启低电量模式,仅对iOS有用
environment和arguments详细信息
environment:{
"Apple_PubSub_Socket_Render" = "/private/tmp/com.apple.launchd.axZ2IgB8PY/Render";
"CA_ASSERT_MAIN_THREAD_TRANSACTIONS" = 0;
"CA_DEBUG_TRANSACTIONS" = 0;
"DYLD_FRAMEWORK_PATH" = "/Users/jerry/XcodeProj/TestOC/build/Debug";
"DYLD_LIBRARY_PATH" = "/Users/jerry/XcodeProj/TestOC/build/Debug:/usr/lib/system/introspection";
GPUProfilerEnabled = YES;
HOME = "/Users/jerry";
LOGNAME = "jerry.yang";
"METAL_DEBUG_ERROR_MODE" = 0;
"METAL_DEVICE_WRAPPER_TYPE" = 1;
MallocNanoZone = 0;
NSUnbufferedIO = YES;
"OS_ACTIVITY_DT_MODE" = YES;
PATH = "/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin";
PWD = "/Users/jerry/XcodeProj/TestOC/build/Debug";
SHELL = "/bin/bash";
"SQLITE_ENABLE_THREAD_ASSERTIONS" = 1;
"SQLITE_EXEMPT_PATH_FROM_VNODE_GUARDS" = "/Users/jerry/Library/WebKit/Databases";
"SSH_AUTH_SOCK" = "/private/tmp/com.apple.launchd.f5BaoIbV3p/Listeners";
"SWIFTUI_VIEW_DEBUG" = 287;
TMPDIR = "/var/folders/mh/szrw8j2x4x9b73d8jmv1vb0m0000gn/T/";
USER = "jerry";
"XPC_FLAGS" = 0x0;
"XPC_SERVICE_NAME" = "com.apple.dt.Xcode.5808";
"__CF_USER_TEXT_ENCODING" = "0x1F5:0x0:0x0";
"__XCODE_BUILT_PRODUCTS_DIR_PATHS" = "/Users/jerry/XcodeProj/TestOC/build/Debug";
"__XPC_DYLD_FRAMEWORK_PATH" = "/Users/jerry/XcodeProj/TestOC/build/Debug";
"__XPC_DYLD_LIBRARY_PATH" = "/Users/jerry/XcodeProj/TestOC/build/Debug";
}arguments:(
"/Users/jerry/XcodeProj/TestOC/build/Debug/TestOC"
)
7、<sys/sysctl.h>
获取系统启动时间
kernel_task是个系统级的task,包括多线程调度管理、虚拟内存、系统IO、线程之间通信等等,所以系统已启动,kernel_task就会跑起来,kernel_task运行的时间,就可以作为启动时间来使用。
sysctl函数用于获取kernel_task的信息,函数返回值:成功:返回0; 失败:返回-1
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;
}
#include<sys/param.h>
#include<sys/sysctl.h>
int sysctl( int *name, u_int namelen, void *oldp, size_t *oldenp, void *newp, size_t newlen );
返回0:成功 -1:失败
name参数是指定名字的一个整数数组,namelen参数指定了该数组中的元素数目。该数组
中的第一个元素指定本请求定向到内核的哪个子系统。第二个及其后元素依次细化指定该系
统的某个部分。为了获取某个值,oldp参数指向一个供内核存放该值的缓冲区。oldlenp则是一个值-结果参
数:函数被调用时,oldlenp指向的值指定该缓冲区的大小;函数返回时,该值给出内核存
放在该缓冲区中的数据量。如果这个缓冲不够大,函数就返回ENOMEM错误。作为特例,
Oldp可以是一个空指针,而oldlenp却是一个非空指针,内核确定这样的调用应该返回的数据量,并通过oldlenp返回这个大小。为了设置某个新值,newp参数指向一个大小为newlen参数值的缓冲区。如果不准备指定一
个新值,那么newp应为一个空指针,newlen因为0.