Coldfusion 从 UNIX 转换为人类可读时间,而无需转换时区之间的时间



我需要从UNIX转换为人类可读时间,但我不想像以下代码那样在时区之间转换时间:

<cffunction name="EpochToDate" hint="Returns Epoch from Date">
<cfargument name="dateSeconds" default="">
<cfscript>
// set the base time from when epoch time starts
startDate = createdatetime( '1970','01','01','00','00','00' );
if ( NOT isnumeric( arguments.dateSeconds ) )
return '';
// return the date
// this adds the seconds to the startDate and the converts it to to a local time from UTC format
return dateConvert( "utc2Local", dateadd( 's', arguments.dateSeconds, startDate ) );
</cfscript>
</cffunction>

例如,时间戳1532118000应返回 GMT:2018 年 7 月 20 日星期五晚上 8:20:00,而不是服务器的时区日期。

我认为您在这里需要的方法是首先将纪元时间从 UTC 转换为本地时间,然后对其进行操作。完成后,您可以将其转换回UTC时间。我在两台服务器上尝试过这个,一台位于 UTC 的转发时区,另一台位于后面。

<cffunction name="EpochToDate" hint="Returns Epoch from Date" output="true">
<cfargument name="dateSeconds" default="">
<cfscript>
// set the base time from when epoch time starts
startDate = DateConvert("utc2Local", CreateDateTime( 1970,1,1,0,0,0 ));
writedump(startDate);
if ( NOT isnumeric( arguments.dateSeconds ) )
return '';
// return the date
// this adds the seconds to the startDate and the converts it to to a local time from UTC format
return DateConvert("local2Utc", dateadd( 's', arguments.dateSeconds, startDate ));
</cfscript>
</cffunction>
<cfdump var="#EpochToDate(1532118000)#">

演示

世界协调时 +5:30{ts '1970-01-01 05:30:00'} {ts '2018-07-20 20:20:00'}

世界协调时 -7:00{ts '1969-12-31 17:00:00'} {ts '2018-07-20 20:20:00'}

最新更新