获取自 Linux、Bash 上的 Epoch 以来的当前时间(以秒为单位)

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

Get current time in seconds since the Epoch on Linux, Bash

linuxbashdatetime

提问by n-alexander

I need something simple like date, but in seconds since 1970 instead of the current date, hours, minutes, and seconds.

我需要一些简单的东西,比如date, 但以 1970 年以来的秒为单位,而不是当前的日期、小时、分钟和秒。

datedoesn't seem to offer that option. Is there an easy way?

date似乎没有提供该选项。有没有简单的方法?

采纳答案by Steef

This should work:

这应该有效:

date +%s

回答by pellucide

Just to add.

Get the seconds since epoch(Jan 1 1970) for any given date(e.g Oct 21 1973).

只是为了补充。

获取任何给定日期(例如 1973 年 10 月 21 日)的自纪元(1970 年 1 月 1 日)以来的秒数。

date -d "Oct 21 1973" +%s


Convert the number of seconds back to date


将秒数转换回日期

date --date @120024000


The command dateis pretty versatile. Another cool thing you can do with date(shamelessly copied from date --help). Show the local time for 9AM next Friday on the west coast of the US


该命令date非常通用。你可以用日期做的另一件很酷的事情(无耻地复制自date --help)。显示美国西海岸下周五上午 9 点的当地时间

date --date='TZ="America/Los_Angeles" 09:00 next Fri'

Better yet, take some time to read the man page http://man7.org/linux/man-pages/man1/date.1.html

更好的是,花一些时间阅读手册页 http://man7.org/linux/man-pages/man1/date.1.html

回答by PeqNP

This is an extension to what @pellucide has done, but for Macs:

这是@pellucide 所做的扩展,但对于 Mac:

To determine the number of seconds since epoch (Jan 1 1970) for any given date (e.g. Oct 21 1973)

确定任何给定日期(例如 1973 年 10 月 21 日)自纪元(1970 年 1 月 1 日)以来的秒数

$ date -j -f "%b %d %Y %T" "Oct 21 1973 00:00:00" "+%s"
120034800

Please note, that for completeness, I have added the time part to the format. The reason being is that datewill take whatever date part you gave it and add the currenttime to the value provided. For example, if you execute the above command at 4:19PM, without the '00:00:00' part, it will add the time automatically. Such that "Oct 21 1973" will be parsed as "Oct 21 1973 16:19:00". That may not be what you want.

请注意,为了完整起见,我在格式中添加了时间部分。原因是date它将采用您提供的任何日期部分并将当前时间添加到提供的值中。例如,如果您在下午 4:19 执行上述命令,没有“00:00:00”部分,它会自动添加时间。这样“Oct 21 1973”将被解析为“Oct 21 1973 16:19:00”。那可能不是您想要的。

To convert your timestamp back to a date:

要将时间戳转换回日期:

$ date -j -r 120034800
Sun Oct 21 00:00:00 PDT 1973

Apple's man page for the date implementation: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/date.1.html

Apple 的日期实现手册页:https: //developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/date.1.html

回答by gniourf_gniourf

So far, all the answers use the external program date.

到目前为止,所有答案都使用外部程序date

Since Bash 4.2, printfhas a new modifier %(dateformat)Tthat, when used with argument -1outputs the current date with format given by dateformat, handled by strftime(3)(man 3 strftimefor informations about the formats).

从 Bash 4.2 开始,printf有一个新的修饰符%(dateformat)T,当与参数一起使用时,-1输出当前日期,格式由 给出dateformat,由strftime(3)(处理man 3 strftime有关格式的信息)。

So, for a pure Bash solution:

因此,对于纯 Bash 解决方案:

printf '%(%s)T\n' -1

or if you need to store the result in a variable var:

或者如果您需要将结果存储在变量中var

printf -v var '%(%s)T' -1

No external programs and no subshells!

没有外部程序,也没有子shell!

Since Bash 4.3, it's even possible to not specify the -1:

从 Bash 4.3 开始,甚至可以不指定-1

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

(but it might be wiser to always give the argument -1nonetheless).

(但始终提出论点可能更明智-1)。

If you use -2as argument instead of -1, Bash will use the time the shell was started instead of the current date. This can be used to compute elapsed times

如果您使用-2as 参数而不是-1,Bash 将使用 shell 的启动时间而不是当前日期。这可用于计算经过的时间

$ printf -v beg '%(%s)T\n' -2
$ printf -v now '%(%s)T\n' -1
$ echo beg=$beg now=$now elapsed=$((now-beg))
beg=1583949610 now=1583953032 elapsed=3422

回答by comonad

use this bash script (my ~/bin/epoch):

使用这个 bash 脚本(我的~/bin/epoch):

#!/bin/bash

# get seconds since epoch
test "x" == x && date +%s && exit 0

# or convert epoch seconds to date format (see "man date" for options)
EPOCH=""
shift
date -d @"$EPOCH" "$@"

回答by Steven Penny

With most Awk implementations:

对于大多数 awk 实现:

awk 'BEGIN {srand(); print srand()}'

回答by Socowi

Pure bash solution

纯 bash 解决方案

Since bash5.0 (released on 7 Jan 2019) you can use the built-in variable EPOCHSECONDS.

bash5.0(2019 年 1 月 7 日发布)开始,您可以使用内置变量EPOCHSECONDS

$ echo $EPOCHSECONDS
1547624774

There is also EPOCHREALTIMEwhich includes fractions of seconds.

还有EPOCHREALTIME其中包括几分之一秒。

$ echo $EPOCHREALTIME
1547624774.371215

EPOCHREALTIMEcan be converted to micro-seconds (μs) by removing the decimal point. This might be of interest when using bash's built-in arithmetic (( expression ))which can only handle integers.

EPOCHREALTIME可以通过去除小数点将其转换为微秒 (μs)。当使用只能处理整数bash的内置算术时,这可能很有趣(( expression ))

$ echo ${EPOCHREALTIME/./}
1547624774371215

In all examples from above the printed time values are equal for better readability. In reality the time values would differ since each command takes a small amount of time to be executed.

在上面的所有示例中,打印的时间值相等以提高可读性。实际上,时间值会有所不同,因为每个命令都需要少量时间来执行。