重击:从给定的时间中减去10分钟 [英] Bash: subtracting 10 mins from a given time

查看:60
本文介绍了重击:从给定的时间中减去10分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在bash脚本中,如果我有一个表示时间的数字,形式为hhmmss(或hmmss),减去10分钟的最佳方法是什么?

In a bash script, if I have a number that represents a time, in the form hhmmss (or hmmss), what is the best way of subtracting 10 minutes?

即90000-> 85000

ie, 90000 -> 85000

推荐答案

这有点棘手.日期可以执行一般操作,即您可以执行以下操作:

This is a bit tricky. Date can do general manipulations, i.e. you can do:

date --date '-10 min'

指定时分秒(使用UTC,因为否则它似乎假定为PM):

Specifying hour-min-seconds (using UTC because otherwise it seems to assume PM):

date --date '11:45:30 UTC -10 min'

要拆分日期字符串,我唯一想到的方法是扩展子字符串:

To split your date string, the only way I can think of is substring expansion:

a=114530
date --date "${a:0:2}:${a:2:2}:${a:4:2} UTC -10 min"

如果您只想找回hhmmss:

And if you want to just get back hhmmss:

date +%H%M%S --date "${a:0:2}:${a:2:2}:${a:4:2} UTC -10 min"

这篇关于重击:从给定的时间中减去10分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆