Bash 将纪元转换为日期,显示错误的时间

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

Bash convert epoch to date, showing wrong time

bashdateepoch

提问by bobbyrne01

How come date is converting to wrong time?

为什么日期转换为错误的时间?

result=$(ls /path/to/file/File.*)
#/path/to/file/File.1361234760790

currentIndexTime=${result##*.}
echo "$currentIndexTime"
#1361234760790

date -d@"$currentIndexTime"
#Tue 24 Oct 45105 10:53:10 PM GMT

回答by andrewdotn

This particular timestamp is in milliseconds since the epoch, not the standard seconds since the epoch. Divide by 1000:

这个特定的时间戳是自纪元以来的毫秒数,而不是自纪元以来的标准秒数。除以 1000:

$ date -d @1361234760.790
Mon Feb 18 17:46:00 MST 2013

回答by Richard Jessop

You can use bash arithmetic expansion to perform the division:

您可以使用 bash 算术扩展来执行除法:

date -d @$((value/1000))

Note that "value" is a bash variable with the $being optional; i.e., $valueor valuecan be used.

请注意,“ value” 是一个 bash 变量,它$是可选的;即,$valuevalue可以使用。

回答by Marcus

For Mac OS X, it's date -r <timestamp_in_seconds_with_no_fractions>

对于 Mac OS X,它是 date -r <timestamp_in_seconds_with_no_fractions>

$ date -r 1553024528
Tue Mar 19 12:42:08 PDT 2019

or

或者

$ date -r `expr 1553024527882 / 1000`
Tue Mar 19 12:42:07 PDT 2019

or

或者

$ date -r $((1553024527882/1000))
Tue Mar 19 12:42:07 PDT 2019