now icon indicating copy to clipboard operation
now copied to clipboard

Odd End semantics

Open andig opened this issue 11 months ago • 0 comments

Your Question

I realise that this library is really old (or rather grown-up) and wide-spread. This makes my question awkward, but here it is.

Looking at the code, I've stumbled over the semantics of the End functions, which define the end of a period as 1ns before the start of the next period. That seems odd:

  • mathematically a time period would usually be described as half-open interval [start, end) where end is actually the beginning of the next period
  • 1ns difference is a somewhat arbitrary choice, simply being the smallest time increment that Go can handle

A similar pattern can be found in the Between function. I would have expected that something between begin of, say, a day and end of day would be anything on the same date, but that's not what Between does:

// Between check time between the begin, end time or not
func (now *Now) Between(begin, end string) bool {
	beginTime := now.MustParse(begin)
	endTime := now.MustParse(end)
	return now.After(beginTime) && now.Before(endTime)
}

This excludes start of day in this example and it also excludes the last nanosecond of the day according to the End semantics. The imho "proper" way to write this would have been:

return !now.Before(beginTime) && now.Before(endTime)

So my question is: why has the End semantic been chosen that way?

Expected answer

Enlightenment and saying thank you for a wonderful and elegant library!

andig avatar Jan 08 '25 21:01 andig