jobwatch/app/timespec/timespec.go

35 lines
588 B
Go

package timespec
type TimeUnit string
const (
Sec = "s"
Min = "m"
Hour = "h"
Day = "d"
)
type TimeSpec struct {
Value int `yaml:"val"`
Unit TimeUnit `yaml:"unit"`
}
func (e TimeSpec) HasValidUnit() bool {
return (e.Unit == "" ||
e.Unit == Sec ||
e.Unit == Min ||
e.Unit == Hour ||
e.Unit == Day)
}
func (e TimeSpec) AsSeconds() int {
if e.Unit == "" || e.Unit == Min {
return e.Value * 60
} else if e.Unit == "" || e.Unit == Hour {
return e.Value * 60 * 60
} else if e.Unit == "" || e.Unit == Day {
return e.Value * 60 * 60 * 24
}
return e.Value
}