Skip to content

Instantly share code, notes, and snippets.

@brunerd
Created August 2, 2021 23:44
Show Gist options
  • Save brunerd/68b34131053bbe7dcf50d7cdb38a5394 to your computer and use it in GitHub Desktop.
Save brunerd/68b34131053bbe7dcf50d7cdb38a5394 to your computer and use it in GitHub Desktop.
Working with CFAbsoluteTime and epoch time in shell
#work with CFAbsoluteTime in shell
#https://developer.apple.com/documentation/corefoundation/cfabsolutetime
#https://developer.apple.com/documentation/corefoundation/1543542-cfabsolutetimegetcurrent
#get epoch time for CFAbsoluteTime 0 (978307200)
#same as the constant kCFAbsoluteTimeIntervalSince1970 in https://github.com/opensource-apple/CF/blob/master/CFDate.c
/bin/date -j -f "%b %d %T %Z %Y" "Jan 1 00:00:00 GMT 2001" "+%s"
#Now in CFAbsoluteTime
echo $(( $(date +"%s") - 978307200 ))
#convert CFAbsoluteTime to epoch
#use parameter expansion to truncate mantissa
CFAbsoluteTime=""
echo $(( 978307200 + ${CFAbsoluteTime%.*} ))
#convert CFAbsoluteTime time to normal date stamp
#use parameter expansion to truncate mantissa
CFAbsoluteTime=""
date -r $(( 978307200 + ${CFAbsoluteTime%.*} ))
#in UTC
date -ur $(( 978307200 + ${CFAbsoluteTime%.*} ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment