From 8ae5eca160cf6ba99c091d8e9f981eb1222221c1 Mon Sep 17 00:00:00 2001 From: B Stack Date: Sun, 9 Jun 2019 23:01:49 -0400 Subject: initial commit --- on-battery-time.sh | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 on-battery-time.sh (limited to 'on-battery-time.sh') diff --git a/on-battery-time.sh b/on-battery-time.sh new file mode 100755 index 0000000..6e901dc --- /dev/null +++ b/on-battery-time.sh @@ -0,0 +1,50 @@ +#!/bin/sh +# File: /usr/local/bin/on-battery-time.sh +# Author: bgstack15 +# Startdate: 2019-06-09 22:35 +# Title: Report how long this system has been on battery or AC power +# Package: on-battery-time +# Purpose: +# History: +# Usage: +# Reference: +# https://stackoverflow.com/questions/12199631/convert-seconds-to-hours-minutes-seconds/18683834#18683834 +# Improve: +# Dependencies: +# bc +# date +# the udev rules from 99-on-battery-time.rules +# Documentation: + +OBT_INFILE=/var/log/battery.log + +# FUNCTIONS +convertsecs() { + _h=$( expr $1 / 3600 ) + _m=$( expr $1 % 3600 / 60 ) + _s=$( expr $1 % 60 ) + printf "%02d:%02d:%02d\n" ${_h} ${_m} ${_s} +} + +# learn most recent utility time +last_utility_time="$( { tac "${OBT_INFILE}" | awk '/utility power/{print $2}' ; echo 0 ; } | head -n1 )" + +# learn most recent battery time +last_battery_time="$( { tac "${OBT_INFILE}" | awk '/on battery/{print $2}' ; echo 0 ; } | head -n1 )" + +# learn current time +OBT_current_time="$( date "+%s" )" + +if test ${last_utility_time} -ge ${last_battery_time} ; +then + OBT_last="${last_utility_time}" + OBT_phrase="utility power" +else + OBT_last="${last_battery_time}" + OBT_phrase="on battery" +fi + +OBT_seconds="$( printf "%s-%s\n" "${OBT_current_time}" "${OBT_last}" | bc )" + +printf "%s seconds: %s\n" "${OBT_phrase}" "${OBT_seconds}" +convertsecs "${OBT_seconds}" -- cgit