Bash: subtracting 10 mins from a given time

后端 未结 4 1647
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-14 08:02

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?

ie, 90000 -> 85000

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-14 08:34

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

    date --date '-10 min'
    

    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"
    

    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"
    

提交回复
热议问题