bash 在 Mac OSX 上将 unix 纪元时间转换为人类可读的日期 - BSD

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

Convert unix epoch time to human readable date on Mac OSX - BSD

bashdateunixepoch

提问by Nehal Shah

On my Mac OSX, my bash script has a epoch time 123439819723. I am able to convert the date to human readable format by date -r 123439819723which gives me Fri Aug 26 09:48:43 EST 5881.

在我的 Mac OSX 上,我的 bash 脚本有一个 epoch time 123439819723。我能够将日期转换为人类可读的格式,通过date -r 123439819723它给我Fri Aug 26 09:48:43 EST 5881.

But I want the date to be in mm/ddd/yyyy:hh:mi:ssformat. The date --dateoption doesn't work on my machine.

但我希望日期采用mm/ddd/yyyy:hh:mi:ss格式。该date --date选项在我的机器上不起作用。

回答by mike.dld

Here you go:

干得好:

# date -r 123439819723 '+%m/%d/%Y:%H:%M:%S'
08/26/5881:17:48:43

回答by chepner

To convert a UNIX epoch time with OS X date, use

要使用 OS X 转换 UNIX 纪元时间date,请使用

date -j -f %s 123439819723

The -jprevents datefrom trying to set the system clock, and -fspecifies the inputformat. You can add +<whatever>to set the outputformat, as with GNU date.

-j防止date从试图设置系统时钟,以及-f指定的输入格式。您可以添加+<whatever>以设置输出格式,就像 GNU 一样date

回答by UserszrKs

from command Shell

从命令外壳

[aks@APC ~]$ date -r 1474588800
Fri Sep 23 05:30:00 IST 2016

[aks@APC ~]$ date -ur 1474588800
Fri Sep 23 00:00:00 UTC 2016

[aks@APC ~]$ echo "1474588800" | xargs -I {} date -jr {} -u
Fri Sep 23 00:00:00 UTC 2016

回答by 0x8BADF00D

Combined solution to run on Mac OS.

在 Mac OS 上运行的组合解决方案。

Shell code:

外壳代码:

T=123439819723 
D=$(date -j -f %s  $(($T/1000)) '+%m/%d/%Y:%H:%M:%S').$(($T%1000))
echo "[$T] ==> [$D]"

Output:

输出:

[123439819723] ==> [11/29/1973:11:50:19.723]

Or one line:

或一行:

> echo 123439819723 | { read T; D=$(date -j -f %s  $(($T/1000)) '+%m/%d/%Y:%H:%M:%S').$(($T%1000));  echo "[$T] ==> [$D]" }
[123439819723] ==> [11/29/1973:11:50:19.723]