Forum OpenACS Development: Compare Dates

Collapse
Posted by Rodrigo C on
Hello, I'am using the:
template::element create date_test date_inicio  and
template::element create date_test date_fin

Now I have an array with the year, month, day, hours, min, etc.  But I need to compare, for example if the second one is not earlier than the first one, and some other comparations.  So, I think there is a function that returns a integer number that represents the date, and recives like "YYYY MM DD 24Hrs Min" for example; so that the comparing with the integer numbers will be more easy.

What should I use??

Collapse
2: Re: Compare Dates (response to 1)
Posted by Rodrigo C on
I did this but I'am sure it is a waste of time, and looks ugly in the code:

if {$date_inicio(year) > $date_fin(year)} {
    ad_return_error "Fecha inicio mayor que fecha final" "Fechas no validas, intente de nuevo."
    ad_script_abort
} elseif { $date_inicio(year) == $date_fin(year)} {
    if {$date_inicio(month) > $date_fin(month)} {
        ad_return_error "Fecha inicio mayor que fecha final" "Fechas no validas, intente de nuevo."
        ad_script_abort
    } elseif {$date_inicio(month) == $date_fin(month)} {
        if {$date_inicio(day) > $date_fin(day)} {
            ad_return_error "Fecha inicio mayor que fecha final" "Fechas no validas, intente de nuevo."
            ad_script_abort
        } elseif { $date_inicio(day) == $date_fin(day)} {
            if {$date_inicio(hours) > $date_fin(hours)} {
                ad_return_error "Fecha inicio mayor que fecha final" "Fechas no validas, intente de nuevo."
                ad_script_abort
            } elseif { $date_inicio(hours) == $date_fin(hours)} {
                if {$date_inicio(minutes) > $date_fin(minutes)} {
                    ad_return_error "Fecha inicio mayor que fecha final" "Fechas no validas, intente de nuevo."
                    ad_script_abort
                } elseif { $date_inicio(minutes) == $date_fin(minutes)} {
                    ad_return_error "Fechas Iguales" "Fechas no validas, intente de nuevo."
                    ad_script_abort
                }
            }
        }
    }
}

Collapse
3: Re: Compare Dates (response to 1)
Posted by Dirk Gomez on
acs-events does it with asking the database (very ugly).

I suggest you take a look at http://www.oche.de/~akupries/soft/pool/f_base_date.tcl.html - I'll probably convert acs-events and calendar to using functions from Andreas' tclib.

Collapse
4: Re: Compare Dates (response to 1)
Posted by Tilmann Singer on
It appears that template::util::date::compare does what you want. If you need to perform other operations on dates I suggest retrieving the tcl clock value from the widget date with

set date_inicio_clock [util::date::get_property clock date_inicio]

And use that value for your operations - it's simply an integer with the unix seconds - see 'man 3tcl clock' to see how you can manipulate it.

To set the value of an OpenACS date widget to a previously calculated clock value use this:

util::date::set_property clock date_inicio $date_inicio_clock

Collapse
5: Re: Compare Dates (response to 1)
Posted by Don Baccus on
Here's an example using the form builder directly (as you're doing):
element create event_add end_time \
    -label "End" \
    -datatype date \ 
    -widget date \
    -format "MONTH DD YYYY HH12:MI AM" \
    -minutes_interval { 0 59 5 } \
    -help \
    -validate { \   
    { expr {[template::util::date::compare [template::element::get_value event_add start_time] $valu
e] < 0} } \
    {End time must be after start time} }

The validation clause calls the date comparison routine with start_time and end_time. The value for start_time must be retrieved usingn get_value because the validation clause is attached to the end_time form element. The value for end_time, the current form element when the validation clause is being executed, is directly available as "$value".

The comparison routine returns -1,0,1 depending on whether the first date is less, equal or greater than the second date param.

You should look into using ad_form, which is a wrapper procedure for the form template builder that simplfies its use considerably in many cases, and supports a validation block. Just grep the toolkit for "ad_form" for examples and documentation.