Skip to content

Commit 123a473

Browse files
fix(time): map time constants
resolves #6836
1 parent 3633ed1 commit 123a473

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/log/log.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ func Error(err error) {
8080
printLn(bug, header, err.Error())
8181
}
8282

83+
func Errorf(format string, args ...any) {
84+
if !enabled {
85+
return
86+
}
87+
88+
Error(fmt.Errorf(format, args...))
89+
}
90+
8391
func String() string {
8492
return log.String()
8593
}

src/segments/time.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,43 @@ func (t *Time) Template() string {
2424

2525
func (t *Time) Enabled() bool {
2626
// if no date set, use now(unit testing)
27-
t.Format = t.props.GetString(TimeFormat, "15:04:05")
27+
formatInput := t.props.GetString(TimeFormat, "15:04:05")
28+
t.Format = t.getTimeFormat(formatInput)
2829
if t.CurrentDate.IsZero() {
2930
t.CurrentDate = time.Now()
3031
}
32+
3133
return true
3234
}
35+
36+
var timeFormatLookup = map[string]string{
37+
// Maps string names to their corresponding time package constants
38+
"Layout": time.Layout, // "01/02 03:04:05PM '06 -0700"
39+
"ANSIC": time.ANSIC, // "Mon Jan _2 15:04:05 2006"
40+
"UnixDate": time.UnixDate, // "Mon Jan _2 15:04:05 MST 2006"
41+
"RubyDate": time.RubyDate, // "Mon Jan 02 15:04:05 -0700 2006"
42+
"RFC822": time.RFC822, // "02 Jan 06 15:04 MST"
43+
"RFC822Z": time.RFC822Z, // "02 Jan 06 15:04 -0700"
44+
"RFC850": time.RFC850, // "Monday, 02-Jan-06 15:04:05 MST"
45+
"RFC1123": time.RFC1123, // "Mon, 02 Jan 2006 15:04:05 MST"
46+
"RFC1123Z": time.RFC1123Z, // "Mon, 02 Jan 2006 15:04:05 -0700"
47+
"RFC3339": time.RFC3339, // "2006-01-02T15:04:05Z07:00"
48+
"RFC3339Nano": time.RFC3339Nano, // "2006-01-02T15:04:05.999999999Z07:00"
49+
"Kitchen": time.Kitchen, // "3:04PM"
50+
"Stamp": time.Stamp, // "Jan _2 15:04:05"
51+
"StampMilli": time.StampMilli, // "Jan _2 15:04:05.000"
52+
"StampMicro": time.StampMicro, // "Jan _2 15:04:05.000000"
53+
"StampNano": time.StampNano, // "Jan _2 15:04:05.000000000"
54+
"DateTime": time.DateTime, // "2006-01-02 15:04:05"
55+
"DateOnly": time.DateOnly, // "2006-01-02"
56+
"TimeOnly": time.TimeOnly, // "15:04:05"
57+
}
58+
59+
// getTimeFormat returns the time format constant if the input matches a known format name,
60+
// otherwise returns the input unchanged
61+
func (t *Time) getTimeFormat(format string) string {
62+
if timeFormat, exists := timeFormatLookup[format]; exists {
63+
return timeFormat
64+
}
65+
return format
66+
}

0 commit comments

Comments
 (0)