Search code examples
macosawkterminalgrepdscl

(MacOS Terminal). How to produce a readable Epoch time from terminal


I am trying to give a command to a friend who has been having account passwords changed. He is trying to determine a way to figure out when the password was last changed. I was able to give him this command, that is working in MacOS terminal, but I want to make it a bit nicer by providing a readable answer. Here is the command:

dscl . read /Users/username accountPolicyData | grep -A1 SetTime          

which results in something like this:

    <key>passwordLastSetTime</key> 
    <real>1670348364.4110398</real>

This command is pulling the reset date and time great, but him having to search out an epoch time calculator may be a bit over his head. My question:

Do any of you have any idea how I could strip out the bracketed text and convert the epoch time from the commandline? I'm happy to drop the title line if that helps if doing so would allow a numerical conversion that is readable.

Thanks for any suggestion you may have.


Solution

  • You can use defaults to read plist :

    #!/bin/bash
    
    file=$(mktemp /tmp/XXXXXXXX.plist)
    dscl . read /Users/username accountPolicyData | tail -n +2 > $file 
    c=$(defaults read $file passwordLastSetTime)
    date -r ${c%.*} '+%Y-%m-%d %H:%M:%S'
    rm $file