简体   繁体   English

使用bash计算时间(以秒为单位)

[英]Calculating time in seconds using bash

I have cron job which processes data every 15 minutes(12:00, 12:15, etc...) I need a bash function/script which determines how many seconds until the next processing cycle relative to the current time. 我有cron作业,每15分钟(12:00、12:15等)处理数据一次,我需要一个bash函数/脚本来确定相对于当前时间的下一个处理周期要多少秒。 If current time = "15:09:00 2016" the next processing cycle would be 360 sec. 如果当前时间=“ 15:09:00 2016”,则下一个处理周期将为360秒。 Any ideas? 有任何想法吗? thanks. 谢谢。

Get the current time in seconds since the UNIX epoch 获取自UNIX时代以来的当前时间(以秒为单位)

$ now=$(date +%s)

then compute that value mod 900 (900 seconds is 15 minutes) and subtract that from 900. 然后计算该值mod 900(900秒为15分钟),然后从900中减去该值。

$ echo $((900 - now % 900))

The date command allows a date to be provided following the -d --date option. date命令允许在-d --date选项之后提供日期。 date also understands relative dates (eg + 6 min , +3 days , etc..). date还可以理解相对日期(例如+ 6 min+3 days等)。 So if you need to know what 6 minutes in the future is you can simply use date -d "+ 6 min" to find the exact time that will be. 因此,如果您需要知道将来的6分钟是什么,您可以简单地使用date -d "+ 6 min"查找确切的时间。 eg 例如

$ date
Fri Jun 10 15:22:45 CDT 2016

$ date -d "+ 6 min"
Fri Jun 10 15:28:47 CDT 2016

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM