获取当前时间,以秒为单位,从Linux上的历元开始,Bash

时间:2022-04-01 15:59:36

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

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

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

date似乎没有提供这样的选择。有简单的方法吗?

6 个解决方案

#1


792  

This should work:

这应该工作:

date +%s

#2


100  

Just to add.

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

补充一下,获取自纪元以来的秒数(1970年1月1日)。g 1973年10月21日)。

date -d "Oct 21 1973" +%s


Convert the number of seconds back to date

将秒数转换为日期

date --date @120024000


The command date is 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复制——帮助)。在美国西海岸显示当地时间为下周五上午9点

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

Better yet, take some time to read the man page http://www.manpages.info/linux/date.1.html

更好的是,花一些时间阅读手册页面http://www.manpages.info/linux/date.1.html

#3


24  

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

到目前为止,所有的答案都使用外部程序日期。

Since Bash 4.2, printf has a new modifier %(dateformat)T that, when used with argument -1 outputs the current date with format given by dateformat, handled by strftime(3) (man 3 strftime for 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 -1 nonetheless).

(尽管如此,总是给出-1的论点可能更明智)。

If you use -2 as argument instead of -1, Bash will use the time the shell was started instead of the current date (but why would you want this?).

如果您使用-2作为参数而不是-1,Bash将使用shell启动的时间而不是当前日期(但是为什么您要这样做呢?)

#4


14  

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

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

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 date will take whatever date part you gave it and add the current time 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.

请注意,为了完整起见,我已将时间部分添加到格式中。原因是日期将使用您提供的任何日期部分,并将当前时间添加到所提供的值中。例如,如果您在下午4:19执行上述命令,而没有使用“00:00:00”部分,它将自动增加时间。这样,“1973年10月21日”将被解析为“1973年10月21日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

苹果的man页面实现日期:https://developer.apple.com/library/mac/documentation/darwination/reference/manpages/man1/date.html

#5


8  

With most Awk implementations:

与大多数Awk实现:

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

#6


1  

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

使用此bash脚本(my ~/bin/epoch):

#!/bin/bash

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

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

#1


792  

This should work:

这应该工作:

date +%s

#2


100  

Just to add.

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

补充一下,获取自纪元以来的秒数(1970年1月1日)。g 1973年10月21日)。

date -d "Oct 21 1973" +%s


Convert the number of seconds back to date

将秒数转换为日期

date --date @120024000


The command date is 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复制——帮助)。在美国西海岸显示当地时间为下周五上午9点

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

Better yet, take some time to read the man page http://www.manpages.info/linux/date.1.html

更好的是,花一些时间阅读手册页面http://www.manpages.info/linux/date.1.html

#3


24  

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

到目前为止,所有的答案都使用外部程序日期。

Since Bash 4.2, printf has a new modifier %(dateformat)T that, when used with argument -1 outputs the current date with format given by dateformat, handled by strftime(3) (man 3 strftime for 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 -1 nonetheless).

(尽管如此,总是给出-1的论点可能更明智)。

If you use -2 as argument instead of -1, Bash will use the time the shell was started instead of the current date (but why would you want this?).

如果您使用-2作为参数而不是-1,Bash将使用shell启动的时间而不是当前日期(但是为什么您要这样做呢?)

#4


14  

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

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

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 date will take whatever date part you gave it and add the current time 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.

请注意,为了完整起见,我已将时间部分添加到格式中。原因是日期将使用您提供的任何日期部分,并将当前时间添加到所提供的值中。例如,如果您在下午4:19执行上述命令,而没有使用“00:00:00”部分,它将自动增加时间。这样,“1973年10月21日”将被解析为“1973年10月21日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

苹果的man页面实现日期:https://developer.apple.com/library/mac/documentation/darwination/reference/manpages/man1/date.html

#5


8  

With most Awk implementations:

与大多数Awk实现:

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

#6


1  

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

使用此bash脚本(my ~/bin/epoch):

#!/bin/bash

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

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