From c77b07188cc2c9ea27f8104316b6b0296d17e92d Mon Sep 17 00:00:00 2001 From: Elijah Duffy Date: Tue, 22 Jul 2025 18:29:13 -0700 Subject: [PATCH] add comparison helpers --- clocktime.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/clocktime.go b/clocktime.go index 1fd46c3..c930268 100644 --- a/clocktime.go +++ b/clocktime.go @@ -54,6 +54,38 @@ func (t ClockTime) Time() time.Time { return time.Date(1970, 1, 1, t.Hour, t.Minute, t.Second, 0, time.UTC) } +// IsZero checks if the ClockTime is zero (00:00:00) or nil. +func (t *ClockTime) IsZero() bool { + return t == nil || (t.Hour == 0 && t.Minute == 0 && t.Second == 0) +} + +// Equal checks if two ClockTime instances are equal. +func (t ClockTime) Equal(other ClockTime) bool { + return t.Hour == other.Hour && t.Minute == other.Minute && t.Second == other.Second +} + +// Before checks if the ClockTime is before another ClockTime. +func (t ClockTime) Before(other ClockTime) bool { + if t.Hour != other.Hour { + return t.Hour < other.Hour + } + if t.Minute != other.Minute { + return t.Minute < other.Minute + } + return t.Second < other.Second +} + +// After checks if the ClockTime is after another ClockTime. +func (t ClockTime) After(other ClockTime) bool { + if t.Hour != other.Hour { + return t.Hour > other.Hour + } + if t.Minute != other.Minute { + return t.Minute > other.Minute + } + return t.Second > other.Second +} + // MarshalJSON implements the json.Marshaler interface for ClockTime. func (t ClockTime) MarshalJSON() ([]byte, error) { return json.Marshal(t.String())