Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 以编程方式获取系统安装日期_Macos_Cocoa_Console Application_Macos Sierra - Fatal编程技术网

Macos 以编程方式获取系统安装日期

Macos 以编程方式获取系统安装日期,macos,cocoa,console-application,macos-sierra,Macos,Cocoa,Console Application,Macos Sierra,我试图通过运行控制台应用程序来获取系统安装日期。 我知道如何做到这一点的唯一方法是解析/var/log/install.log文件中包含OSInstaller&安装完成项的最新字符串 我缺少一个方便的系统API吗?仅作为探索的建议: 您可以尝试查看文件/System/Library/Receipts 您可能会看到一个com.apple.pkg.BaseSystemResources.plist文件,它的修改日期可能会告诉您操作系统的安装时间 还有com.apple.pkg.update.os.*

我试图通过运行控制台应用程序来获取系统安装日期。 我知道如何做到这一点的唯一方法是解析
/var/log/install.log
文件中包含
OSInstaller
&
安装完成
项的最新字符串


我缺少一个方便的系统API吗?

仅作为探索的建议

您可以尝试查看文件
/System/Library/Receipts

您可能会看到一个
com.apple.pkg.BaseSystemResources.plist
文件,它的修改日期可能会告诉您操作系统的安装时间

还有
com.apple.pkg.update.os.*.plist
文件用于更新,再次查看修改日期,如果可以确定命名约定,可以解析通配符(
*
)位


嗨,狩猎快乐

仅作为勘探建议

您可以尝试查看文件
/System/Library/Receipts

您可能会看到一个
com.apple.pkg.BaseSystemResources.plist
文件,它的修改日期可能会告诉您操作系统的安装时间

还有
com.apple.pkg.update.os.*.plist
文件用于更新,再次查看修改日期,如果可以确定命名约定,可以解析通配符(
*
)位


嗨,狩猎快乐

所以万一有人发现这很有用

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: $0.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)")

所以万一有人发现这很有用

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: $0.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)")

我很快在/System/Library/receives/com.apple.pkg.BaseSystemResources.p上找到了日期字符串‌​列表这比解析几千行日志要快得多。谢谢我很快在/System/Library/receives/com.apple.pkg.BaseSystemResources.p上找到了日期字符串‌​列表这比解析几千行日志要快得多。谢谢第二个解返回nil。第二个解返回nil。