From fa85fb714847545afefd2659701d5ff01ea3a0a7 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Wed, 28 Aug 2013 15:51:40 +0200 Subject: Added MultiChronos --- chronos/chronos.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/chronos/chronos.go b/chronos/chronos.go index 1d2c943..74fec19 100644 --- a/chronos/chronos.go +++ b/chronos/chronos.go @@ -210,7 +210,7 @@ func ParseChronos(s string) (c Chronos, err error) { return } c.Freq.Count = uint(count) - + var ok bool if c.Freq.Unit, ok = tuLookup[elems[3]]; !ok { err = fmt.Errorf("Unknown timeunit %s", elems[3]) @@ -227,3 +227,54 @@ func ParseChronos(s string) (c Chronos, err error) { return } + +type MultiChronos []Chronos + +func (mc MultiChronos) NextAfter(t time.Time) time.Time { + var nearest time.Time + + for _, c := range mc { + next := c.NextAfter(t) + if next.IsZero() { + continue + } + + if nearest.IsZero() { + nearest = next + } else if next.Before(nearest) { + nearest = next + } + } + + return nearest +} + +func (mc MultiChronos) String() (s string) { + sep := "" + + for _, c := range mc { + s += sep + c.String() + sep = "\n" + } + + return +} + +func ParseMultiChronos(s string) (mc MultiChronos, err error) { + parts := strings.Split(s, "\n") + for l, _part := range parts { + part := strings.TrimSpace(_part) + if part == "" { + continue + } + + c, err := ParseChronos(part) + if err != nil { + return nil, fmt.Errorf("Line %d: %s", l+1, err) + } + + mc = append(mc, c) + } + + return +} -- cgit v1.2.3-70-g09d2