Coldfusion конвертирует из UNIX в удобочитаемое время без преобразования времени между часовыми поясами - PullRequest
0 голосов
/ 23 мая 2018

Мне нужно преобразовать 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 должна возвращатьВремя по Гринвичу: пятница, 20 июля 2018 года, 20:20, а не дата часового пояса сервера.

1 Ответ

0 голосов
/ 23 мая 2018

Я думаю, что подход, который вам нужен, заключается в том, чтобы сначала преобразовать время эпохи из 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'}

...