MarshalText implements the encoding.TextMarshaler interface.
()
| 14 | |
| 15 | // MarshalText implements the encoding.TextMarshaler interface. |
| 16 | func (d Duration) MarshalText() ([]byte, error) { |
| 17 | if d == 0 { |
| 18 | return nil, nil |
| 19 | } |
| 20 | |
| 21 | out := "PT" |
| 22 | if d < 0 { |
| 23 | d *= -1 |
| 24 | out = "-" + out |
| 25 | } |
| 26 | |
| 27 | h := time.Duration(d) / time.Hour |
| 28 | m := time.Duration(d) % time.Hour / time.Minute |
| 29 | s := time.Duration(d) % time.Minute / time.Second |
| 30 | ns := time.Duration(d) % time.Second |
| 31 | if h > 0 { |
| 32 | out += fmt.Sprintf("%dH", h) |
| 33 | } |
| 34 | if m > 0 { |
| 35 | out += fmt.Sprintf("%dM", m) |
| 36 | } |
| 37 | if s > 0 || ns > 0 { |
| 38 | out += fmt.Sprintf("%d", s) |
| 39 | if ns > 0 { |
| 40 | out += strings.TrimRight(fmt.Sprintf(".%09d", ns), "0") |
| 41 | } |
| 42 | out += "S" |
| 43 | } |
| 44 | |
| 45 | return []byte(out), nil |
| 46 | } |
| 47 | |
| 48 | const ( |
| 49 | day = 24 * time.Hour |
nothing calls this directly
no outgoing calls
no test coverage detected