Skip to content

Instantly share code, notes, and snippets.

@buger
Last active July 26, 2017 21:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save buger/e5831566f6c9d63256c95e54a83f8066 to your computer and use it in GitHub Desktop.
Save buger/e5831566f6c9d63256c95e54a83f8066 to your computer and use it in GitHub Desktop.
Bash time tracker
active_time() {
# Query display logs
logs=$(/usr/bin/pmset -g log | grep 'Display is turned')
# By default Mac turn off monitor after 15 minutes
timeout=900
# Read logs line by line
# Line format: 2016-07-12 07:50:02 +0300 Notification Display is turned on
echo "$logs" | while IFS= read -r line; do
d=$(echo $line | cut -c 1-19); # extract full date
ts=$(date -j -f "%Y-%m-%d %H:%M:%S" "$d" "+%s"); # get unix date
h=$(echo "scale=1; ($ts-$p_ts-900)/3600" | bc -l); # calculate ~ hours
sd=$(echo $line | cut -c 1-11); # extract day for pretty print
if [ "$h" != "0" ] && [[ "$h" != *"-"* ]]
then
echo "$sd - $h hours";
fi
p_ts=$ts; # remember previous date
done
}
active_time
# MacBook-Pro:~ buger$ active_time
# 2016-07-12 - .6 hours
# 2016-07-12 - 2.9 hours
# 2016-07-12 - 4.1 hours
# 2016-07-12 - 2.5 hours
# 2016-07-12 - .2 hours
# 2016-07-12 - 1.1 hours
# 2016-07-13 - 8.3 hours
# 2016-07-13 - .7 hours
# 2016-07-13 - .3 hours
# 2016-07-13 - .4 hours
# 2016-07-13 - 2.3 hours
# 2016-07-13 - .7 hours
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment