-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalctime.sh
More file actions
executable file
·74 lines (64 loc) · 1.56 KB
/
calctime.sh
File metadata and controls
executable file
·74 lines (64 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#! /bin/bash
# Simple script for calculating amount of time spend on task
# Parses uptime to format 'hh mm'
function uptime_raw(){
UPTIME=`uptime | awk '{print $3}' | sed 's/,//g' | sed 's/:/ /g'`
echo "$UPTIME"
}
# Formats time
function format_time(){
HOURS=$(($1 / 60))
MINUTES=$(($1 % 60))
if [ $HOURS -eq 0 ]
then
echo "$MINUTES""m"
else
echo "$HOURS""h""$MINUTES""m"
fi
}
# Formats uptime to format '1h23m'
function format_uptime(){
uptime=`uptime_in_mins`
echo "`format_time $uptime`"
}
# Converts current uptime to minutes
function uptime_in_mins(){
time=`uptime_raw`
echo "`time_to_mins $time`"
}
# Converts time to minutes args:
function time_to_mins(){
if [ $# -eq 1 ]
then
echo "$1"
else
echo $(($1 * 60 + 10#$2))
fi
}
if [ $# -gt 0 ]
then
matching=`echo "$1" | sed -n '/^\([[:digit:]]\?[[:digit:]]h\)\?\([[:digit:]]\?[[:digit:]]m\)\?$/p'`
if [ ${#matching} -gt 0 ]
then
TIME=`echo "$1" | sed 's/h/ 0/g' | sed 's/m//g'`
TIME_IN_MINS=`time_to_mins $TIME`
UPTIME_IN_MINS=`uptime_in_mins`
DIFF_MIN=$((UPTIME_IN_MINS - TIME_IN_MINS))
format_time $DIFF_MIN
else
if [ $1 == "--help" ]
then
echo "Usage:"
echo " calctime - prints foramted uptime"
echo " calctime [formated_time] - prints differenc between uptime and passed time"
echo " eg: calctime 1h23m"
echo
echo "Time Formating"
echo "yyhxxm - yy hours and xx minutes"
else
echo "Wrong arg '$1' see '--help' for instructions."
fi
fi
else
format_uptime
fi