6

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?

Andreas
  • 1,183
  • 1
  • 11
  • 24

2 Answers2

26

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.

ddekany
  • 29,656
  • 4
  • 57
  • 64
  • 2
    thanx! I must have overseen this in the documentation. One hint for others. You have to convert the String first to a number and then to a datetime like this: ${(row[h])?number?number_to_datetime} . The datetime format for output can be set with: <#setting datetime_format="yyyy-MM-dd hh:mm:ss"> – Andreas Oct 21 '11 at 11:24
  • Thanks! Being in the "Seldom used and expert built-ins" section, one can accurately say this information was buried. Much appreciated! – MaxRocket Aug 14 '18 at 15:19
  • 1
    @MaxRocket Yeah, most of that section should probably be dissolved in the others... – ddekany Aug 14 '18 at 18:00
6

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

Jerry Ni
  • 521
  • 6
  • 7