1

I'm having some trouble while trying to use the Freemarker Template to display dates in the desired format.

I store points with a date information in a PostGIS database written through an FME-process in an ISO format (%Y-%m-%d) to use them in an time-enabled WMS with GeoServer.

When calling the GetFeatureInfo, the date is displayed in the following format 10/4/12 12:00 AM, where it should be 2012-10-04. We allready changened the server setting to -Dorg.geotools.localDateTimeHandling=true -Duser.country=DE -Duser.timezone=GMT -Duser.language=de.

Since this didn't give the desired outcome, we tried it with the Freemarker Template. The idea was to check the attributes for date format and format them accordingly. Somehow, I can't make it work. I tried this:

<#if attribute.is_unknown_date_like>
${attribute.value?string("YYYY-MM-DD")}
<#else>
${attribute.value}
</#if>

I get an error message for the line where the condition starts:

freemarker.core.ParseException

How can I make this condition statement work?

ErikBoehm
  • 11
  • 2
  • What's the rest of the error message? It contains what the problem is. Also, `freemarker.core.ParseException` means that the template is syntactically wrong, so it has nothing to do with the date formatting yet. Also the fragment you show has no such problem, so the cause is elsewhere, or you didn't paste the actual fragment. – ddekany Jun 30 '20 at 18:59
  • That said, the fragment does have further problems (like you should use `attribute?is_date_like`, with a `?`), but first let's get rid of the parse exception. – ddekany Jun 30 '20 at 19:00
  • @ddekany - I posted the fragment I added to the Freemarker Template, which works without this addition. So the cause has to be in this fragment. You are right, I should use `attribute?is_data_like` with a `?` since it is a build-in. With this, the error message gets more significant. See coment below your answer. – ErikBoehm Jul 01 '20 at 06:54
  • Is it possible that the FreeMarker version your are using is lower than 2.3.21? That's when `is_date_like` was added. Try `${.version}`. You really need to upgrade then. – ddekany Jul 02 '20 at 13:47
  • Do you get `attribute.value` as a string? I was in the belief that it's a `java.util.Date`, but your recent "answer" tells that it's just a string. Then you won't need `?is_date_like`, as it will be always `false` anyway. – ddekany Jul 02 '20 at 13:49
  • See my updated answers, the tries to consider all these possibilities. – ddekany Jul 02 '20 at 14:08

2 Answers2

0

Updated: Added parsing to the #else branch, etc.

You could do this if you don't know if attribute.value will be java.lang.String or a java.util.Date:

<#if attribute.value?is_date_like>
${attribute.value?date?string.iso}
<#else>
${attribute.value?date("M/d/yy hh:mm a")?string.iso}
</#if>

If you know the type of attribute.value, then you only have to do what's inside the #if, or the #else.

If you are using a really old version of FreeMarker, then instead of ?string.iso, you have to use ?string("yyyy-MM-dd"). Also then ?is_date_like might not be available yet, and you had to use attribute.value?is_unknown_date_like || attribute.value?is_datetime || attribute.value?is_date.

By the way, if you typically output date/time values with ISO format, then someone should just set the date_format/time_format/datetime_format configuration settings to iso, and then you can omit ?string.iso. (Or, these can be set in the template too, like <#setting date_format='iso'>, etc.)

ddekany
  • 29,656
  • 4
  • 57
  • 64
0

@ddekany, you are rigth, I am sorry. I will keep that in mind for the future!

So I tried your suggestion, and as statet above using a ? still gives back a errror message but it gets more significant.

2020-07-01 08:19:02,521 ERROR [geoserver.ows] - freemarker.core.ParseException: Error on line 29, column 46, in template content.ftl Found is_date_like, expecting one of: is_directive, parent, js_string, j_string, uncap_first, is_transform, number, is_hash, trim, children, has_content, iso_ms, xml, iso_utc, byte, double, left_pad, matches, capitalize, number_to_datetime, contains, size, iso_local_h_nz, iso_utc_ms, iso_local_m_nz, is_collection, long, default, iso_utc_h_nz, iso_local_ms, is_boolean, last_index_of, c, iso_utc_m_nz, is_macro, rtf, iso_utc_nz, upper_case, node_name, reverse, cap_first, url, is_hash_ex, iso_nz, is_enumerable, exists, number_to_date, first, iso_local, date, iso, replace, float, right_pad, datetime, node_type, split, iso_ms_nz, number_to_time, is_sequence, iso_utc_m, html, ancestors, iso_utc_h, iso_local_ms_nz, new, last, sort, eval, lower_case, web_safe, is_date, is_string, iso_local_nz, word_list, seq_last_index_of, node_namespace, string, keys, iso_m_nz, values, seq_index_of, chunk, sort_by, iso_m, starts_with, substring, index_of, iso_h, root, floor, iso_h_nz, ceiling, if_exists, chop_linebreak, iso_local_h, length, is_indexable, groups, is_node, iso_local_m, int, iso_utc_ms_nz, xhtml, ends_with, round, interpret, is_method, namespace, short, seq_contains, time, is_number in content.ftl

So, the problem seems to lie within the built-in. As statet in the error message, Freemarker expects is_date instead of is_date_like, eventhough in the Freemarker documentation it is states that is_date_like should be uses instead of is_date link. So I tried your suggestion with is_date.

Now, no error message appears, but the date format is unchanged.

ErikBoehm
  • 11
  • 2
  • Please edit your original question instead of adding information in something that supposed to be an answer to the original question. I will answer under the original question. – ddekany Jul 02 '20 at 13:44