Skip to content

Commit

Permalink
Changing TIME_FORMAT to \pset time <FORMAT>
Browse files Browse the repository at this point in the history
Changes the way the time display value is set. Deviates from `psql` as
`psql` has no client side setting for changing time display.
  • Loading branch information
kenshaw committed May 1, 2021
1 parent 521f8f5 commit 5489327
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 42 deletions.
57 changes: 40 additions & 17 deletions README.md
Expand Up @@ -859,31 +859,54 @@ highlighting:
Some databases support time/date columns that [support formatting][go-time]. By
default, `usql` formats time/date columns as [RFC3339Nano][go-time], and can be
set using the [`TIME_FORMAT` variable][variables]:
set using `\pset time <FORMAT>`:
```sh
$ usql pg://
Connected with driver postgres (PostgreSQL 9.6.9)
Connected with driver postgres (PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1))
Type "help" for help.
pg:booktest@=> \set
TIME_FORMAT = 'RFC3339Nano'
pg:booktest@=> select now();
now
+----------------------------------+
2018-06-14T03:24:12.481923+07:00
(1 rows)
pg:booktest@=> \set TIME_FORMAT Kitchen
pg:booktest@=> \g
pg:postgres@=> \pset
time RFC3339Nano
pg:postgres@=> select now();
now
-----------------------------
2021-05-01T22:21:44.710385Z
(1 row)
pg:postgres@=> \pset time Kitchen
Time display is "Kitchen" ("3:04PM").
pg:postgres@=> select now();
now
+--------+
3:24AM
(1 rows)
---------
10:22PM
(1 row)
pg:postgres@=>
```
Any [Go supported time format][go-time] or const name (for example, `Kitchen`,
in the above) can be used for `TIME_FORMAT`.
Any [Go supported time format][go-time] or the standard Go const name (for example,
`Kitchen`, in the above).
##### Constants
| Constant Name | Value |
|---------------|---------------------------------------|
| ANSIC | `Mon Jan _2 15:04:05 2006` |
| UnixDate | `Mon Jan _2 15:04:05 MST 2006` |
| RubyDate | `Mon Jan 02 15:04:05 -0700 2006` |
| RFC822 | `02 Jan 06 15:04 MST` |
| RFC822Z | `02 Jan 06 15:04 -0700` |
| RFC850 | `Monday, 02-Jan-06 15:04:05 MST` |
| RFC1123 | `Mon, 02 Jan 2006 15:04:05 MST` |
| RFC1123Z | `Mon, 02 Jan 2006 15:04:05 -0700` |
| RFC3339 | `2006-01-02T15:04:05Z07:00` |
| RFC3339Nano | `2006-01-02T15:04:05.999999999Z07:00` |
| Kitchen | `3:04PM` |
| Stamp | `Jan _2 15:04:05` |
| StampMilli | `Jan _2 15:04:05.000` |
| StampMicro | `Jan _2 15:04:05.000000` |
| StampNano | `Jan _2 15:04:05.000000000` |
#### Copy
Expand Down
16 changes: 6 additions & 10 deletions env/types.go
Expand Up @@ -40,10 +40,6 @@ func init() {
if v := Getenv("USQL_SHOW_HOST_INFORMATION"); v != "" {
enableHostInformation = v
}
timefmt := "RFC3339Nano"
if v := Getenv("USQL_TIME_FORMAT"); v != "" {
timefmt = v
}
// get color level
colorLevel, _ := terminfo.ColorLevelFromEnv()
enableSyntaxHL := "true"
Expand All @@ -53,7 +49,6 @@ func init() {
vars = Vars{
// usql related logic
"SHOW_HOST_INFORMATION": enableHostInformation,
"TIME_FORMAT": timefmt,
"PAGER": Getenv("USQL_PAGER", "PAGER"),
// syntax highlighting variables
"SYNTAX_HL": enableSyntaxHL,
Expand All @@ -78,6 +73,7 @@ func init() {
"recordsep": "\n",
"recordsep_zero": "off",
"tableattr": "",
"time": "RFC3339Nano",
"title": "",
"tuples_only": "off",
"unicode_border_linestyle": "single",
Expand Down Expand Up @@ -262,7 +258,7 @@ func Ptoggle(name, extra string) (string, error) {
pvars[name] = "aligned"
}
case "linestyle":
case "csv_fieldsep", "fieldsep", "null", "recordsep":
case "csv_fieldsep", "fieldsep", "null", "recordsep", "time":
case "tableattr", "title":
pvars[name] = ""
case "unicode_border_linestyle", "unicode_column_linestyle", "unicode_header_linestyle":
Expand Down Expand Up @@ -310,7 +306,7 @@ func Pset(name, value string) (string, error) {
return "", text.ErrInvalidFormatLineStyle
}
pvars[name] = value
case "csv_fieldsep", "fieldsep", "null", "recordsep", "tableattr", "title":
case "csv_fieldsep", "fieldsep", "null", "recordsep", "tableattr", "time", "title":
pvars[name] = value
case "unicode_border_linestyle", "unicode_column_linestyle", "unicode_header_linestyle":
if !borderRE.MatchString(value) {
Expand Down Expand Up @@ -342,9 +338,9 @@ var timeConstMap = map[string]string{
"StampNano": time.StampNano,
}

// Timefmt returns the environment TIME_FORMAT.
func Timefmt() string {
tfmt := vars["TIME_FORMAT"]
// GoTime returns the user's time format converted to Go's time.Format value.
func GoTime() string {
tfmt := pvars["time"]
if s, ok := timeConstMap[tfmt]; ok {
return s
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -80,7 +80,7 @@ require (
github.com/vertica/vertica-sql-go v1.1.1
github.com/vmihailenco/msgpack/v5 v5.3.1 // indirect
github.com/xo/dburl v0.6.1
github.com/xo/tblfmt v0.7.3
github.com/xo/tblfmt v0.7.4
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778
github.com/zaf/temp v0.0.0-20170209143821-94e385923345
github.com/ziutek/mymysql v1.5.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -1071,8 +1071,8 @@ github.com/xinsnake/go-http-digest-auth-client v0.6.0/go.mod h1:QK1t1v7ylyGb363v
github.com/xo/dburl v0.6.1 h1:Y2lQsjXbAbBy0RZjsLAefS0Hu7DxOizHMN/iGcoXN9U=
github.com/xo/dburl v0.6.1/go.mod h1:W68zXnBfTb4zcKLI1yEYRyYIQjcjoyCRn4YMD/QzcpE=
github.com/xo/tblfmt v0.0.0-20190609041254-28c54ec42ce8/go.mod h1:3U5kKQdIhwACye7ml3acccHmjGExY9WmUGU7rnDWgv0=
github.com/xo/tblfmt v0.7.3 h1:KBYR7jumA1uxO7kQ6f6Gzmh4gSvKC+drYOfao1U2JFs=
github.com/xo/tblfmt v0.7.3/go.mod h1:2fh9mXteiVnG68zzU2TS+CsCCU9lq3mgLi/EYw7ljrM=
github.com/xo/tblfmt v0.7.4 h1:i8kyQFBVVqLkl6T4nX76axFNCSYf2w5ARsCScryPU0Q=
github.com/xo/tblfmt v0.7.4/go.mod h1:2fh9mXteiVnG68zzU2TS+CsCCU9lq3mgLi/EYw7ljrM=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 h1:zzrxE1FKn5ryBNl9eKOeqQ58Y/Qpo3Q9QNxKHX5uzzQ=
Expand Down
15 changes: 3 additions & 12 deletions handler/handler.go
Expand Up @@ -805,15 +805,6 @@ func (h *Handler) Print(format string, a ...interface{}) {
fmt.Fprintln(out)
}

// timefmt returns the current time format setting.
func (h *Handler) timefmt() string {
s := env.Timefmt()
if s == "" {
s = time.RFC3339Nano
}
return s
}

// execWatch repeatedly executes a query against the database.
func (h *Handler) execWatch(ctx context.Context, w io.Writer, opt metacmd.Option, prefix, qstr string, qtyp bool) error {
for {
Expand Down Expand Up @@ -860,7 +851,7 @@ func (h *Handler) execSet(ctx context.Context, w io.Writer, opt metacmd.Option,
// process row(s)
var i int
var row []string
clen, tfmt := len(cols), h.timefmt()
clen, tfmt := len(cols), env.GoTime()
for q.Next() {
if i == 0 {
row, err = h.scan(q, clen, tfmt)
Expand Down Expand Up @@ -917,7 +908,7 @@ func (h *Handler) query(ctx context.Context, w io.Writer, opt metacmd.Option, _,
}
defer q.Close()
params := env.Pall()
params["time_format"] = env.Timefmt()
params["time"] = env.GoTime()
for k, v := range opt.Params {
params[k] = v
}
Expand Down Expand Up @@ -983,7 +974,7 @@ func (h *Handler) execRows(ctx context.Context, w io.Writer, q *sql.Rows) error
}
// process rows
res := metacmd.Option{Exec: metacmd.ExecOnly}
clen, tfmt := len(cols), h.timefmt()
clen, tfmt := len(cols), env.GoTime()
for q.Next() {
if clen != 0 {
row, err := h.scan(q, clen, tfmt)
Expand Down
6 changes: 6 additions & 0 deletions metacmd/cmds.go
Expand Up @@ -708,6 +708,12 @@ func init() {
case !strings.Contains(mask, "%"):
p.Handler.Print(mask)
default:
if field == "time" {
val = fmt.Sprintf("%q", val)
if tfmt := env.GoTime(); tfmt != val {
val = fmt.Sprintf("%s (%q)", val, tfmt)
}
}
p.Handler.Print(mask, val)
}
return nil
Expand Down
1 change: 1 addition & 0 deletions text/text.go
Expand Up @@ -71,6 +71,7 @@ var (
`recordsep`: `Field separator is %q.`,
`recordsep_zero`: `Record separator is zero byte.`,
`tableattr`: `Table attributes are %q.`,
`time`: `Time display is %s.`,
`title`: `Title is %q.`,
`tuples_only`: `Tuples only is %s.`,
`unicode_border_linestyle`: `Unicode border line style is %q.`,
Expand Down

0 comments on commit 5489327

Please sign in to comment.