123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package grpcutil
- import (
- "strconv"
- "time"
- )
- const maxTimeoutValue int64 = 100000000 - 1
- func div(d, r time.Duration) int64 {
- if d%r > 0 {
- return int64(d/r + 1)
- }
- return int64(d / r)
- }
- func EncodeDuration(t time.Duration) string {
-
- if t <= 0 {
- return "0n"
- }
- if d := div(t, time.Nanosecond); d <= maxTimeoutValue {
- return strconv.FormatInt(d, 10) + "n"
- }
- if d := div(t, time.Microsecond); d <= maxTimeoutValue {
- return strconv.FormatInt(d, 10) + "u"
- }
- if d := div(t, time.Millisecond); d <= maxTimeoutValue {
- return strconv.FormatInt(d, 10) + "m"
- }
- if d := div(t, time.Second); d <= maxTimeoutValue {
- return strconv.FormatInt(d, 10) + "S"
- }
- if d := div(t, time.Minute); d <= maxTimeoutValue {
- return strconv.FormatInt(d, 10) + "M"
- }
-
- return strconv.FormatInt(div(t, time.Hour), 10) + "H"
- }
|