bash 回合分钟到 5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22783007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:04:16  来源:igfitidea点击:

bash round minutes to 5

bashtimestamp

提问by user3408380

We have this string to retrieve date and time in clean format.

我们有这个字符串以干净的格式检索日期和时间。

 TIMESTAMP=$(date "+%Y%m%d%H%M")

Is there some inline code that we can use to round the minute to 5 down. So if it is 12:03 it will make it 12:00 and if it is 12:49 it will be 12:45

是否有一些内联代码可以用来将分钟四舍五入到 5。因此,如果是 12:03 则是 12:00,如果是 12:49 则是 12:45

Thank you!

谢谢!

采纳答案by Josh Jolly

To do this you can subtract the minutes modulo 5 from the total minutes. To print it out:

为此,您可以从总分钟数中减去模数 5 的分钟数。打印出来:

echo "$(date "+%Y%m%d%H%M") - ($(date +%M)%5)" | bc

To save it to a variable:

要将其保存到变量:

my_var=$(echo "$(date "+%Y%m%d%H%M") - ($(date +%M)%5)" | bc)

This relies on your date format string remaining as it is now - a string of numbers.

这取决于您现在保留的日期格式字符串 - 一串数字。

Example output:

示例输出:

$ date "+%Y%m%d%H%M"
201404010701
$ echo "$(date "+%Y%m%d%H%M") - ($(date +%M)%5)" | bc
201404010700

回答by glenn Hymanman

A little string manipulation:

一个小的字符串操作:

case $TIMESTAMP in 
    *[1234]) TIMESTAMP=${TIMESTAMP%?}0;; 
    *[6789]) TIMESTAMP=${TIMESTAMP%?}5;; 
esac

${TIMESTAMP%?}removes the last character. Ref: http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

${TIMESTAMP%?}删除最后一个字符。参考:http: //www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

回答by hroptatyr

Not exactly bash only but using dateutils' droundutility, it boils down to:

不完全是 bash 而是使用dateutils 的 dround实用程序,它归结为:

$ dround now -5m
2014-04-01T10:35:00

or with your format specifiers:

或使用您的格式说明符:

$ dround now -5m -f '%Y%m%d%H%M'
201404011035

Disclaimer: I am the author of that project.

免责声明:我是该项目的作者。

回答by fedorqui 'SO stop harming'

You can use the integer division and multiply back to get the round value.

您可以使用整数除法并乘回以获取舍入值。

$ m=23
$ (( m /= 5, m *= 5 )) && echo $m
20

Integer division just return the integer part of the result, so in the case above is returning 4. Then multiplying by 5 gives 20.

整数除法只返回结果的整数部分,所以在上面的例子中返回 4。然后乘以 5 得到 20。

In your case:

在你的情况下:

$ TIMESTAMP=$(date "+%Y%m%d%H%M")
$ echo $TIMESTAMP
201404011231
$ (( TIMESTAMP /= 5, TIMESTAMP *= 5 ))
$ echo $TIMESTAMP
201404011230

$ TIMESTAMP=$(date "+%Y%m%d%H%M")
$ echo $TIMESTAMP
201404011257
$ (( TIMESTAMP /= 5, TIMESTAMP *= 5 ))
$ echo $TIMESTAMP
201404011255

回答by Jasen

using the remainder operator the we can determine the number of excess minutes, that can then be subtracted from the original:

使用余数运算符,我们可以确定超出的分钟数,然后可以从原始值中减去:

TIMESTAMP=$(date "+%Y%m%d%H%M")
(( TIMESTAMP -= TIMESTAMP%15 ))
echo $TIMESTAMP

this only works because both 100 and 60 are divisible by 5 if 15 minutes was wanted instead of 5 the expression would be more complex:

这只有效,因为如果需要 15 分钟而不是 5,则 100 和 60 都可以被 5 整除,表达式会更复杂:

(( TIMESTAMP -= TIMESTAMP%100%5 ))

回答by xerostomus

The solution of Fedorqui is very smart and versatile:

Fedorqui 的解决方案非常智能且通用:

seconds=$(date +%s) 
echo $seconds $(date --date="@$seconds" +%H:%M:%S) # say 1559449650 # 06:27:30
# (( seconds /= 3600, seconds *= 3600 )) # rounds to this hour
# (( seconds += 3600, seconds /= 3600, seconds *= 3600 )) # rounds to next hour
(( seconds += 1800, seconds /= 1800, seconds *= 1800 )) # rounds to next half of a hour
next_hour=$(date --date="@$seconds" +%H:%M:%S)
echo $seconds $next_hour # say 1559449800 06:30:00