@@ -339,11 +339,9 @@ func TestLazyLogEntriesFilter(t *testing.T) {
339339func TestSecondTimeFormatting (t * testing.T ) {
340340 t .Parallel ()
341341
342- cfg := getTimestampFormattingConfig (config .FieldKindSecondTime )
343-
344342 expectedOutput := time .Unix (1 , 0 ).UTC ().Format (time .RFC3339 )
345343
346- secondsTestCases := [... ]TimeFormattingTestCase {{
344+ secondsTestCases := [... ]timeFormattingTestCase {{
347345 TestName : "Seconds (float)" ,
348346 JSON : `{"timestamp":1.0}` ,
349347 ExpectedOutput : expectedOutput ,
@@ -369,6 +367,8 @@ func TestSecondTimeFormatting(t *testing.T) {
369367 t .Run (testCase .TestName , func (t * testing.T ) {
370368 t .Parallel ()
371369
370+ cfg := getTimestampFormattingConfig (config .FieldKindSecondTime , testCase .Format )
371+
372372 actual := parseTableRow (t , testCase .JSON , cfg )
373373 assert .Equal (t , testCase .ExpectedOutput , actual [0 ])
374374 })
@@ -378,11 +378,9 @@ func TestSecondTimeFormatting(t *testing.T) {
378378func TestMillisecondTimeFormatting (t * testing.T ) {
379379 t .Parallel ()
380380
381- cfg := getTimestampFormattingConfig (config .FieldKindMilliTime )
382-
383381 expectedOutput := time .Unix (2 , 0 ).UTC ().Format (time .RFC3339 )
384382
385- millisecondTestCases := [... ]TimeFormattingTestCase {{
383+ millisecondTestCases := [... ]timeFormattingTestCase {{
386384 TestName : "Milliseconds (float)" ,
387385 JSON : `{"timestamp":2000.0}` ,
388386 ExpectedOutput : expectedOutput ,
@@ -404,6 +402,8 @@ func TestMillisecondTimeFormatting(t *testing.T) {
404402 t .Run (testCase .TestName , func (t * testing.T ) {
405403 t .Parallel ()
406404
405+ cfg := getTimestampFormattingConfig (config .FieldKindMilliTime , testCase .Format )
406+
407407 actual := parseTableRow (t , testCase .JSON , cfg )
408408 assert .Equal (t , testCase .ExpectedOutput , actual [0 ])
409409 })
@@ -413,11 +413,9 @@ func TestMillisecondTimeFormatting(t *testing.T) {
413413func TestMicrosecondTimeFormatting (t * testing.T ) {
414414 t .Parallel ()
415415
416- cfg := getTimestampFormattingConfig (config .FieldKindMicroTime )
417-
418416 expectedOutput := time .Unix (4 , 0 ).UTC ().Format (time .RFC3339 )
419417
420- microsecondTestCases := [... ]TimeFormattingTestCase {{
418+ microsecondTestCases := [... ]timeFormattingTestCase {{
421419 TestName : "Microseconds (float)" ,
422420 JSON : `{"timestamp":4000000.0}` ,
423421 ExpectedOutput : expectedOutput ,
@@ -439,6 +437,8 @@ func TestMicrosecondTimeFormatting(t *testing.T) {
439437 t .Run (testCase .TestName , func (t * testing.T ) {
440438 t .Parallel ()
441439
440+ cfg := getTimestampFormattingConfig (config .FieldKindMicroTime , testCase .Format )
441+
442442 actual := parseTableRow (t , testCase .JSON , cfg )
443443 assert .Equal (t , testCase .ExpectedOutput , actual [0 ])
444444 })
@@ -448,7 +448,7 @@ func TestMicrosecondTimeFormatting(t *testing.T) {
448448func TestFormattingUnknown (t * testing.T ) {
449449 t .Parallel ()
450450
451- cfg := getTimestampFormattingConfig (config .FieldKind ("unknown" ))
451+ cfg := getTimestampFormattingConfig (config .FieldKind ("unknown" ), config . DefaultTimeFormat )
452452
453453 actual := parseTableRow (t , `{"timestamp": 1}` , cfg )
454454 assert .Equal (t , "1" , actual [0 ])
@@ -457,7 +457,7 @@ func TestFormattingUnknown(t *testing.T) {
457457func TestFormattingAny (t * testing.T ) {
458458 t .Parallel ()
459459
460- cfg := getTimestampFormattingConfig (config .FieldKindAny )
460+ cfg := getTimestampFormattingConfig (config .FieldKindAny , config . DefaultTimeFormat )
461461
462462 actual := parseTableRow (t , `{"timestamp": 1}` , cfg )
463463 assert .Equal (t , "1" , actual [0 ])
@@ -466,9 +466,7 @@ func TestFormattingAny(t *testing.T) {
466466func TestNumericKindTimeFormatting (t * testing.T ) {
467467 t .Parallel ()
468468
469- cfg := getTimestampFormattingConfig (config .FieldKindNumericTime )
470-
471- numericKindCases := [... ]TimeFormattingTestCase {{
469+ numericKindCases := [... ]timeFormattingTestCase {{
472470 TestName : "Date passthru" ,
473471 JSON : `{"timestamp":"2023-10-08 20:00:00"}` ,
474472 ExpectedOutput : "2023-10-08 20:00:00" ,
@@ -522,6 +520,8 @@ func TestNumericKindTimeFormatting(t *testing.T) {
522520 t .Run (testCase .TestName , func (t * testing.T ) {
523521 t .Parallel ()
524522
523+ cfg := getTimestampFormattingConfig (config .FieldKindNumericTime , testCase .Format )
524+
525525 actual := parseTableRow (t , testCase .JSON , cfg )
526526 assert .Equal (t , testCase .ExpectedOutput , actual [0 ])
527527 })
@@ -613,6 +613,56 @@ func TestLazyLogEntryLogEntry(t *testing.T) {
613613 })
614614}
615615
616+ func TestTimeFormat (t * testing.T ) {
617+ t .Parallel ()
618+
619+ logDate := time .Date (
620+ 2000 , // Year.
621+ time .January ,
622+ 2 , // Day.
623+ 3 , // Hour.
624+ 4 , // Minutes.
625+ 5 , // Seconds.
626+ 0 , // Nanoseconds.
627+ time .UTC ,
628+ )
629+
630+ jsonContent := fmt .Sprintf (`{"timestamp":"%d"}` , logDate .Unix ())
631+
632+ numericKindCases := [... ]timeFormattingTestCase {{
633+ TestName : "RFC3339" ,
634+ JSON : jsonContent ,
635+ ExpectedOutput : logDate .Format (time .RFC3339 ),
636+ Format : time .RFC3339 ,
637+ }, {
638+ TestName : "RFC1123" ,
639+ JSON : jsonContent ,
640+ ExpectedOutput : logDate .Format (time .RFC1123 ),
641+ Format : time .RFC1123 ,
642+ }, {
643+ TestName : "TimeOnly" ,
644+ JSON : jsonContent ,
645+ ExpectedOutput : logDate .Format (time .TimeOnly ),
646+ Format : time .TimeOnly ,
647+ }, {
648+ TestName : "TimeOnly" ,
649+ JSON : jsonContent ,
650+ ExpectedOutput : "invalid" ,
651+ Format : "invalid" ,
652+ }}
653+
654+ for _ , testCase := range numericKindCases {
655+ t .Run (testCase .TestName , func (t * testing.T ) {
656+ t .Parallel ()
657+
658+ cfg := getTimestampFormattingConfig (config .FieldKindSecondTime , testCase .Format )
659+
660+ actual := parseTableRow (t , testCase .JSON , cfg )
661+ assert .Equal (t , testCase .ExpectedOutput , actual [0 ])
662+ })
663+ }
664+ }
665+
616666func parseLazyLogEntry (tb testing.TB , value string , cfg * config.Config ) source.LazyLogEntry {
617667 tb .Helper ()
618668
@@ -653,20 +703,28 @@ func getFieldKindToValue(cfg *config.Config, entries []string) map[config.FieldK
653703 return fieldKindToValue
654704}
655705
656- type TimeFormattingTestCase struct {
706+ type timeFormattingTestCase struct {
657707 TestName string
658708 JSON string
659709 ExpectedOutput string
710+ Format string
660711}
661712
662- func getTimestampFormattingConfig (fieldKind config.FieldKind ) * config.Config {
713+ func getTimestampFormattingConfig (fieldKind config.FieldKind , format string ) * config.Config {
663714 cfg := config .GetDefaultConfig ()
664715
716+ var timeFormat * string
717+
718+ if format != "" {
719+ timeFormat = & format
720+ }
721+
665722 cfg .Fields = []config.Field {{
666723 Title : "Time" ,
667724 Kind : fieldKind ,
668725 References : []string {"$.timestamp" , "$.time" , "$.t" , "$.ts" },
669726 Width : 30 ,
727+ TimeFormat : timeFormat ,
670728 }}
671729
672730 return cfg
0 commit comments