From 0928f36089fe928193750e13554ef4e5ee7c1b5f Mon Sep 17 00:00:00 2001 From: Mudit Joshi <116863733+mudit-postman@users.noreply.github.com> Date: Fri, 25 Oct 2024 13:18:16 +0530 Subject: [PATCH] [POA-1965] Add truncate function in time span (#218) Added a new `truncate()` function in time span class, which truncate both start and end time by the given duration. --- time_span/time_span.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/time_span/time_span.go b/time_span/time_span.go index 900ce5e..3cd80fd 100644 --- a/time_span/time_span.go +++ b/time_span/time_span.go @@ -119,6 +119,14 @@ func (t ClosedInterval) Combine(t2 ClosedInterval) ClosedInterval { } } +// Returns a new ClosedInterval with the start and end times truncated. +func (t ClosedInterval) Truncate(d time.Duration) ClosedInterval { + return ClosedInterval{ + Start: t.Start.Truncate(d), + End: t.End.Truncate(d), + } +} + // An half-open interval [start,end) // Empty intervals are represented by having Start >= End. type HalfOpenInterval BaseInterval @@ -205,6 +213,14 @@ func (t HalfOpenInterval) Combine(t2 HalfOpenInterval) HalfOpenInterval { } } +// Returns a new ClosedInterval with the start and end times truncated. +func (t HalfOpenInterval) Truncate(d time.Duration) HalfOpenInterval { + return HalfOpenInterval{ + Start: t.Start.Truncate(d), + End: t.End.Truncate(d), + } +} + type TimeSpan = HalfOpenInterval func NewTimeSpan(start time.Time, end time.Time) *TimeSpan {