diff options
-rw-r--r-- | chronos/chronos.go | 12 | ||||
-rw-r--r-- | chronos/chronos_test.go | 16 |
2 files changed, 15 insertions, 13 deletions
diff --git a/chronos/chronos.go b/chronos/chronos.go index 18bf775..e66fc76 100644 --- a/chronos/chronos.go +++ b/chronos/chronos.go @@ -16,7 +16,7 @@ const ( Year ) -var NilTime time.Time +var nilTime time.Time func (tu TimeUnit) String() string { switch tu { @@ -123,24 +123,26 @@ func (f Frequency) addTo(t time.Time, mul uint) time.Time { case Year: return time.Date(year+fq, month, day, hour, min, sec, 0, loc) default: - return NilTime + return nilTime } } func (f Frequency) minApprox() time.Duration { return time.Duration(f.Count) * f.Unit.minApprox() } func (f Frequency) maxApprox() time.Duration { return time.Duration(f.Count) * f.Unit.maxApprox() } +// Chronos describes a time schedule. It has a start and optional end point and an optional frequency. type Chronos struct { Start, End time.Time Freq Frequency } +// NextAfter calculates the next time in the schedule after t. If no such time exists, nil is returned (test with Time.IsZero()). func (c Chronos) NextAfter(t time.Time) time.Time { if !t.After(c.Start) { return c.Start } if c.Freq.Count == 0 { - return NilTime + return nilTime } d := t.Sub(c.Start) @@ -154,10 +156,10 @@ func (c Chronos) NextAfter(t time.Time) time.Time { continue } if (!c.End.IsZero()) && t2.After(c.End) { - return NilTime + return nilTime } return t2 } - return NilTime // Should actually never happen... + return nilTime // Should actually never happen... } diff --git a/chronos/chronos_test.go b/chronos/chronos_test.go index 5efb7e9..692af14 100644 --- a/chronos/chronos_test.go +++ b/chronos/chronos_test.go @@ -18,14 +18,14 @@ func TestChronos(t *testing.T) { now time.Time want time.Time }{ - {mktime(1991, 4, 30, 0, 0), NilTime, Year, 1, mktime(2013, 8, 26, 13, 37), mktime(2014, 4, 30, 0, 0)}, - {mktime(2013, 1, 1, 0, 0), NilTime, Year, 0, mktime(2013, 8, 26, 13, 37), NilTime}, - {mktime(2013, 1, 1, 0, 0), NilTime, Year, 0, mktime(2012, 1, 1, 0, 0), mktime(2013, 1, 1, 0, 0)}, - {mktime(1900, 12, 24, 12, 34), NilTime, Year, 5, mktime(2013, 8, 26, 13, 37), mktime(2015, 12, 24, 12, 34)}, - {mktime(1900, 12, 24, 12, 34), mktime(2010, 1, 1, 1, 1), Year, 5, mktime(2013, 8, 26, 13, 37), NilTime}, - {mktime(2013, 8, 1, 4, 2), NilTime, Week, 3, mktime(2013, 8, 26, 13, 37), mktime(2013, 9, 12, 4, 2)}, - {mktime(2013, 8, 26, 13, 37), NilTime, Year, 0, mktime(2013, 8, 26, 13, 37), mktime(2013, 8, 26, 13, 37)}, - {mktime(2013, 8, 25, 13, 37), NilTime, Day, 1, mktime(2013, 8, 26, 13, 37), mktime(2013, 8, 26, 13, 37)}, + {mktime(1991, 4, 30, 0, 0), nilTime, Year, 1, mktime(2013, 8, 26, 13, 37), mktime(2014, 4, 30, 0, 0)}, + {mktime(2013, 1, 1, 0, 0), nilTime, Year, 0, mktime(2013, 8, 26, 13, 37), nilTime}, + {mktime(2013, 1, 1, 0, 0), nilTime, Year, 0, mktime(2012, 1, 1, 0, 0), mktime(2013, 1, 1, 0, 0)}, + {mktime(1900, 12, 24, 12, 34), nilTime, Year, 5, mktime(2013, 8, 26, 13, 37), mktime(2015, 12, 24, 12, 34)}, + {mktime(1900, 12, 24, 12, 34), mktime(2010, 1, 1, 1, 1), Year, 5, mktime(2013, 8, 26, 13, 37), nilTime}, + {mktime(2013, 8, 1, 4, 2), nilTime, Week, 3, mktime(2013, 8, 26, 13, 37), mktime(2013, 9, 12, 4, 2)}, + {mktime(2013, 8, 26, 13, 37), nilTime, Year, 0, mktime(2013, 8, 26, 13, 37), mktime(2013, 8, 26, 13, 37)}, + {mktime(2013, 8, 25, 13, 37), nilTime, Day, 1, mktime(2013, 8, 26, 13, 37), mktime(2013, 8, 26, 13, 37)}, } for i, e := range tbl { |