-
Notifications
You must be signed in to change notification settings - Fork 0
/
date_diff.sh_.txt
48 lines (40 loc) · 1.29 KB
/
date_diff.sh_.txt
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
#!/bin/bash
if [ $# -ne 2 ]; then
echo Usage:
echo " $(basename $0) <datetime begin> <datetime end>"
echo Example:
echo " $(basename $0) '2012-08-12 15:34:59' '2012-08-13 01:23:45'"
echo " $(basename $0) '2012-08-12 15:34:59' 2012-08-13"
echo " $(basename $0) '2012-08-11 now"
echo
exit 1
fi
START="$1"
STOP="$2"
START_TS=$(date +'%s' -d "$START")
STOP_TS=$(date +'%s' -d "$STOP")
if [[ $STOP_TS -lt $START_TS ]]; then
echo "$(basename $0): min > max" >&2
exit 9
fi
ORE=$((($STOP_TS - $START_TS) / 3600 ))
MIN=$(((($STOP_TS - $START_TS) / 60 ) % 60))
SEC=$((($STOP_TS - $START_TS) % 60 ))
if [[ $ORE -gt 24 ]]; then
GIORNI=$(( $ORE / 24 ))
ORE_RIMANENTI=$(( $ORE % 24 ))
if [[ $GIORNI -gt 1 ]];then
DURATA="$GIORNI days $ORE_RIMANENTI:$MIN:$SEC"
else
DURATA="$GIORNI day $ORE_RIMANENTI:$MIN:$SEC"
fi
else
DURATA="$ORE:$MIN:$SEC"
fi
echo ""
echo "Begin : $(date +'%d-%m-%Y %H:%M:%S' -d@$START_TS)"
echo "End : $(date +'%d-%m-%Y %H:%M:%S' -d@$STOP_TS)"
echo ""
echo "Duration : $DURATA ($(echo $ORE:$MIN:$SEC) hours) ($(($STOP_TS - $START_TS)) seconds)"
echo ""
# vim: ts=2 sw=2 background=dark