以编程方式获取系统安装日期
Get system installation date programmatically
我正在尝试通过 运行 控制台应用程序获取系统安装日期。
我知道如何做到这一点的唯一方法是解析 /var/log/install.log
文件以获取包含 OSInstaller
& Install Complete
项的最新字符串。
有方便的系统吗API我失踪了?
作为探索的建议仅:
您可以尝试查看文件 /System/Library/Receipts
。
您可能会看到一个 com.apple.pkg.BaseSystemResources.plist
文件,它的修改日期可能会告诉您 OS 安装的时间。
还有 com.apple.pkg.update.os.*.plist
个文件用于更新,再次查看修改日期,如果您可以确定命名约定,也许可以解析通配符 (*
) 位。
HTH,狩猎愉快!
万一有人觉得这有用。
Swift 3.0
解决方案 1
最初我解析 /var/log/install.log
文件以获取日期字符串:
// To get OS installation date we'll need to check system log file and find entry which contains installation date
var systemDates : String? = nil
do {
let fullSystemLog = try NSString(contentsOf: URL(fileURLWithPath: "/var/log/install.log"), encoding: String.Encoding.utf8.rawValue)
let entries = fullSystemLog.components(separatedBy: "\n")
//Filter to get only entries about OS installation
let filtered = entries.filter{ entry in
return entry.contains("OSInstaller") && entry.contains("Install Complete") //Markers telling that OS was installed
}
var latestMention = ""
if filtered.count > 0 {
//If 1 or more entries found we'll pick last one
latestMention = filtered.last!
}
else if entries.count > 0 {
//If there are 0 mentions of OS installation - we'll use first entry in logs
latestMention = entries.first!
}
//parse picked entry for date
do {
let regex = try NSRegularExpression(pattern: ".+:[0-9]{2}", options: [])
let nsString = latestMention as NSString
let results = regex.matches(in: latestMention,
options: [], range: NSMakeRange(0, nsString.length))
let actualDateSubstrings = results.map { nsString.substring(with: [=10=].range)}
if let dateStringFromMention = actualDateSubstrings.first {
systemDates = dateStringFromMention
}
else {
systemDates = "<Error: no date results>"
}
} catch let error as NSError {
systemDates = "<Error: invalid regex: \(error.localizedDescription)>"
}
}
catch {
systemDates = "<Error: system log file not found>"
}
print("\tSYSTEM INSTALLED: \(systemDates)")
解决方案 2
第二个解决方案看起来简单得多。查看 /System/Library/Receipts/com.apple.pkg.BaseSystemResources.plist
的 InstallDate
字段:
let systemDates = NSDictionary(contentsOfFile: "/System/Library/Receipts/com.apple.pkg.BaseSystemResources.plist")
print("\tSYSTEM INSTALLED: \(systemDates?["InstallDate"])")
我正在尝试通过 运行 控制台应用程序获取系统安装日期。
我知道如何做到这一点的唯一方法是解析 /var/log/install.log
文件以获取包含 OSInstaller
& Install Complete
项的最新字符串。
有方便的系统吗API我失踪了?
作为探索的建议仅:
您可以尝试查看文件 /System/Library/Receipts
。
您可能会看到一个 com.apple.pkg.BaseSystemResources.plist
文件,它的修改日期可能会告诉您 OS 安装的时间。
还有 com.apple.pkg.update.os.*.plist
个文件用于更新,再次查看修改日期,如果您可以确定命名约定,也许可以解析通配符 (*
) 位。
HTH,狩猎愉快!
万一有人觉得这有用。
Swift 3.0
解决方案 1
最初我解析 /var/log/install.log
文件以获取日期字符串:
// To get OS installation date we'll need to check system log file and find entry which contains installation date
var systemDates : String? = nil
do {
let fullSystemLog = try NSString(contentsOf: URL(fileURLWithPath: "/var/log/install.log"), encoding: String.Encoding.utf8.rawValue)
let entries = fullSystemLog.components(separatedBy: "\n")
//Filter to get only entries about OS installation
let filtered = entries.filter{ entry in
return entry.contains("OSInstaller") && entry.contains("Install Complete") //Markers telling that OS was installed
}
var latestMention = ""
if filtered.count > 0 {
//If 1 or more entries found we'll pick last one
latestMention = filtered.last!
}
else if entries.count > 0 {
//If there are 0 mentions of OS installation - we'll use first entry in logs
latestMention = entries.first!
}
//parse picked entry for date
do {
let regex = try NSRegularExpression(pattern: ".+:[0-9]{2}", options: [])
let nsString = latestMention as NSString
let results = regex.matches(in: latestMention,
options: [], range: NSMakeRange(0, nsString.length))
let actualDateSubstrings = results.map { nsString.substring(with: [=10=].range)}
if let dateStringFromMention = actualDateSubstrings.first {
systemDates = dateStringFromMention
}
else {
systemDates = "<Error: no date results>"
}
} catch let error as NSError {
systemDates = "<Error: invalid regex: \(error.localizedDescription)>"
}
}
catch {
systemDates = "<Error: system log file not found>"
}
print("\tSYSTEM INSTALLED: \(systemDates)")
解决方案 2
第二个解决方案看起来简单得多。查看 /System/Library/Receipts/com.apple.pkg.BaseSystemResources.plist
的 InstallDate
字段:
let systemDates = NSDictionary(contentsOfFile: "/System/Library/Receipts/com.apple.pkg.BaseSystemResources.plist")
print("\tSYSTEM INSTALLED: \(systemDates?["InstallDate"])")