Comparison of Dates

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Comparison of Dates

Jaime Garza
Consider the following code in ftl:

<#assign todaysdate = "Jan 30, 2013"?date>
<#assign testdate = ("2013-01-30T11:00:00")?date("yyyy-MM-dd'T'HH:mm:ss")>
Todaysdate: ${todaysdate}
Testdate:${testdate}
<#if todaysdate?date <= testdate?date>
It is less than
</#if>

I get
Todaysdate: Jan 30, 2013
Testdate:Jan 30, 2013
It is less than

It turns out that the time portion is still used to compare the dates, as per the code below:

        else if(ltm instanceof TemplateDateModel && rtm instanceof TemplateDateModel) {
            TemplateDateModel ltdm = (TemplateDateModel)ltm;
            TemplateDateModel rtdm = (TemplateDateModel)rtm;
            int ltype = ltdm.getDateType();
            int rtype = rtdm.getDateType();
            if(ltype != rtype) {
                throw new TemplateException(
                    "Can not compare dates of different type. Left date is of "
                    + TemplateDateModel.TYPE_NAMES.get(ltype)
                    + " type, right date is of "
                    + TemplateDateModel.TYPE_NAMES.get(rtype) + " type.",
                    env);
            }
            if(ltype == TemplateDateModel.UNKNOWN) {
                throw new TemplateException(
                    "Left date is of UNKNOWN type, and can not be compared.", env);
            }
            if(rtype == TemplateDateModel.UNKNOWN) {
                throw new TemplateException(
                    "Right date is of UNKNOWN type, and can not be compared.", env);
            }
           
            Date first = EvaluationUtil.getDate(ltdm, left, env);
            Date second = EvaluationUtil.getDate(rtdm, right, env);
            comp = first.compareTo(second);
        }

Here first and second still contain the time portion since SimpleDate just returns the current date without any modifications regarding its internal type.  So I get "less than".  The problem is compounded since I am using .now, and not a hardcoded string for the full date.

Questions:

Are you guys planning on fixing this behavior?  Currently only converting a date to string, and back to a date will work.
Was this as planned?

Thanks.

------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
FreeMarker-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-devel
Reply | Threaded
Open this post in threaded view
|

Re: Comparison of Dates

Daniel Dekany
Wednesday, January 30, 2013, 10:34:55 PM, Jaime Garza wrote:

> Consider the following code in ftl:
>
> <#assign todaysdate = "Jan 30, 2013"?date>
> <#assign testdate =
> ("2013-01-30T11:00:00")?date("yyyy-MM-dd'T'HH:mm:ss")>
> Todaysdate: ${todaysdate}
> Testdate:${testdate}
> <#if todaysdate?date <= testdate?date>
> It is less than
> </#if>
>
> I get
> Todaysdate: Jan 30, 2013
> Testdate:Jan 30, 2013
> It is less than
>
> It turns out that the time portion is still used to compare the dates, as per the code below:
>
>         else if(ltm instanceof TemplateDateModel && rtm instanceof TemplateDateModel) {
>             TemplateDateModel ltdm = (TemplateDateModel)ltm;
>             TemplateDateModel rtdm = (TemplateDateModel)rtm;
>             int ltype = ltdm.getDateType();
>             int rtype = rtdm.getDateType();
>             if(ltype != rtype) {
>                 throw new TemplateException(
>                     "Can not compare dates of different type. Left date is of "
>                     + TemplateDateModel.TYPE_NAMES.get(ltype)
>                     + " type, right date is of "
>                     + TemplateDateModel.TYPE_NAMES.get(rtype) + " type.",
>                     env);
>             }
>             if(ltype == TemplateDateModel.UNKNOWN) {
>                 throw new TemplateException(
>                     "Left date is of UNKNOWN type, and can not be compared.", env);
>             }
>             if(rtype == TemplateDateModel.UNKNOWN) {
>                 throw new TemplateException(
>                     "Right date is of UNKNOWN type, and can not be compared.", env);
>             }
>            
>             Date first = EvaluationUtil.getDate(ltdm, left, env);
>             Date second = EvaluationUtil.getDate(rtdm, right, env);
>             comp = first.compareTo(second);
>         }
>
> Here first and second still contain the time portion since
> SimpleDate just returns the current date without any modifications
> regarding its internal type.  So I get "less than".  The problem is
> compounded since I am using .now, and not a hardcoded string for the full date.
>
> Questions:
>
> Are you guys planning on fixing this behavior?  Currently only
> converting a date to string, and back to a date will work.
> Was this as planned?

Yes, although the end result is kind of confusing. The thing is,
?date, ?time and ?datetime aren't meant to change the value. They
barely exists because the Java API is broken, and you can't always
tell if a Date object is Date-only, Time-only or date-time value, and
without that its not possible to convert the date to string. So it's
only to mark a Date as one of those, since the Java API fails to do
so.

(BTW, converting a Date to Date-only or Time-only is kind of a
dangerous operation too, as the result depends on the time zone...)

> Thanks.

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
FreeMarker-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-devel