从命令行的Unix时间转换(Convert Unix timestamp to a date string)

编程入门 行业动态 更新时间:2024-10-27 00:26:31
命令行的Unix时间转换(Convert Unix timestamp to a date string)

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

“日期”可能有效,除了指定每个元素(月,日,年,小时等)相当尴尬,我无法弄清楚如何使其正常工作。 似乎可能会有一个更简单的方法 - 我错过了什么吗?

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

date might 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?

最满意答案

您可以使用strftime()格式化Unix时间戳。 它不是直接从shell,但我们可以通过gawk访问它。 %c说明符以区域设置相关的方式显示时间戳。

echo $TIMESTAMP | gawk '{print strftime("%c", $0)}' # echo 0 | gawk '{print strftime("%c", $0)}' Wed 31 Dec 1969 07:00:00 PM EST

另外,GNU的date语法更简单:

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

(从: BASH:将Unix时间戳转换为日期 )

With GNU's date you can do:

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

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

On OS X, use date -r.

date -r "$TIMESTAMP"

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

echo "$TIMESTAMP" | gawk '{print strftime("%c", $0)}' # echo 0 | gawk '{print strftime("%c", $0)}' Wed 31 Dec 1969 07:00:00 PM EST

更多推荐

本文发布于:2023-07-06 07:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047544.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令行   时间   Unix   Convert   date

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!