Skip to content

Instantly share code, notes, and snippets.

@pofat
Last active June 20, 2019 17:20
Show Gist options
  • Save pofat/4d81660fa078c25f57247427148c042f to your computer and use it in GitHub Desktop.
Save pofat/4d81660fa078c25f57247427148c042f to your computer and use it in GitHub Desktop.
Get the timestamp of creation time of the process which runs your application in iOS
func getProcessStartTime() -> TimeInterval {
let pid = ProcessInfo.processInfo.processIdentifier
var procInfo = kinfo_proc()
var cmd: [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, pid]
var size = MemoryLayout.stride(ofValue: procInfo)
if sysctl(&cmd, UInt32(cmd.count), &procInfo, &size, nil, 0) == 0 {
// tv_sec is timestamp measured in second; tv_usec is the rest fraction part in microsecond
return Double(procInfo.kp_proc.p_un.__p_starttime.tv_sec) * 1000.0 + Double(procInfo.kp_proc.p_un.__p_starttime.tv_usec) / 1000.0
} else {
print("Can't get information of process \(pid)")
return 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment