template::util::date::validate (public)

 template::util::date::validate date error_ref

Defined in packages/acs-templating/tcl/date-procs.tcl

Validate a date object.

Parameters:
date
error_ref
Returns:
1 if the object is valid, 0 otherwise. Set the error_ref variable to contain an error message, if any.

Partial Call Graph (max 5 caller/called nodes):
%3 test_validate_date validate_date (test acs-templating) template::util::date::validate template::util::date::validate test_validate_date->template::util::date::validate _ _ (public) template::util::date::validate->_ template::util::date::get_property template::util::date::get_property (public) template::util::date::validate->template::util::date::get_property template::util::date::unpack template::util::date::unpack (public) template::util::date::validate->template::util::date::unpack util::trim_leading_zeros util::trim_leading_zeros (public) template::util::date::validate->util::trim_leading_zeros template::data::validate::date template::data::validate::date (public) template::data::validate::date->template::util::date::validate template::data::validate::time_of_day template::data::validate::time_of_day (public) template::data::validate::time_of_day->template::util::date::validate template::data::validate::timestamp template::data::validate::timestamp (public) template::data::validate::timestamp->template::util::date::validate

Testcases:
validate_date
Source code:
    # If the date is empty, it's valid
    if { ![get_property not_null $date] } {
        return 1
    }

    variable fragment_formats
    upvar $error_ref error_msg

    unpack $date

    set error_msg [list]

    foreach {field exp} {
        year "YYYY|YY"
        month "MM|MON|MONTH"
        day "DD"
        hours "HH24|HH12"
        minutes "MI"
        seconds "SS"
    } {
        # Trim leading zeros to avoid numbers being interpreted as
        # octals when comparing them.
        set $field [util::trim_leading_zeros [set $field]]

        # If the field is required, but missing, report an error
        if {[set $field] eq ""} {
            if { [regexp $exp $format match] } {
                set field_pretty [_ acs-templating.${field}]
                lappend error_msg [_ acs-templating.lt_No_value_supplied_for_-field_pretty-]
            }
        } else {
            # fields should only be integers
            if { ![regexp {^[0-9]+$} [set $field] match] } {
                set field_pretty [_ acs-templating.${field}]
                lappend error_msg [_ acs-templating.lt_The_-field_pretty-_must_be_non_negative]
                set $field {}
            }
        }
    }

    if { $year ne {} && ($year < 1 || $year > 9999)} {
        lappend error_msg [_ acs-templating.Year_must_be_between_1_and_9999]
    }

    if { $month ne {} } {
        if { $month < 1 || $month > 12 } {
            lappend error_msg [_ acs-templating.Month_must_be_between_1_and_12]
        } elseif$year > 0 && $day ne {} } {
            set maxdays [get_property days_in_month $date]
            if { $day < 1 || $day > $maxdays } {
                set month_pretty [template::util::date::get_property long_month_name $date]
                if { $month == 2 } {
                    # February has a different number of days depending on the year
                    append month_pretty ${year}"
                }
                lappend error_msg [_ acs-templating.lt_day_between_for_month_pretty]
            }
        }
    }

    if { $hours ne {} && ($hours < 0 || $hours > 23) } {
        lappend error_msg [_ acs-templating.Hours_must_be_between_0_and_23]
    }

    if { $minutes ne {} && ($minutes < 0 || $minutes > 59) } {
        lappend error_msg [_ acs-templating.Minutes_must_be_between_0_and_59]
    }

    if { $seconds ne {} && ($seconds < 0 || $seconds > 59) } {
        lappend error_msg [_ acs-templating.Seconds_must_be_between_0_and_59]
    }

    if { [llength $error_msg] > 0 } {
        set error_msg "[join $error_msg {<br>}]"
        return 0
    } else {
        return 1
    }
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: