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

Coldfusion convert from UNIX to human readable time without converting the time between the time zones

我需要从 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 应该 return 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)#">

DEMO

UTC +5:30 {ts '1970-01-01 05:30:00'} {ts '2018-07-20 20:20:00'}

UTC -7:00 {ts '1969-12-31 17:00:00'} {ts '2018-07-20 20:20:00'}