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.58.2.2 -r1.58.2.3 --- openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl 27 Nov 2003 12:19:42 -0000 1.58.2.2 +++ openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl 5 Feb 2004 16:54:57 -0000 1.58.2.3 @@ -4616,3 +4616,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] + + } +} Index: openacs-4/packages/acs-tcl/tcl/test/acs-tcl-test-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/test/acs-tcl-test-procs.tcl,v diff -u -r1.17 -r1.17.2.1 --- openacs-4/packages/acs-tcl/tcl/test/acs-tcl-test-procs.tcl 4 Nov 2003 10:33:37 -0000 1.17 +++ openacs-4/packages/acs-tcl/tcl/test/acs-tcl-test-procs.tcl 5 Feb 2004 16:54:57 -0000 1.17.2.1 @@ -691,3 +691,43 @@ aa_equals "List is not a subset" [util_get_subset_missing [list a b c d] [list a b c]] [list d] } +aa_register_case util__age_pretty { + Test the util::age_pretty proc. +} { + aa_log "Forcing locale to en_US for all strings so that tests work in any locale" + aa_equals "0 secs" [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-01 12:00:00" -locale en_US] "1 minute ago" + aa_equals "1 sec" [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-01 12:00:01" -locale en_US] "1 minute ago" + aa_equals "29 secs" [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-01 12:00:29" -locale en_US] "1 minute ago" + aa_equals "30 secs" [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-01 12:00:30" -locale en_US] "1 minute ago" + aa_equals "31 secs" [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-01 12:00:31" -locale en_US] "1 minute ago" + aa_equals "59 secs" [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-01 12:00:59" -locale en_US] "1 minute ago" + aa_equals "1 min" [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-01 12:01:00" -locale en_US] "1 minute ago" + aa_equals "1 min 1 sec" [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-01 12:01:01" -locale en_US] "1 minute ago" + + aa_equals "1 min 29 sec" [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-01 12:01:29" -locale en_US] "1 minute ago" + aa_equals "1 min 30 sec" [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-01 12:01:30" -locale en_US] "2 minutes ago" + aa_equals "1 min 31 sec" [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-01 12:01:31" -locale en_US] "2 minutes ago" + + aa_equals "11 hours 59 minutes" [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-01 23:59:00" -locale en_US] "11 hours 59 minutes ago" + aa_equals "15 hours 0 minutes with override" \ + [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-02 03:00:00" -hours_limit 16 -locale en_US] "15 hours ago" + + + aa_equals "12 hours 0 minutes" [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-02 00:00:00" -locale en_US] "12:00 PM, Thursday" + + aa_equals "15 hours 0 minutes" [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-02 03:00:00" -locale en_US] "12:00 PM, Thursday" + + aa_equals "4 days 0 hours 0 minutes with override" \ + [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-05 12:00:00" -days_limit 5 -locale en_US] "12:00 PM, Thursday" + + aa_equals "3 days 0 hours 0 minutes" \ + [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-04 12:00:00" -locale en_US] "12:00 PM, 01 Jan 2004" + + aa_equals "5 days 0 hours 0 minutes" \ + [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2004-01-06 12:00:00" -locale en_US] "12:00 PM, 01 Jan 2004" + + aa_equals "10 years" \ + [util::age_pretty -timestamp_ansi "2004-01-01 12:00:00" -sysdate_ansi "2014-01-01 12:00:00" -locale en_US] "12:00 PM, 01 Jan 2004" + + aa_log "100 years - we know it's wrong because of Tcl library limitations: [util::age_pretty -timestamp_ansi "1904-01-01 12:00:00" -sysdate_ansi "2004-01-01 12:00:00"]" +}