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

问题描述 投票:1回答:1

我需要从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而不是服务器的时区日期。

coldfusion timestamp epoch
1个回答
2
投票

我认为你需要的方法是先将纪元时间从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

组织+ 5:30 Cakesupoei

Tissue-7:00 ckssoupi

© www.soinside.com 2019 - 2024. All rights reserved.