Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freemarker model convert timestamp in milliseconds to date

I have a csv-file which i want to transform with fmpp (freemarker). The first column is a long value (milliseconds since 1.1.1970) which i want to convert into a date and format it as datetime.

src format:

timeStamp,elapsed,label,responseCode,threadName,dataType,success,bytes,URL,Latency
1319115474244,40142,Login,200,Login 1-2,text,true,862184,http://localhost:8080/xxx,5378

desirable target format:

timeStamp;elapsed;label;responseCode;threadName;dataType;success;bytes;URL;Latency
20.12.2011 13:45;40142;Login;200;Login 1-2;text;true;862184;http://localhost:8080/xxx;5378

My (running) template:

<#list csv.headers as h>${h}<#if h_has_next>;</#if></#list>
<#list csv as row>
<#list csv.headers as h><#if h_index == 0>Do the date magic<#else>${(row[h]!"N/A")?string}</#if>$<#if h_has_next>;</#if></#list>
</#list>

For column 0 I want to do the conversion. I DON'T want to write a new model which contains a date. My question is, can this be done in the template without modifying freemarker or fmpp.

any ideas?

like image 443
Andreas Avatar asked Oct 21 '11 07:10

Andreas


2 Answers

FreeMarker 2.3.17 has introduced ?number_to_date, ?number_to_time and ?number_to_datetime for that. See: http://freemarker.org/docs/ref_builtins_expert.html#ref_builtin_numToDate

You will also want to set the date/time format and zone; see http://fmpp.sourceforge.net/settings.html#sect17

Mayble you will have to upgrade FreeMarker in FMPP. For that, simply replace the <FMPP_HOME>/lib/freemarker.jar with the latest version.

like image 80
ddekany Avatar answered Nov 06 '22 10:11

ddekany


In my situation, I use this:

${(timeStamp)?number_to_date?string("yyyy.MM.dd")}

And replace number_to_date with number_to_datetime or number_to_time;

And you can replace yyyy.MM.dd with YYYY-MM-dd HH:mm:ss as you need.

check this: http://freemarker.org/docs/ref_builtins_expert.html#ref_builtin_numToDate

like image 5
Jerry Ni Avatar answered Nov 06 '22 10:11

Jerry Ni