Last active
March 4, 2025 08:37
-
-
Save besserwisser/66e64b1824af4a4133ddd2eb0bfeabd7 to your computer and use it in GitHub Desktop.
Get display on/off timestamps and differences (macos, zsh, per day)
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
# zsh function to get the first display on time and the last display off time of the day | |
# Also shows difference time difference between those both | |
# installation: put this function in your .zshrc file | |
# reload zshrc: source ~/.zshrc | |
# use it like this for today: when | |
# use it like this for yesterday: when 1 | |
# use it like this for the day before yesterday: when 2 | |
# use it like this for today with a break of 30 minutes: when 0 30 | |
# use it like this for the day before yesterday with a break of 30 minutes: when 2 30 | |
when() { | |
if ! [[ -z $1 || $1 =~ '^[0-9]+$' ]]; then | |
print "Usage: when [pastDayOffset]" | |
print "Use no argument or argument must be a number." | |
return | |
fi | |
pastDayOffset="0" | |
breakTimeInMinutes="30" | |
if [[ -n $1 ]]; then | |
pastDayOffset=$1 | |
fi | |
if [[ -n $2 ]]; then | |
breakTimeInMinutes=$2 | |
fi | |
endTimestamp=$(date +"%Y-%m-%d %H:%M:%S") | |
offsettedDate=$(date -v-"$pastDayOffset"d +"%Y-%m-%d") | |
startLine=$(pmset -g log | grep "$offsettedDate.*Display is turned on" | head -n 1) | |
startTimestamp=$(echo $startLine | cut -c 1-19) | |
if [ $1 != "0" ]; then | |
endLine=$(pmset -g log | grep "$offsettedDate.*Display is turned off" | tail -n 1) | |
endTimestamp=$(echo $endLine | cut -c 1-19) | |
fi | |
print "\nstart time: $startTimestamp" | |
print "end time: $endTimestamp\n" | |
startTimestampInSeconds=$(date -j -f "%Y-%m-%d %H:%M:%S" "$startTimestamp" +%s) | |
endInSeconds=$(date -j -f "%Y-%m-%d %H:%M:%S" "$endTimestamp" +%s) | |
endInSecondsWithBreak=$(($endInSeconds - ($breakTimeInMinutes * 60))) | |
difference="$(($endInSeconds - $startTimestampInSeconds))" | |
differenceWithBreak="$(($endInSecondsWithBreak - $startTimestampInSeconds))" | |
humanReadableDifference=$(date -u -r $difference +"%T") | |
humanReadableDifferenceWithBreak=$(date -u -r $differenceWithBreak +"%T") | |
printf "Time from first display on: $humanReadableDifference\n" | |
printf "Time from first display on minus break of $breakTimeInMinutes minutes: $(tput setaf 5)$humanReadableDifferenceWithBreak\n\n" | |
} | |
# zsh function to get the first display on time and the last display off time of the day | |
# Also shows difference time difference between those both | |
# installation: put this function in your .zshrc file | |
# reload zshrc: source ~/.zshrc | |
# use it like this for today: when | |
when_simple() { | |
pastDayOffset="0" | |
breakTimeInMinutes="30" | |
endTimestamp=$(date +"%Y-%m-%d %H:%M:%S") | |
offsettedDate=$(date -v-"$pastDayOffset"d +"%Y-%m-%d") | |
startLine=$(pmset -g log | grep "$offsettedDate.*Display is turned on" | head -n 1) | |
startTimestamp=$(echo $startLine | cut -c 1-19) | |
if [ -z "$startTimestamp" ]; then | |
echo "No display on event found for the given date." | |
return 1 | |
fi | |
print "\nstart time: $startTimestamp" | |
print "end time: $endTimestamp\n" | |
startTimestampInSeconds=$(date -j -f "%Y-%m-%d %H:%M:%S" "$startTimestamp" +%s) | |
endInSeconds=$(date -j -f "%Y-%m-%d %H:%M:%S" "$endTimestamp" +%s) | |
endInSecondsWithBreak=$((endInSeconds - (breakTimeInMinutes * 60))) | |
difference=$((endInSeconds - startTimestampInSeconds)) | |
differenceWithBreak=$((endInSecondsWithBreak - startTimestampInSeconds)) | |
humanReadableDifference=$(date -u -r $difference +"%T") | |
humanReadableDifferenceWithBreak=$(date -u -r $differenceWithBreak +"%T") | |
printf "Time from first display on: $humanReadableDifference\n\n" | |
printf "Time from first display on minus break of $breakTimeInMinutes minutes: $humanReadableDifferenceWithBreak\n\n" | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment