如何在 Bash 中将时间戳转换为日期?

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

How to convert timestamps to dates in Bash?

bashdateunix-timestamp

提问by chiborg

I need a shell command or script that converts a Unix timestamp to a date. The input can come either from the first parameter or from stdin, allowing for the following usage patterns:

我需要一个将 Unix 时间戳转换为日期的 shell 命令或脚本。输入可以来自第一个参数或标准输入,允许以下使用模式:

ts2date 1267619929

and

echo 1267619929 | ts2date

Both commands should output "Wed Mar 3 13:38:49 2010".

这两个命令都应该输出“Wed Mar 3 13:38:49 2010”。

回答by a'r

On later versions of common Linux distributions you can use:

在常见 Linux 发行版的更高版本上,您可以使用:

date -d @1267619929

回答by rafaelzlisboa

date -r <number>

works for me on Mac OS X.

在 Mac OS X 上对我有用。

回答by Paused until further notice.

This version is similar to chiborg'sanswer, but it eliminates the need for the external ttyand cat. It uses date, but could just as easily use gawk. You can change the shebang and replace the double square brackets with single ones and this will also run in sh.

此版本类似于chiborg 的答案,但它消除了对外部ttycat. 它使用date,但也可以很容易地使用gawk。您可以更改shebang并将双方括号替换为单个方括号,这也将在sh.

#!/bin/bash
LANG=C
if [[ -z "" ]]
then
    if [[ -p /dev/stdin ]]    # input from a pipe
    then
        read -r p
    else
        echo "No timestamp given." >&2
        exit
    fi
else
    p=
fi
date -d "@$p" +%c

回答by ghostdog74

You can use GNU date, for example,

您可以使用 GNU 日期,例如,

$ sec=1267619929
$ date -d "UTC 1970-01-01 $sec secs"

or

或者

$ date -ud @1267619929

回答by codaddict

You can use this simple awk script:

您可以使用这个简单的 awk 脚本:

#!/bin/gawk -f   
{ print strftime("%c", 
$ echo '1098181096' | ./a.awk 
Tue 19 Oct 2004 03:18:16 AM PDT
$
); }

Sample usage:

示例用法:

$ printf '%(%c)T\n' 1267619929
Wed 03 Mar 2010 01:38:49 PM CET

回答by gniourf_gniourf

Since Bash 4.2 you can use printf's %(datefmt)Tformat:

从 Bash 4.2 开始,您可以使用printf's%(datefmt)T格式:

#!/bin/bash

if (($#)); then
    printf '%(%c)T\n' "$@"
else
    while read -r line; do
        printf '%(%c)T\n' "$line"
    done
fi

That's nice, because it's a shell builtin. The format for datefmtis a string accepted by strftime(3)(see man 3 strftime). Here %cis:

这很好,因为它是一个内置的 shell。datefmt的格式是strftime(3)(请参阅man 3 strftime)接受的字符串。这%c是:

%cThe preferred date and time representation for the current locale.

%c当前语言环境的首选日期和时间表示。



Now if you want a script that accepts an argument and, if none is provided, reads stdin, you can proceed as:

现在,如果您想要一个接受参数的脚本,并且如果没有提供,则读取标准输入,您可以继续:

tail -f <log file> | gawk \
'{ printf strftime("%c", ); for (i=2; i<NF; i++) printf $i " "; print $NF }'

回答by user3544224

I use this when converting log files or monitoring them:

我在转换日志文件或监视它们时使用它:

$  date; date +%s; date -r `date +%s`
Tue Oct 24 16:27:42 CDT 2017
1508880462
Tue Oct 24 16:27:42 CDT 2017

回答by Daniel Farrell

In OSX, or BSD, there's an equivalent -rflag which apparently takes a unix timestamp. Here's an example that runs date four times: once for the first date, to show what it is; one for the conversion to unix timestamp with %s, and finally, one which, with -r, converts what %sprovides back to a string.

在 OSX 或 BSD 中,有一个等效的-r标志,它显然采用 unix 时间戳。这是一个运行四次 date 的示例:一次是第一次约会,以显示它是什么;一个用于使用 转换为 unix 时间戳%s,最后一个用于-r%s提供的内容转换回字符串。

$ uname -a
Darwin XXX-XXXXXXXX 16.7.0 Darwin Kernel Version 16.7.0: Thu Jun 15 17:36:27 PDT 2017; root:xnu-3789.70.16~2/RELEASE_X86_64 x86_64

At least, seems to work on my machine.

至少,似乎在我的机器上工作。

date +'%Y-%m-%d %H:%M:%S' -d "@timestamp"

回答by andranikasl

You can get formatted date from timestamp like this

您可以像这样从时间戳中获取格式化日期

#!/bin/bash
LANG=C
if [[ -z "" ]]
then
    if [[ -p /dev/stdin ]]    # input from a pipe
    then
        cat - | gawk '{ print strftime("%c", ); }'
    else
        echo "No timestamp given." >&2
        exit
    fi
else
    date -d @ +%c
fi

回答by webb

In this answer I copy Dennis Williamson's answer and modify it slightly to allow a vast speed increase when piping a column of many timestamps to the script. For example, piping 1000 timestamps to the original script with xargs -n1 on my machine took 6.929s as opposed to 0.027s with this modified version:

在这个答案中,我复制了丹尼斯·威廉姆森的答案并对其稍作修改,以在将包含许多时间戳的列输送到脚本时允许大幅提高速度。例如,在我的机器上使用 xargs -n1 将 1000 个时间戳传送到原始脚本需要 6.929 秒,而这个修改后的版本需要 0.027 秒:

##代码##