bash 将 Unix 时间戳转换为日期字符串

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

Convert Unix timestamp to a date string

bashshellunixunix-timestamp

提问by chimeracoder

Is there a quick, one-liner way to convert a Unix timestamp to a date from the Unix command line?

是否有一种快速的单行方式将 Unix 时间戳从 Unix 命令行转换为日期?

datemight work, except it's rather awkward to specify each element (month, day, year, hour, etc.), and I can't figure out how to get it to work properly. It seems like there might be an easier way - am I missing something?

date可能会起作用,除了指定每个元素(月、日、年、小时等)相当尴尬,而且我不知道如何让它正常工作。似乎有一种更简单的方法 - 我错过了什么吗?

回答by John Kugelman

With GNU's dateyou can do:

使用 GNU,date您可以执行以下操作:

date -d "@$TIMESTAMP"
# date -d @0
Wed Dec 31 19:00:00 EST 1969

(From: BASH: Convert Unix Timestamp to a Date)

(来自:BASH:将 Unix 时间戳转换为日期

On OS X, use date -r.

在 OS X 上,使用date -r.

date -r "$TIMESTAMP"

Alternatively, use strftime(). It's not available directly from the shell, but you can access it via gawk. The %cspecifier displays the timestamp in a locale-dependent manner.

或者,使用strftime(). 它不能直接从 shell 获得,但您可以通过gawk访问它。的%c说明符显示区域设置依赖性方式的时间戳。

echo "$TIMESTAMP" | gawk '{print strftime("%c", 
# echo 0 | gawk '{print strftime("%c", 
date -d '1970-01-01 1357004952 sec UTC'
Mon Dec 31 17:49:12 PST 2012
)}' Wed 31 Dec 1969 07:00:00 PM EST
)}'
date -r 1357004952

回答by qbi

date -d @1278999698 +'%Y-%m-%d %H:%M:%S'Where the number behind @is the number in seconds

date -d @1278999698 +'%Y-%m-%d %H:%M:%S'后面@的数字是以秒为单位的数字

回答by dabest1

This solution works with versions of date which do not support date -d @. It does not require AWKor other commands. A Unix timestamp is the number of seconds since Jan 1, 1970, UTC so it is important to specify UTC.

此解决方案适用于不支持date -d @. 它不需要AWK或其他命令。Unix 时间戳是自 1970 年 1 月 1 日 UTC 以来的秒数,因此指定 UTC 很重要。

date +%s
1357004952

If you are on a Mac, then use:

如果您使用的是 Mac,请使用:

echo $TIMESTAMP | perl -nE 'say scalar gmtime $_'

Command for getting epoch:

获取纪元的命令:

$ date -d '1970-01-01 1416275583 sec GMT'
Tue Nov 18 00:53:03 GMT 2014
$ date -d '1970-01-01 1416275583 sec UTC'
Tue Nov 18 01:53:03 GMT 2014

Credit goes to Anton: BASH: Convert Unix Timestamp to a Date

归功于Anton:BASH:将 Unix 时间戳转换为日期

回答by Greg A. Woods

As @TomMcKenzie says in a comment to another answer, date -r 123456789is arguably a more common (i.e. more widely implemented) simple solution for times given as seconds since the Unix Epoch, but unfortunately there's no universal guaranteed portable solution.

正如@TomMcKenzie 在对另一个答案的评论中所说的那样,date -r 123456789对于自 Unix Epoch 以来以秒为单位给出的时间,可以说是一种更常见(即更广泛实施)的简单解决方案,但不幸的是,没有通用的保证便携式解决方案。

The -doption on many types of systems means something entirely different than GNU Date's --dateextension. Sadly GNU Date doesn't interpret -rthe same as these other implementations. So unfortunately you have to know which version of dateyou're using, and many older Unix datecommands don't support either option.

-d许多类型的系统上的选项意味着与 GNU Date 的--date扩展完全不同的东西。遗憾的是,GNU Date 的解释-r与这些其他实现不同。所以不幸的是,您必须知道date您使用的是哪个版本,而且许多较旧的 Unixdate命令不支持任一选项。

Even worse, POSIX daterecognizes neither -dnor -rand provides no standard way in any command at all (that I know of) to format a Unix time from the command line (since POSIX Awk also lacks strftime()). (You can't use touch -tand lsbecause the former does not accept a time given as seconds since the Unix Epoch.)

更糟糕的是,POSIXdate既不识别-d也不-r提供任何命令(据我所知)从命令行格式化 Unix 时间的标准方法(因为 POSIX Awk 也缺少strftime())。(您不能使用touch -t并且ls因为前者不接受自 Unix Epoch 以来以秒形式给出的时间。)

Note though The One True Awk available direct from Brian Kernighan does now have the strftime()function built-in as well as a systime()function to return the current time in seconds since the Unix Epoch), so perhaps the Awk solution is the most portable.

请注意,尽管直接从 Brian Kernighan 获得的 The One True Awk 现在确实具有strftime()内置函数以及systime()返回自 Unix Epoch 以来以秒为单位的当前时间的函数),因此也许 Awk 解决方案是最便携的。

回答by qbi

If you find the notation awkward, maybe the -R-option does help. It outpouts the date in RFC 2822 format. So you won't need all those identifiers: date -d @1278999698 -R. Another possibility is to output the date in seconds in your locale: date -d @1278999698 +%c. Should be easy to remember. :-)

如果你觉得这个符号很尴尬,也许 - 选项-R确实有帮助。它以 RFC 2822 格式输出日期。所以你不需要所有这些标识符:date -d @1278999698 -R. 另一种可能性是在您的语言环境中以秒为单位输出日期:date -d @1278999698 +%c。应该很容易记住。:-)

回答by William Pursell

The standard Perl solution is:

标准的 Perl 解决方案是:

date -r 1305712800

(or localtime, if preferred)

(或当地时间,如果愿意)

回答by Tony Mountifield

Slight correction to dabest1's answer above. Specify the timezone as UTC, not GMT:

对上面 dabest1 的回答稍作修正。将时区指定为 UTC,而不是 GMT:

function unixts() { date -d "@"; }

The second one is correct. I think the reason is that in the UK, daylight saving was in force continually from 1968 to 1971.

第二个是正确的。我认为原因是在英国,夏令时从 1968 年到 1971 年一直有效。

回答by denmojo

Other examples here are difficult to remember. At its simplest:

这里的其他例子很难记住。最简单的:

$ unixts 1551276383

Wed Feb 27 14:06:23 GMT 2019

回答by Dangelov

Put the following in your ~/.bashrc :

将以下内容放入您的 ~/.bashrc :

awk 'BEGIN { print strftime("%c", 1271603087); }'

Example usage:

用法示例:

##代码##

回答by Sjoerd

##代码##