Forum OpenACS Q&A: Re: Formatting dates / times nicely

Collapse
Posted by Tom Ayles on
Is there any way of obtaining a locale-specific format string to use in DB to_char commands? A lot of the formatting seems to happen with the DB (which is fair enough) but the format string is hard-coded into the query. Over a period of time, it might be better to work these out and replace them with a format string generated by the localisation procs.
Collapse
Posted by Lars Pind on
No.

Code that does pretty-formatting in the DB should be changed to do so in the Tcl layer.

Here's the recommended way of doing things:

db_multirow -extend { mydate_pretty } {
    select to_char(mydate, 'YYYY-MM-DD HH24:MI:SS') as mydate_ansi,
          ...
    ...
} {
    set mydate_ansi [lc_time_system_to_conn $mydate_ansi]
    set mydate_pretty [lc_time_fmt $mydate_ansi "%x %X"]
}

... or use your desired date/time format as the second argument to lc_time_fmt.

The only date format we should ever get out of the DB is ANSI dates.

/Lars