Linux 将人类可读的 Epoch 日期转换为变量

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

Getting human readable date from Epoch into variable

linuxbashepoch

提问by W3BGUY

Okay, this is probably a very basic question; but, I'm just getting back in the saddle with Linux.

好的,这可能是一个非常基本的问题;但是,我刚回到 Linux 的马鞍上。

I have a variable that hold an Epoch time called pauseTime. I need that variable to become human readable (something like 2012-06-13 13:48:30).

我有一个变量,它保存一个名为 pauseTime 的 Epoch 时间。我需要该变量成为人类可读的(类似于 2012-06-13 13:48:30)。

I know I can just type in

我知道我可以输入

date -d @133986838 //just a random number there

and that will print something similar. But I need to get the variable to hold that human readable date, instead of the epoch time... I keep running into errors with everything I'm trying. Any thoughts on how I can do this?

这将打印类似的东西。但是我需要让变量来保存那个人类可读的日期,而不是纪元时间......我在尝试的每件事中都遇到了错误。关于我如何做到这一点的任何想法?

采纳答案by 0xC0000022L

Well do this:

那么这样做:

VARIABLENAME=$(date -d @133986838)

and then

进而

export VARIABLENAME

or in Bash do directly:

或在 Bash 中直接执行:

export VARIABLENAME=$(date -d @133986838)

If you want formatting, say in the usual ISO format:

如果要格式化,请使用通常的 ISO 格式:

export ISODATE=$(date -d @133986838 +"%Y-%m-%d %H:%M:%S")
# or
EPOCHDATE=133986838
export ISODATE=$(date -d @$EPOCHDATE +"%Y-%m-%d %H:%M:%S")

The formats behind the +are explained in man date.

后面的格式+man date.

Note: $()is the modern form of the backticks. I.e. catching output from a command into a variable. However, $()makes some things easier, most notably escaping rules inside of it. So you should always prefer it over backticks if your shell understands it.

注意:$()是现代形式的反引号。即将命令的输出捕获到变量中。然而,$()让一些事情变得更容易,最显着的是逃避其中的规则。所以如果你的 shell 理解它,你应该总是喜欢它而不是反引号。