In recent time, when dealing with shell scripts, encountered time processing problems. The addition and subtraction of time, as well as the calculation of timing.
1. Time plus minus
The way to do this is to turn the base time into a timestamp, and then you need to add or change time to seconds.
For example: 1990-01-01 01:01:01 plus 1 hours 20 minutes
Processing method:
A. Converting the base time to time stamp
time1=$ (Date +%s-d ' 1990-01-01 01:01:01 ')
Echo $time 1
631126861 "Time Stamp"
B. Increase the time into seconds
[Root@localhost ~]# time2=$ ((1*60*60+20*60))
[Root@localhost ~]# Echo $time 2
4800
C. Two time added to calculate the result time
time1=$ (($time 1+ $time 2))
time1=$ (date +%y-%m-%d\%h:%m:%s-d "1970-01-01 UTC $time 1 Seconds");
Echo $time 1
1990-01-01 02:21:01
2. Calculation method of time difference
such as: 2010-01-01 and 2009-01-01 11:11:11 time lag
Principle: Also turn into a timestamp, and then calculate the day, time, minutes, seconds
Time1=$ (($ (Date +%s-d ' 2010-01-01 ')-$ (Date +%s-d ' 2009-01-01 11:11:11 '));
Echo time1
Will time1/60 seconds, will become divided.
Supplementary Note:
Shell single bracket operator symbol:
a=$ (date);
Equivalent to: a= ' Date ';
Double bracket Operator:
a=$ ((1+2));
echo $a;
Equivalent to:
A= ' expr 1 + 2 '
Http://www.cnblogs.com/chengmo/archive/2010/07/13/1776473.html