Linux Shell 时间运算以及时间差计算方法
2017-05-27 09:35
260 查看
最近一段时间,在处理Shell 脚本时候,遇到时间的处理问题。 时间的加减,以及时间差的计算。
1。 时间加减
这里处理方法,是将基础的时间转变为时间戳,然后,需要增加或者改变时间,变成 秒。
如:1990-01-01 01:01:01 加上 1小时 20分
处理方法:
a.将基础时间转为时间戳
time1=$(date +%s -d '1990-01-01
01:01:01')
echo $time1
631126861 【时间戳】
b.将增加时间变成秒
[root localhost ~]#
time2=$((1*60*60+20*60))
[root localhost ~]#
echo $time2
4800
c.两个时间相加,计算出结果时间
time1=$(($time1+$time2))
time1=$(date +%Y-%m-%d\ %H:%M:%S -d "1970-01-01
UTC $time1 seconds");
echo $time1
1990-01-01 02:21:01
2。时间差计算方法
如:2010-01-01 与 2009-01-01 11:11:11 时间差
原理:同样转成时间戳,然后计算天,时,分,秒
time1=$(($(date +%s -d '2010-01-01') - $(date +%s -d '2009-01-01
11:11:11')));
echo time1
将time1 / 60 秒,就变成分了。
补充说明:
shell 单括号运算符号:
a=$(date);
等同于:a=date;
双括号运算符:
a=$((1+2));
echo $a;
等同于:
a=expr 1 + 2
1。 时间加减
这里处理方法,是将基础的时间转变为时间戳,然后,需要增加或者改变时间,变成 秒。
如:1990-01-01 01:01:01 加上 1小时 20分
处理方法:
a.将基础时间转为时间戳
time1=$(date +%s -d '1990-01-01
01:01:01')
echo $time1
631126861 【时间戳】
b.将增加时间变成秒
[root localhost ~]#
time2=$((1*60*60+20*60))
[root localhost ~]#
echo $time2
4800
c.两个时间相加,计算出结果时间
time1=$(($time1+$time2))
time1=$(date +%Y-%m-%d\ %H:%M:%S -d "1970-01-01
UTC $time1 seconds");
echo $time1
1990-01-01 02:21:01
2。时间差计算方法
如:2010-01-01 与 2009-01-01 11:11:11 时间差
原理:同样转成时间戳,然后计算天,时,分,秒
time1=$(($(date +%s -d '2010-01-01') - $(date +%s -d '2009-01-01
11:11:11')));
echo time1
将time1 / 60 秒,就变成分了。
补充说明:
shell 单括号运算符号:
a=$(date);
等同于:a=date;
双括号运算符:
a=$((1+2));
echo $a;
等同于:
a=expr 1 + 2
相关文章推荐
- linux shell 时间运算以及时间差计算方法
- linux shell 时间运算以及时间差计算方法
- linux shell 时间运算以及时间差计算方法
- linux shell 时间运算以及时间差计算方法
- linux shell 时间运算以及时间差计算方法
- linux shell 时间运算以及时间差计算方法
- linux shell 时间运算以及时间差计算方法
- linux shell 时间运算以及时间差计算方法
- linux shell date 时间运算以及时间差计算方法
- linux shell 时间运算以及时间差计算方法
- linux shell 时间运算以及时间差计算方法
- linux shell 时间运算以及时间差计算方法
- linux shell 时间运算以及时间差计算方法
- 获取时间方法,以及计算两个整型日期之间的天数
- C#中几种比较时间以及计算时间差的方法
- php计算时间差的函数以及方法
- js的移位运算以及二进制补码反码的计算方法
- Shell时间运算及时间差计算方法
- 队列应用银行排队问题模拟:计算客户的平均停留时间和等待时间以及每个客户的时间信息,两种方法实现
- Shell时间运算及时间差计算方法