当前位置: 技术问答>linux和unix
unix 下时间相互转换,求其相差秒数,多谢!
来源: 互联网 发布时间:2015-12-23
本文导语: 比方:date +"%y%d%m%H%M%S" 这种格式的两个时间如何求其相差秒数?如何用shell实现。多谢! | #!/bin/sh D1=20030202233107 D2=20030202233108 declare -a MONTHNAMES MONTHNAMES=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct...
比方:date +"%y%d%m%H%M%S" 这种格式的两个时间如何求其相差秒数?如何用shell实现。多谢!
|
#!/bin/sh
D1=20030202233107
D2=20030202233108
declare -a MONTHNAMES
MONTHNAMES=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
seconds () {
YEAR=${1:0:4}
MONTH=${MONTHNAMES[`expr ${1:4:2} - 1`]}
DAY=${1:6:2}
HOUR=${1:8:2}
MINUTE=${1:10:2}
SECOND=${1:12}
echo `date -d "$MONTH $DAY $HOUR:$MINUTE:$SECOND CST $YEAR" +%s`
}
expr `seconds $D2` - `seconds $D1`
D1=20030202233107
D2=20030202233108
declare -a MONTHNAMES
MONTHNAMES=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
seconds () {
YEAR=${1:0:4}
MONTH=${MONTHNAMES[`expr ${1:4:2} - 1`]}
DAY=${1:6:2}
HOUR=${1:8:2}
MINUTE=${1:10:2}
SECOND=${1:12}
echo `date -d "$MONTH $DAY $HOUR:$MINUTE:$SECOND CST $YEAR" +%s`
}
expr `seconds $D2` - `seconds $D1`
|
期待高手现身!