Bash 脚本/命令在 5 分钟之前/之后打印出日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20688664/
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-10 00:33:54  来源:igfitidea点击:

Bash script/command to print out date 5 min before/after

bash

提问by user117974

I need to somehow use the date command in bash or another utility to print out the date and time, 5 minutes before and 5 minutes after a given value. For example:

我需要以某种方式使用date命令在bash或其他工具来打印出日期和时间,前5分钟,给定值后的5分钟。例如:

input:

输入:

Thu Dec 19 14:10

output:

输出:

Thu Dec 19 14:05 Thu Dec 19 14:10 Thu Dec 19 14:15

I see that the datecommand can be used to do this on the current date/time, can it be used with a passed value, i.e. not the current date/time?

我看到,date命令可以用来做这在当前的日期/时间,才能将其与传递的值,即不是当前的日期/时间使用?

回答by Rafa

You can achieve this, for the current time, by typing.

您可以在当前时间通过键入来实现此目的。

$ date --date='5 minutes ago'; date; date --date='5 minutes'
Qui Dez 19 16:09:17 BRST 2013
Qui Dez 19 16:14:17 BRST 2013
Qui Dez 19 16:19:17 BRST 2013

To use a specific date (ex 1978/01/10).

使用特定日期(例如 1978/01/10)。

$ date --date='1978-01-10 + 5 minutes'
Ter Jan 10 00:05:00 BRT 1978

回答by chepner

With GNU date, you can do a simple form of date/time arithmetic with the argument to the --dateoption:

使用 GNU date,您可以使用--date选项的参数执行简单形式的日期/时间算术:

$ date --date 'now + 5 minutes'

With BSD date(at least, the version that ships with Mac OS X), the -voption allows you to do something similar, but only with the current time:

随着BSD date(至少,该版本附带的Mac OS X),该-v选项允许你做同样的事情,但只有与当前时间:

$ date -v +5M
$ date -v -5M

Otherwise, you need to perform the math explicitly after converting the time to a UNIX timestamp (seconds since Jan 1, 1970):

否则,您需要在将时间转换为 UNIX 时间戳(自 1970 年 1 月 1 日以来的秒数)后显式执行数学运算:

$ date -r $(( $(date +%s) + 300 ))   # 5 minutes (300 seconds) from now
$ date -r $(( $(date +%s) - 300 ))   # 5 minutes ago

回答by Donovan

If you're using bash under linux, you can use the -dparameter to perform date manipulation on an existingdate:

如果您在 linux 下使用 bash,则可以使用该-d参数对现有日期执行日期操作:

  1. Get the EPOCH time for the date in question:

    EPOCH=$(date -d 'Thu Dec 19 14:10' '+%s')
    

    This gives you the time, in seconds, since the EPOCH (typically 01/01/1970)

  2. Now you can use simple math to subtract or add 5 minutes (in seconds) to the EPOCH time

    NEW_EPOCH=$(($EPOCH - 300))
    

    obviously, there are 300 seconds in 5 minutes

  3. Now convert this NEW_EPOCH back into a human readable date

    NEW_DATE=$(date -d "1970-01-01 ${NEW_EPOCH} sec")
    
  1. 获取相关日期的 EPOCH 时间:

    EPOCH=$(date -d 'Thu Dec 19 14:10' '+%s')
    

    这为您提供了自 EPOCH(通常为 01/01/1970)以来的时间(以秒为单位

  2. 现在您可以使用简单的数学运算将 EPOCH 时间减去或增加 5 分钟(以秒为单位)

    NEW_EPOCH=$(($EPOCH - 300))
    

    很明显,5分钟有300秒

  3. 现在将此 NEW_EPOCH 转换回人类可读的日期

    NEW_DATE=$(date -d "1970-01-01 ${NEW_EPOCH} sec")
    

NOTE that this only works on unix systems which support the date -d option (i.e. Linux)

请注意,这仅适用于支持 date -d 选项的 unix 系统(即 Linux)

回答by Benjamin W.

If you want to do this for the current time +/-5 minutes and you use Bash 4.2 or newer, you can do it without external tools:

如果您想在当前时间 +/-5 分钟内执行此操作,并且您使用 Bash 4.2 或更高版本,则无需外部工具即可执行此操作:

$ printf -v now '%(%s)T'
$ echo "$now"
1516161094
$ f='%a %b %d %R'
$ printf "%($f)T %($f)T %($f)T\n" "$((now-300))" "$now" "$((now+300))"
Tue Jan 16 22:46 Tue Jan 16 22:51 Tue Jan 16 22:56

The %(datefmt)Tformatting string of printfallows to print date-time strings. If the argument is skipped (like here) or is -1, the current time is used.

的格式化字符串允许打印日期时间字符串。如果参数被跳过(如这里)或 is ,则使用当前时间。%(datefmt)Tprintf-1

%sformats the time in seconds since the epoch, and -v nowstores the output in nowinstead of printing it.

%s以秒为单位格式化时间,并将-v now输出存储在其中now而不是打印出来。

fis just a convenience variable so I don't have to repeat the formatting string for the output three times.

f只是一个方便的变量,所以我不必重复输出的格式字符串三遍。

Since the argument for this usage of printfhas to be in seconds since the epoch, you're stuck with external tools to convert an input string like Thu Dec 19 14:10into that format and you'd replace

由于这种用法的参数printf必须是自纪元以来的几秒钟内,因此您必须使用外部工具将输入字符串Thu Dec 19 14:10转换为该格式,并且您将替换

printf -v now '%(%s)T'

with, for example, any of

例如,与任何

now=$(date '+%s' -d 'Thu Dec 19 14:10')                   # GNU date
now=$(date -j -f '%a %b %d %T' 'Thu Dec 19 14:10' '+%s')  # macOS date