Search code examples
iosshellcore-datatimestamp

How to get apple's CF Absolute Time OR core data supported timestamp using terminal shell command?


date +"%s" - this command is giving output like: 1623847472

I want output like: 645540272 using shell command

I have tried date command's mostly all options but no success yet.


Solution

  • The date command doesn't have this option because it's not originally a macOS command line tool and uses a different reference date. It also doesn't support using alternate reference dates. You would need to adjust the value to make up the difference.

    The date command uses the Unix time reference of January 1, 1970 UTC. Core Data and most other macOS and iOS frameworks use the Mac reference date of January 1, 2001 UTC. The difference between these reference dates is 978307200 seconds.

    If you're using bash, you can get the number of seconds since the macOS reference date with

    echo $((`date +"%s"` - 978307200))
    

    That takes the Unix reference date, which %s prints, and subtracts the difference between the two reference dates to get the time since the macOS reference date.