mirror of
https://github.com/kopia/kopia.git
synced 2026-01-11 07:57:53 -05:00
39 lines
737 B
Go
39 lines
737 B
Go
// Package jsonencoding defines common types with JSON marshalers.
|
|
package jsonencoding
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// Duration adds text/json (un)marshaling functions to time.Duration.
|
|
type Duration struct {
|
|
time.Duration
|
|
}
|
|
|
|
// MarshalText writes d as text.
|
|
func (d Duration) MarshalText() ([]byte, error) {
|
|
return []byte(d.String()), nil
|
|
}
|
|
|
|
// UnmarshalText read d from a text representation.
|
|
func (d *Duration) UnmarshalText(b []byte) error {
|
|
s := string(bytes.TrimSpace(b))
|
|
|
|
f, err := strconv.ParseFloat(s, 64)
|
|
if err == nil {
|
|
d.Duration = time.Duration(f)
|
|
|
|
return nil
|
|
}
|
|
|
|
d.Duration, err = time.ParseDuration(s)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid duration %s: %w", s, err)
|
|
}
|
|
|
|
return nil
|
|
}
|