@@ -339,11 +339,9 @@ func TestLazyLogEntriesFilter(t *testing.T) {
339
339
func TestSecondTimeFormatting (t * testing.T ) {
340
340
t .Parallel ()
341
341
342
- cfg := getTimestampFormattingConfig (config .FieldKindSecondTime )
343
-
344
342
expectedOutput := time .Unix (1 , 0 ).UTC ().Format (time .RFC3339 )
345
343
346
- secondsTestCases := [... ]TimeFormattingTestCase {{
344
+ secondsTestCases := [... ]timeFormattingTestCase {{
347
345
TestName : "Seconds (float)" ,
348
346
JSON : `{"timestamp":1.0}` ,
349
347
ExpectedOutput : expectedOutput ,
@@ -369,6 +367,8 @@ func TestSecondTimeFormatting(t *testing.T) {
369
367
t .Run (testCase .TestName , func (t * testing.T ) {
370
368
t .Parallel ()
371
369
370
+ cfg := getTimestampFormattingConfig (config .FieldKindSecondTime , testCase .Format )
371
+
372
372
actual := parseTableRow (t , testCase .JSON , cfg )
373
373
assert .Equal (t , testCase .ExpectedOutput , actual [0 ])
374
374
})
@@ -378,11 +378,9 @@ func TestSecondTimeFormatting(t *testing.T) {
378
378
func TestMillisecondTimeFormatting (t * testing.T ) {
379
379
t .Parallel ()
380
380
381
- cfg := getTimestampFormattingConfig (config .FieldKindMilliTime )
382
-
383
381
expectedOutput := time .Unix (2 , 0 ).UTC ().Format (time .RFC3339 )
384
382
385
- millisecondTestCases := [... ]TimeFormattingTestCase {{
383
+ millisecondTestCases := [... ]timeFormattingTestCase {{
386
384
TestName : "Milliseconds (float)" ,
387
385
JSON : `{"timestamp":2000.0}` ,
388
386
ExpectedOutput : expectedOutput ,
@@ -404,6 +402,8 @@ func TestMillisecondTimeFormatting(t *testing.T) {
404
402
t .Run (testCase .TestName , func (t * testing.T ) {
405
403
t .Parallel ()
406
404
405
+ cfg := getTimestampFormattingConfig (config .FieldKindMilliTime , testCase .Format )
406
+
407
407
actual := parseTableRow (t , testCase .JSON , cfg )
408
408
assert .Equal (t , testCase .ExpectedOutput , actual [0 ])
409
409
})
@@ -413,11 +413,9 @@ func TestMillisecondTimeFormatting(t *testing.T) {
413
413
func TestMicrosecondTimeFormatting (t * testing.T ) {
414
414
t .Parallel ()
415
415
416
- cfg := getTimestampFormattingConfig (config .FieldKindMicroTime )
417
-
418
416
expectedOutput := time .Unix (4 , 0 ).UTC ().Format (time .RFC3339 )
419
417
420
- microsecondTestCases := [... ]TimeFormattingTestCase {{
418
+ microsecondTestCases := [... ]timeFormattingTestCase {{
421
419
TestName : "Microseconds (float)" ,
422
420
JSON : `{"timestamp":4000000.0}` ,
423
421
ExpectedOutput : expectedOutput ,
@@ -439,6 +437,8 @@ func TestMicrosecondTimeFormatting(t *testing.T) {
439
437
t .Run (testCase .TestName , func (t * testing.T ) {
440
438
t .Parallel ()
441
439
440
+ cfg := getTimestampFormattingConfig (config .FieldKindMicroTime , testCase .Format )
441
+
442
442
actual := parseTableRow (t , testCase .JSON , cfg )
443
443
assert .Equal (t , testCase .ExpectedOutput , actual [0 ])
444
444
})
@@ -448,7 +448,7 @@ func TestMicrosecondTimeFormatting(t *testing.T) {
448
448
func TestFormattingUnknown (t * testing.T ) {
449
449
t .Parallel ()
450
450
451
- cfg := getTimestampFormattingConfig (config .FieldKind ("unknown" ))
451
+ cfg := getTimestampFormattingConfig (config .FieldKind ("unknown" ), config . DefaultTimeFormat )
452
452
453
453
actual := parseTableRow (t , `{"timestamp": 1}` , cfg )
454
454
assert .Equal (t , "1" , actual [0 ])
@@ -457,7 +457,7 @@ func TestFormattingUnknown(t *testing.T) {
457
457
func TestFormattingAny (t * testing.T ) {
458
458
t .Parallel ()
459
459
460
- cfg := getTimestampFormattingConfig (config .FieldKindAny )
460
+ cfg := getTimestampFormattingConfig (config .FieldKindAny , config . DefaultTimeFormat )
461
461
462
462
actual := parseTableRow (t , `{"timestamp": 1}` , cfg )
463
463
assert .Equal (t , "1" , actual [0 ])
@@ -466,9 +466,7 @@ func TestFormattingAny(t *testing.T) {
466
466
func TestNumericKindTimeFormatting (t * testing.T ) {
467
467
t .Parallel ()
468
468
469
- cfg := getTimestampFormattingConfig (config .FieldKindNumericTime )
470
-
471
- numericKindCases := [... ]TimeFormattingTestCase {{
469
+ numericKindCases := [... ]timeFormattingTestCase {{
472
470
TestName : "Date passthru" ,
473
471
JSON : `{"timestamp":"2023-10-08 20:00:00"}` ,
474
472
ExpectedOutput : "2023-10-08 20:00:00" ,
@@ -522,6 +520,8 @@ func TestNumericKindTimeFormatting(t *testing.T) {
522
520
t .Run (testCase .TestName , func (t * testing.T ) {
523
521
t .Parallel ()
524
522
523
+ cfg := getTimestampFormattingConfig (config .FieldKindNumericTime , testCase .Format )
524
+
525
525
actual := parseTableRow (t , testCase .JSON , cfg )
526
526
assert .Equal (t , testCase .ExpectedOutput , actual [0 ])
527
527
})
@@ -613,6 +613,56 @@ func TestLazyLogEntryLogEntry(t *testing.T) {
613
613
})
614
614
}
615
615
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
+
616
666
func parseLazyLogEntry (tb testing.TB , value string , cfg * config.Config ) source.LazyLogEntry {
617
667
tb .Helper ()
618
668
@@ -653,20 +703,28 @@ func getFieldKindToValue(cfg *config.Config, entries []string) map[config.FieldK
653
703
return fieldKindToValue
654
704
}
655
705
656
- type TimeFormattingTestCase struct {
706
+ type timeFormattingTestCase struct {
657
707
TestName string
658
708
JSON string
659
709
ExpectedOutput string
710
+ Format string
660
711
}
661
712
662
- func getTimestampFormattingConfig (fieldKind config.FieldKind ) * config.Config {
713
+ func getTimestampFormattingConfig (fieldKind config.FieldKind , format string ) * config.Config {
663
714
cfg := config .GetDefaultConfig ()
664
715
716
+ var timeFormat * string
717
+
718
+ if format != "" {
719
+ timeFormat = & format
720
+ }
721
+
665
722
cfg .Fields = []config.Field {{
666
723
Title : "Time" ,
667
724
Kind : fieldKind ,
668
725
References : []string {"$.timestamp" , "$.time" , "$.t" , "$.ts" },
669
726
Width : 30 ,
727
+ TimeFormat : timeFormat ,
670
728
}}
671
729
672
730
return cfg
0 commit comments