Last active
July 26, 2017 21:13
-
-
Save buger/e5831566f6c9d63256c95e54a83f8066 to your computer and use it in GitHub Desktop.
Bash time tracker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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