Home
Back To Top
Home
Requirement:-
To work on date/time difference calculation along with time conversion to EPOC from Normal and vice versa
Inputs:-
# sh date.sh
Usage: date.sh {ETON|NTOE|COM}
: for ETON - EPOC/UNIX Format value (i.e. 1448337029 )
: for NTOE - Normal Format value (i.e. 23 Nov 2015 09:50:29 24 hour clock )
: for COM - Normal Format two values (i.e. 23 Nov 2015 09:50:29 24 hour clock )
Outputs:-
# sh date.sh COM
Normal Time values Comparision for difference calculation in Seconds
Normal Format date value1 (i.e. 23 Nov 2015 09:50:29 24 hour clock ): 23 Nov 2015 09:50:29
Normal Format date value2 (i.e. 23 Nov 2015 09:50:29 24 hour clock ): 23 Nov 2015 08:50:29
1448293829 is greater than 1448290229 and difference is of 3600 seconds
# sh date.sh NTOE
Normal to Epoc Time Conversion
Normal Format value (i.e. 23 Nov 2015 09:50:29 24 hour clock ):23 Nov 2015 09:50:29
Normal to Epoc Time Conversion
1448293829
# sh date.sh ETON
Epoc to Normal Time Conversion
EPOC/UNIX Format value (i.e. 1448337029 ):1448337029
Mon 23 Nov 2015 09:50:29 PM CST
Mon-Nov-23-21.50.29-CST-2015
Script:-
#!/bin/bash
# To check whether input argument is passed to the script or not
####
epoctonormal ()
{
VALUE=$1
if [ $# -ne 1 ]
then
echo "Function Usage Example: epoctonormal 1448337029 "
else
# To convert input argument data of Unix Epoc to Normal Timestamp pattern
date -d @$VALUE +%c # Format with spaces like "Mon 23 Nov 2015 09:50:29 PM CST"
# To convert input argument data of Unix Epoc to Normal Timestamp pattern without any space (For filename usage)
date -d @$VALUE +%a-%b-%e-%H.%M.%S-%Z-%Y # Format with spaces removed like "Mon-Nov-23-21.50.29-CST-2015"
fi
}
####
normaltoepoc ()
{
VALUE=$@
if [ $# -eq 0 ] && [ $# -gt 4 ]
then
echo "Function Usage Example: normaltoepoc 23 Nov 2015 09:50:29 "
exit
else
date -d "$VALUE" +%s
fi
}
####
comparenormal ()
{
if [ $# -eq 0 ] && [ $# -gt 8 ]
then
echo "Function Usage Example: comparedate VALUE1 VALUE2"
exit
fi
VALUE1=`date -d "$1 $2 $3 $4 " +%s`
VALUE2=`date -d "$5 $6 $7 $8 " +%s`
let DIFF=$VALUE1-$VALUE2
DIFF=${DIFF/-/}
if [ $VALUE1 -gt $VALUE2 ]
then
echo $VALUE1 is greater than $VALUE2 and difference is of $DIFF seconds
elif [ $VALUE1 -eq $VALUE2 ]
then
echo $VALUE1 is equal to $VALUE2
else
echo $VALUE1 is smaller than $VALUE2 and difference is of $DIFF seconds
fi
}
####
main ()
{
case "$1" in
ETON)
echo "Epoc to Normal Time Conversion"
echo -n "EPOC/UNIX Format value (i.e. 1448337029 ):"; read VALUE
epoctonormal $VALUE
;;
NTOE)
echo "Normal to Epoc Time Conversion"
echo -n "Normal Format value (i.e. 23 Nov 2015 09:50:29 24 hour clock ):"; read VALUE
normaltoepoc $VALUE
;;
COM)
echo "Normal Time values Comparision for difference calculation in Seconds"
echo -n "Normal Format date value1 (i.e. 23 Nov 2015 09:50:29 24 hour clock ): "; read VALUE1
echo -n "Normal Format date value2 (i.e. 23 Nov 2015 09:50:29 24 hour clock ): "; read VALUE2
comparenormal $VALUE1 $VALUE2
;;
*)
echo "Usage: $0 {ETON|NTOE|COM}"
echo " : for ETON - EPOC/UNIX Format value (i.e. 1448337029 )"
echo " : for NTOE - Normal Format value (i.e. 23 Nov 2015 09:50:29 24 hour clock )"
echo " : for COM - Normal Format two values (i.e. 23 Nov 2015 09:50:29 24 hour clock )"
esac
}
####
main $1
Home
No comments:
Post a Comment