From 42b4302ba32e503c0a8011baae0ca433c8592dd7 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Tue, 26 Oct 2021 17:24:45 +0200 Subject: index: allow dynamic formatting of message dates When a message has been sent today (or this year) allow formatting the date differently. For example, with: [ui] index-format=%-25.25n %-25.25D %s timestamp-format=2006 Jan 02, 15:04 GMT-0700 this-day-time-format=Today at 15:04 this-year-time-format=Jan 02 The message list would look like this (spaces collapsed): Robin Jarry Today at 16:30 [PATCH 1/2] bindings: prepare for more modifers bugzilla@dpdk.org Oct 26 [dpdk-dev] [Bug 839] pdump: any subsequent runs of pdump_autotest fail Holger Levsen 2020 Mar 15, 13:44 GMT+01 +1 (Re: FTP Team -- call for volunteers) Signed-off-by: Robin Jarry --- lib/format/format.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'lib/format/format.go') diff --git a/lib/format/format.go b/lib/format/format.go index 30e8be7f..16398865 100644 --- a/lib/format/format.go +++ b/lib/format/format.go @@ -47,8 +47,8 @@ type Ctx struct { MsgIsMarked bool } -func ParseMessageFormat(format string, timeFmt string, ctx Ctx) (string, - []interface{}, error) { +func ParseMessageFormat(format string, timeFmt string, thisDayTimeFmt string, + thisYearTimeFmt string, ctx Ctx) (string, []interface{}, error) { retval := make([]byte, 0, len(format)) var args []interface{} @@ -144,7 +144,8 @@ func ParseMessageFormat(format string, timeFmt string, ctx Ctx) (string, } retval = append(retval, 's') args = append(args, - dummyIfZeroDate(date.Local(), timeFmt)) + dummyIfZeroDate(date.Local(), + timeFmt, thisDayTimeFmt, thisYearTimeFmt)) case 'D': date := envelope.Date if date.IsZero() { @@ -152,7 +153,8 @@ func ParseMessageFormat(format string, timeFmt string, ctx Ctx) (string, } retval = append(retval, 's') args = append(args, - dummyIfZeroDate(date.Local(), timeFmt)) + dummyIfZeroDate(date.Local(), + timeFmt, thisDayTimeFmt, thisYearTimeFmt)) case 'f': if envelope == nil { return "", nil, @@ -374,9 +376,20 @@ handle_end_error: errors.New("reached end of string while parsing message format") } -func dummyIfZeroDate(date time.Time, format string) string { +func dummyIfZeroDate(date time.Time, format string, todayFormat string, + thisYearFormat string) string { if date.IsZero() { return strings.Repeat("?", len(format)) } + year, month, day := date.Date() + thisYear, thisMonth, thisDay := time.Now().Date() + if year == thisYear { + if month == thisMonth && day == thisDay && todayFormat != "" { + return date.Format(todayFormat) + } + if thisYearFormat != "" { + return date.Format(thisYearFormat) + } + } return date.Format(format) } -- cgit