Skip to content

Commit

Permalink
[POA-1965] Add truncate function in time span (#218)
Browse files Browse the repository at this point in the history
Added a new `truncate()` function in time span class, which truncate
both start and end time by the given duration.
  • Loading branch information
mudit-postman authored Oct 25, 2024
1 parent 3b01e02 commit 0928f36
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions time_span/time_span.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0928f36

Please sign in to comment.