File tree 3 files changed +88
-1
lines changed
3 files changed +88
-1
lines changed Original file line number Diff line number Diff line change @@ -94,7 +94,7 @@ func (u *using) writeCql(cql *bytes.Buffer) (names []string) {
94
94
95
95
if u .timeout != 0 {
96
96
writePreamble (cql )
97
- fmt .Fprintf (cql , "TIMEOUT %s " , u .timeout )
97
+ fmt .Fprintf (cql , "TIMEOUT %s " , formatDuration ( u .timeout ) )
98
98
} else if u .timeoutName != "" {
99
99
writePreamble (cql )
100
100
cql .WriteString ("TIMEOUT ? " )
Original file line number Diff line number Diff line change @@ -6,6 +6,9 @@ package qb
6
6
7
7
import (
8
8
"bytes"
9
+ "fmt"
10
+ "strings"
11
+ "time"
9
12
)
10
13
11
14
// placeholders returns a string with count ? placeholders joined with commas.
@@ -31,3 +34,26 @@ func (cols columns) writeCql(cql *bytes.Buffer) {
31
34
}
32
35
}
33
36
}
37
+
38
+ func formatDuration (d time.Duration ) string {
39
+ // Round the duration to the nearest millisecond
40
+ // Extract hours, minutes, seconds, and milliseconds
41
+ minutes := d / time .Minute
42
+ d %= time .Minute
43
+ seconds := d / time .Second
44
+ d %= time .Second
45
+ milliseconds := d / time .Millisecond
46
+
47
+ // Format the duration string
48
+ var res []string
49
+ if minutes > 0 {
50
+ res = append (res , fmt .Sprintf ("%dm" , minutes ))
51
+ }
52
+ if seconds > 0 {
53
+ res = append (res , fmt .Sprintf ("%ds" , seconds ))
54
+ }
55
+ if milliseconds > 0 {
56
+ res = append (res , fmt .Sprintf ("%dms" , milliseconds ))
57
+ }
58
+ return strings .Join (res , "" )
59
+ }
Original file line number Diff line number Diff line change
1
+ package qb
2
+
3
+ import (
4
+ "testing"
5
+ "time"
6
+ )
7
+
8
+ func TestFormatDuration (t * testing.T ) {
9
+ tests := []struct {
10
+ name string
11
+ input time.Duration
12
+ expected string
13
+ }{
14
+ {
15
+ name : "Zero duration" ,
16
+ input : 0 ,
17
+ expected : "" ,
18
+ },
19
+ {
20
+ input : 500 * time .Millisecond ,
21
+ expected : "500ms" ,
22
+ },
23
+ {
24
+ input : 10 * time .Second ,
25
+ expected : "10s" ,
26
+ },
27
+ {
28
+ input : 3 * time .Minute ,
29
+ expected : "3m" ,
30
+ },
31
+ {
32
+ input : (2 * time .Minute ) + (30 * time .Second ),
33
+ expected : "2m30s" ,
34
+ },
35
+ {
36
+ input : (15 * time .Second ) + (250 * time .Millisecond ),
37
+ expected : "15s250ms" ,
38
+ },
39
+ {
40
+ input : (1 * time .Minute ) + (45 * time .Second ) + (123 * time .Millisecond ),
41
+ expected : "1m45s123ms" ,
42
+ },
43
+ {
44
+ input : (5 * time .Minute ) + (1 * time .Second ) + (999 * time .Millisecond ),
45
+ expected : "5m1s999ms" ,
46
+ },
47
+ {
48
+ input : (2 * time .Second ) + (1500 * time .Millisecond ), // 3 seconds, 500ms
49
+ expected : "3s500ms" ,
50
+ },
51
+ }
52
+
53
+ for _ , tt := range tests {
54
+ t .Run (tt .name , func (t * testing.T ) {
55
+ actual := formatDuration (tt .input )
56
+ if actual != tt .expected {
57
+ t .Errorf ("got %q, want %q" , actual , tt .expected )
58
+ }
59
+ })
60
+ }
61
+ }
You can’t perform that action at this time.
0 commit comments