Index: openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl,v diff -u -r1.69 -r1.70 --- openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl 25 Feb 2004 18:18:22 -0000 1.69 +++ openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl 26 Feb 2004 15:28:54 -0000 1.70 @@ -4789,3 +4789,53 @@ } return $result } + +ad_proc -public util::age_pretty { + -timestamp_ansi:required + -sysdate_ansi:required + {-hours_limit 12} + {-days_limit 3} + {-mode_2_fmt "%X, %A"} + {-mode_3_fmt "%X, %d %b %Y"} + {-locale ""} +} { + Formats past time intervals in one of three different modes depending on age. The first mode is "1 hour 3 minutes" and is NOT currently internationalized. The second mode is e.g. "14:10, Thursday" and is internationalized. The third mode is "14:10, 01 Mar 2001" and is internationalized. Both the locale and the exact format string for modes 2 and 3 can be overridden by parameters. (Once mode 1 is i18nd, the following sentence will be true:'In mode 1, only the locale can be overridden.' Until then, move along. These aren't the timestamps you're looking for.) + + @param timestamp_ansi The older timestamp in full ANSI: YYYY-MM-DD HH24:MI:SS + @param sysdate_ansi The newer timestamp. + + @param hours_limit The upper limit, in hours, for mode 1. + @param days_limit The upper limit, in days, for mode 2. + @param mode_2_fmt A formatting string, as per lc_time_fmt, for mode 2 + @param mode_3_fmt A formatting string, as per lc_time_fmt, for mode 3 + @param locale If present, overrides the default locale + @return Interval between timestamp and sysdate, as localized text string. +} { + set age_seconds [expr [clock scan $sysdate_ansi] - [clock scan $timestamp_ansi]] + + if { $age_seconds < 30 } { + # Handle with normal processing below -- otherwise this would require another string to localize + set age_seconds 60 + } + + if { $age_seconds < [expr $hours_limit * 60 * 60] } { + set hours [expr abs($age_seconds / 3600)] + set minutes [expr round(($age_seconds% 3600)/60.0)] + switch $hours { + 0 { set result "" } + 1 { set result "One hour " } + default { set result "$hours hours "} + } + switch $minutes { + 0 {} + 1 { append result "$minutes minute " } + default { append result "$minutes minutes " } + } + append result "ago" + } elseif { $age_seconds < [expr $days_limit * 60 * 60 * 24] } { + set result [lc_time_fmt $timestamp_ansi $mode_2_fmt $locale] + } else { + set result [lc_time_fmt $timestamp_ansi $mode_3_fmt $locale] + + } +}