Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash将历元转换为日期,显示错误的时间_Bash_Date_Epoch - Fatal编程技术网

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

Bash将历元转换为日期,显示错误的时间,bash,date,epoch,Bash,Date,Epoch,为什么日期转换成了错误的时间 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 这个特定的时间戳是以毫秒为单位的,而不是以秒为单位的。除以10

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

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

这个特定的时间戳是以毫秒为单位的,而不是以秒为单位的。除以1000:

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

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

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

注意,“
value
”是一个bash变量,
$
是可选的;i、 例如,可以使用
$value
value

对于Mac OS X,它是
日期-r


1361234760790/(60*60*24*365)=43164.47年不确定这能解释什么?大概是43164+1970~=45105(更准确地说是43135.8+1970)所以这个日期是对的有趣的,这个日期怎么能正确打印?使用,我得到
GMT:Tue,2013年2月19日00:46:00GMT
这是期望的结果,它是由于时间戳(毫秒)。我也在这里进行了检查,得到了正确的结果,2013年2月19日,星期二,格林尼治标准时间上午12:46:00
$ date -r 1553024528
Tue Mar 19 12:42:08 PDT 2019
$ date -r `expr 1553024527882 / 1000`
Tue Mar 19 12:42:07 PDT 2019
$ date -r $((1553024527882/1000))
Tue Mar 19 12:42:07 PDT 2019