How to compare time in Freemarker? - time

I have time in following format:
2017-02-08T08:59:22+00:00
2017-02-08T09:00:55+00:00
How can I compare these two times using Freemarker ?

Like t1 < t2, t1 == t2, etc. That assuming that you get those timestamps as java.util.Date-s, not as strings. If you receive them as strings, first of all see if the Java side can be changed to fix that. If it can't be, you can use someString?datetime('iso') to convert them to java.util.Date in the template.

If those timestamps are representations of java.util.Date objects you can use .before() and .after() java methods.
<#if date1.before(date2) >
...
</#if>
Same approach if you are using Joda Time or other libraries that cover date comparisons.

Related

How to convert Unix Time to (dd-MMM-yyyy) with FTL code

I am trying to convert a unix time to datetime depending on its locale given
Example I wanted to get locale Australia and convert UNIX time to date:
<#setting locale= getlocale>
<#assign unix = "1660545165"?number>
<#assign expiryDate = unix.getstarttime()?number?number_to_datetime?string["yyyy-mm-dd"]>
here is the time ${expiryDate}
I searched in google and suggested me to use getstarttime()?number?number_to_datetime?string["yyyy-mm-dd"]
However I am getting an error and currently stuck:
Expected a hash, but this has evaluated to a number
Lot of what's wrong I think is simply unnecessary there. Also based on the format you actually want a date, not a date-time. Here's a working example:
<#assign unixTime = 1660545165>
Here is the time: ${unixTime?number_to_date?string["yyyy-MM-dd"]}
Also usually you want to set the date_format setting to yyyy-MM-dd once (in the Java code, or with <#setting date_format = "yyyy-MM-dd"> near the top of the the template), and then just write ${unixTime?number_to_date} everywhere.

Perform add/subtract hour/min from date time in freemaker

I am new to freemarker.
In freemarker, I would like to perform arithmetic expression on DateTime as follows:
${triggerTimestamp}-1h
But this is not working. Could anyone please help here?
FreeMarker doesn't do date/time arithmetic out of the box (as of 2.3.30 at least). It's expected that the data-model (context) contains the values that you actually want to display. One can implement some helper method in Java though.
You'll want to explore converting the date into unix/epoch time. If you add ?long to the end of the variable you'll convert the datetime into epoch time (which is in seconds) then do math and convert it back to a date. Your example would be
${(triggerTimestamp?long - 1 * 1000 * 60 * 60)?number_to_datetime?string.iso}

In Ruby, compare two strings as dates and get the older one

Much like this OP asked in Java, I'd like to know how to do in Ruby:
How to compare a date - String in the custom format “dd.MM.yyyy, HH:mm:ss” with another one?
I have these two strings, which convey dates and time and I would like to find the older one of the two:
20141024_133641
20141024_142440
The format is %Y%m%d_%H%M%S ; which makes the former the older one in this case.
Plain lexicographical string comparison will do the job here as your date strings are ordered from most significant to least significant (years to seconds) in strict order. If d1 is your first date and d2 is your second date string, then simple ruby will get the older one:
[d1,d2].min
You could use DateTime.parse
d1 = DateTime.parse("20141024_142440","%Y%m%d_%H%M%S")
d2 = DateTime.parse("20141024_133641","%Y%m%d_%H%M%S")
and compare them as usual
if d1 > d2 ...
or like #kroky said
[d1,d2].min.to_s #=> "2014-10-24T13:36:41+00:00"
[d1,d2].max.to_s #=> "2014-10-24T14:24:40+00:00"

Formatting Dates in Freemarker Where Date Type is Unknown

I have a Freemarker function whose aim is to print any value passed to it and am having difficulty handling dates in particular.
I understand that when Freemarker cannot determine what portion of a date is in use, that it will error when attempting to print the value directly, and so some special-casing is required for dates, but I've been unable to find a reliable workaround of this feature.
My function looks something like this:
<#function format value=''>
<#if value?is_date>
<#-- code to attempt to handle all types of date -->
<#else>
<#-- handle non-date values -->
</#if>
</#function>
So far, I have tried the following:
First attempt: just always print date and time; e.g. value?datetime
Problem: bombs if the value has already 'been told' it's date-only (e.g. format(value?date) - a usage I want to support)
Second attempt: attempt to print raw value using attempt/recover directives to handle problem cases; e.g.
<#attempt>
<#return value>
<#recover>
<#return value?datetime>
</#attempt>
Problem: the attempt/recover directives don't successful catch the exception - instead it's propagated out as before
I've tried many other things but the above approaches were the more sensible, and unfortunately neither were successful. There seems to be a catch-22: if the date-type is unknown I can only print by choosing an arbitrary type to apply to all date values, but if I attempt to apply that type to a known-type date, it will fail where the types don't match.
Is there any way to determine whether the date type of a value is known before trying to print the value? If so, I could use the ?datetime built in only when necessary.
Ideally, I could tell Freemarker to just print the full date where it's unable to determine the exact type, instead of bombing - but I'm not sure this is currently possible.
Update: In FreeMarker 2.3.21 you can use <#if value?is_date_like>${value?datetime_if_unknown}<#else>...
Yeah, there should exist something like ?is_unknown_type_date, but it doesn't... I'm an FM maintainer so I will add that in 2.3.21 (but don't hold your breath until that's released). Meanwhile, you can write a TemplateMethodModelEx that does just that. Implementing it trivial as you will see, how to make them accessible to templates is a bit underdocumented... One way is just doping the TemplateMethodModelEx into the data-model or into the "shared variable" set of the Configuration. Another is putting this into some of your commonly #import-ed or #included template like <#assign isUnknownTypeDate='com.example.IsUnknownTypeDateMethod'?new()>.
BTW, #recover works for me for this (using a nightly 2.3.21, but I don't remember that it was ever broken). But don't use it for this anyway, as it will log the error. #recover is for emergency situations only, not for normal program flow.
As of providing a default format for unknowns-type dates... I feel uneasy about it as then these issues won't be caught during development, and very few will care to use a different FM configuration for production than for development.

How I can format date in report to appear exactly as I want - RDLC

I have a report in my application and this report will show a long date from db and I used this expression to make it shorter:
=FormatDateTime(Fields!StatementDate.Value,DateFormat.ShortDate)
and the date will show like this : 1/1/2010
I need to make it like this : 2010/1/1
How I can do it?
That expression do the trick
=CDate(Fields!Fecha.Value).ToString("yyyy/M/d")
I think that it is a lot cleaner to use the Format property rather than format it in your expressions:
http://msdn.microsoft.com/en-us/library/ms252080%28v=vs.90%29.aspx
You can use the standard .NET formatting strings.
Value=Fields!StatementDate.Value
Format=yyyy/M/d
Fields!StatementDate.Value will need to be a DateTime, if not you can try convert it:
Value=CDate(Fields!StatementDate.Value)
=CDate(Fields!StatementDate.Value).ToShortDateString()

Resources