Forum OpenACS Q&A: Response to formatting timestamps

Collapse
Posted by Jonathan Ellis on
and here it is without apostrophes in the comments that I added while posting, in case you tried it and it broke (oops :)
create function pretty_timestamp (timestamp) returns varchar as '
    spi_exec "select to_char(''$1''::timestamp, ''Month DD YYYY HH:MI AM TZ'') as s"
    set s_date [string range $s 0 16] ;# month is padded so it is always this long
    spi_exec "select to_char(now(), ''Month DD YYYY'') as now_date"

    #todo: trim hours (but not minutes)

    if ![string compare $s_date $now_date] {
        # same day -- just return time
        return [string range $s 18 end]
    }
    # different day -- return date + time
    regsub -all " +" $s " " s ;# rm space padding from month name
    set s_list [split $s]
    set trimmed_day [string trimleft [lindex $s_list 1] "0"]
    return [join [lreplace $s_list 1 1 $trimmed_day]]
' LANGUAGE 'pltcl'
;