Skip to content

Commit f03fc6c

Browse files
committed
feat: allow predefined time formats
1 parent 3839b8d commit f03fc6c

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

log/command.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,35 @@ func (o Options) Run() error {
2727
l.SetPrefix(o.Prefix)
2828
l.SetLevel(-math.MaxInt32) // log all levels
2929
l.SetReportTimestamp(o.Time)
30-
l.SetTimeFormat(o.TimeFormat)
30+
31+
timeFormats := map[string]string{
32+
"layout": "01/02 03:04:05PM '06 -0700",
33+
"ansic": "Mon Jan _2 15:04:05 2006",
34+
"unixdate": "Mon Jan _2 15:04:05 MST 2006",
35+
"rubydate": "Mon Jan 02 15:04:05 -0700 2006",
36+
"rfc822": "02 Jan 06 15:04 MST",
37+
"rfc822z": "02 Jan 06 15:04 -0700",
38+
"rfc850": "Monday, 02-Jan-06 15:04:05 MST",
39+
"rfc1123": "Mon, 02 Jan 2006 15:04:05 MST",
40+
"rfc1123z": "Mon, 02 Jan 2006 15:04:05 -0700",
41+
"rfc3339": "2006-01-02T15:04:05Z07:00",
42+
"rfc3339nano": "2006-01-02T15:04:05.999999999Z07:00",
43+
"kitchen": "3:04PM",
44+
"stamp": "Jan _2 15:04:05",
45+
"stampmilli": "Jan _2 15:04:05.000",
46+
"stampmicro": "Jan _2 15:04:05.000000",
47+
"stampnano": "Jan _2 15:04:05.000000000",
48+
"datetime": "2006-01-02 15:04:05",
49+
"dateonly": "2006-01-02",
50+
"timeonly": "15:04:05",
51+
}
52+
53+
tf, ok := timeFormats[strings.ToLower(o.TimeFormat)]
54+
if ok {
55+
l.SetTimeFormat(tf)
56+
} else {
57+
l.SetTimeFormat(o.TimeFormat)
58+
}
3159

3260
st := log.DefaultStyles()
3361
defaultColors := map[log.Level]lipgloss.Color{

0 commit comments

Comments
 (0)