e113b276e7
Merge in DNS/adguard-home from 2504-querylog-ivl to master
Updates #2504.
Squashed commit of the following:
commit 5d15a6f735cd195fc81c8af909b56fbc7db1fe21
Merge: 8cd5c30d 97073d0d
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Thu Jul 1 18:45:10 2021 +0300
Merge branch 'master' into 2504-querylog-ivl
commit 8cd5c30de6f72d4b12162dbc9e3d90132795fe94
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Thu Jul 1 18:35:50 2021 +0300
client: fix fmt
commit e95d462c31d886bacec0735acc567fec7c962149
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Thu Jul 1 17:58:06 2021 +0300
home: imp code
commit 48737b249c52a997a4f34dac45fbaf699477b007
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Thu Jul 1 17:23:18 2021 +0300
home: imp duration
commit 44f5dc3d3ada5120d74caa24cace9a253b8f15d3
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Thu Jul 1 16:55:31 2021 +0300
home: imp code, docs
commit bb2826521b7e5d248ce2ab686528219c312b8ba2
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Thu Jul 1 16:11:40 2021 +0300
all: imp code, docs
commit d688aed1f340807a8bac8807c263956b0fc16f5b
Author: Eugene Burkov <e.burkov@adguard.com>
Date: Thu Jul 1 13:49:42 2021 +0300
all: change querylog interval setting format
60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package home
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
)
|
|
|
|
// Duration is a wrapper for time.Duration providing functionality for encoding.
|
|
type Duration struct {
|
|
// time.Duration is embedded here to avoid implementing all the methods.
|
|
time.Duration
|
|
}
|
|
|
|
// String implements the fmt.Stringer interface for Duration. It wraps
|
|
// time.Duration.String method and additionally cuts off non-leading zero values
|
|
// of minutes and seconds. Some values which are differ between the
|
|
// implementations:
|
|
//
|
|
// Duration: "1m", time.Duration: "1m0s"
|
|
// Duration: "1h", time.Duration: "1h0m0s"
|
|
// Duration: "1h1m", time.Duration: "1h1m0s"
|
|
//
|
|
func (d Duration) String() (str string) {
|
|
str = d.Duration.String()
|
|
secs := d.Seconds()
|
|
var secsInt int
|
|
if secsInt = int(secs); float64(secsInt) != secs || secsInt%60 != 0 {
|
|
return str
|
|
}
|
|
|
|
const (
|
|
tailMin = len(`0s`)
|
|
tailMinSec = len(`0m0s`)
|
|
)
|
|
|
|
if (secsInt%3600)/60 != 0 {
|
|
return str[:len(str)-tailMin]
|
|
}
|
|
|
|
return str[:len(str)-tailMinSec]
|
|
}
|
|
|
|
// MarshalText implements the encoding.TextMarshaler interface for Duration.
|
|
func (d Duration) MarshalText() (text []byte, err error) {
|
|
return []byte(d.String()), nil
|
|
}
|
|
|
|
// UnmarshalText implements the encoding.TextUnmarshaler interface for
|
|
// *Duration.
|
|
//
|
|
// TODO(e.burkov): Make it able to parse larger units like days.
|
|
func (d *Duration) UnmarshalText(b []byte) (err error) {
|
|
defer func() { err = errors.Annotate(err, "unmarshalling duration: %w") }()
|
|
|
|
d.Duration, err = time.ParseDuration(string(b))
|
|
|
|
return err
|
|
}
|