2023-02-07 06:48:12 +03:00
|
|
|
package time
|
|
|
|
|
|
|
|
import (
|
2023-02-13 14:02:08 +03:00
|
|
|
"encoding/json"
|
2023-05-27 23:55:08 +03:00
|
|
|
"errors"
|
2023-02-07 06:48:12 +03:00
|
|
|
"fmt"
|
2023-05-27 23:55:08 +03:00
|
|
|
"strconv"
|
2023-02-07 06:48:12 +03:00
|
|
|
"time"
|
2024-11-01 11:23:29 +03:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
2023-02-07 06:48:12 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Duration int64
|
|
|
|
|
|
|
|
func ParseDuration(s string) (time.Duration, error) {
|
|
|
|
if s == "" {
|
2024-12-06 18:52:37 +03:00
|
|
|
return 0, errors.New(`time: invalid duration "` + s + `"`)
|
2023-02-07 06:48:12 +03:00
|
|
|
}
|
|
|
|
|
2023-05-27 23:55:08 +03:00
|
|
|
var p int
|
|
|
|
var hours int
|
|
|
|
loop:
|
|
|
|
for i, r := range s {
|
|
|
|
switch r {
|
|
|
|
case 's', 'm':
|
|
|
|
break loop
|
|
|
|
case 'h':
|
|
|
|
d, err := strconv.Atoi(s[p:i])
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.New("time: invalid duration " + s)
|
2023-02-07 06:48:12 +03:00
|
|
|
}
|
2023-05-27 23:55:08 +03:00
|
|
|
hours += d
|
|
|
|
p = i + 1
|
|
|
|
case 'd':
|
|
|
|
d, err := strconv.Atoi(s[p:i])
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.New("time: invalid duration " + s)
|
|
|
|
}
|
|
|
|
hours += d * 24
|
|
|
|
p = i + 1
|
|
|
|
case 'y':
|
|
|
|
n, err := strconv.Atoi(s[p:i])
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.New("time: invalid duration " + s)
|
|
|
|
}
|
|
|
|
var d int
|
|
|
|
for j := n - 1; j >= 0; j-- {
|
|
|
|
d += time.Date(time.Now().Year()+j, time.December, 31, 0, 0, 0, 0, time.Local).YearDay()
|
|
|
|
}
|
|
|
|
hours += d * 24
|
|
|
|
p = i + 1
|
2023-02-07 06:48:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-27 23:55:08 +03:00
|
|
|
return time.ParseDuration(fmt.Sprintf("%dh%s", hours, s[p:]))
|
2023-02-07 06:48:12 +03:00
|
|
|
}
|
2023-02-13 14:02:08 +03:00
|
|
|
|
2024-11-01 11:23:29 +03:00
|
|
|
func (d Duration) MarshalYAML() (interface{}, error) {
|
|
|
|
return time.Duration(d).String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Duration) UnmarshalYAML(n *yaml.Node) error {
|
|
|
|
var v interface{}
|
|
|
|
if err := yaml.Unmarshal([]byte(n.Value), &v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch value := v.(type) {
|
|
|
|
case float64:
|
|
|
|
*d = Duration(time.Duration(value))
|
|
|
|
return nil
|
|
|
|
case string:
|
|
|
|
dv, err := ParseDuration(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*d = Duration(dv)
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid duration")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:02:08 +03:00
|
|
|
func (d Duration) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(time.Duration(d).String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Duration) UnmarshalJSON(b []byte) error {
|
|
|
|
var v interface{}
|
|
|
|
if err := json.Unmarshal(b, &v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch value := v.(type) {
|
|
|
|
case float64:
|
|
|
|
*d = Duration(time.Duration(value))
|
|
|
|
return nil
|
|
|
|
case string:
|
2023-05-29 13:59:30 +03:00
|
|
|
dv, err := ParseDuration(value)
|
2023-02-13 14:02:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*d = Duration(dv)
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid duration")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
func (d Duration) MarshalYAML() (interface{}, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-02-13 14:03:02 +03:00
|
|
|
func (d Duration) UnmarshalYAML(fn func(interface{}) error) error
|
2023-02-13 14:02:08 +03:00
|
|
|
*/
|