栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Bash脚本:两次之间的分钟差

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Bash脚本:两次之间的分钟差

一个纯bash解决方案:

old=09:11new=17:22# feeding variables by using read and splitting with IFSIFS=: read old_hour old_min <<< "$old"IFS=: read hour min <<< "$new"# convert hours to minutes# the 10# is there to avoid errors with leading zeros# by telling bash that we use base 10total_old_minutes=$((10#$old_hour*60 + 10#$old_min))total_minutes=$((10#$hour*60 + 10#$min))echo "the difference is $((total_minutes - total_old_minutes)) minutes"

使用的另一种解决方案

date
(我们使用小时/分钟,因此日期并不重要)

old=09:11new=17:22IFS=: read old_hour old_min <<< "$old"IFS=: read hour min <<< "$new"# convert the date "1970-01-01 hour:min:00" in seconds from Unix EPOCH timesec_old=$(date -d "1970-01-01 $old_hour:$old_min:00" +%s)sec_new=$(date -d "1970-01-01 $hour:$min:00" +%s)echo "the difference is $(( (sec_new - sec_old) / 60)) minutes"

参见http://en.wikipedia.org/wiki/Unix_time



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/374882.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号