Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Odd End semantics #69

Open
andig opened this issue Jan 8, 2025 · 0 comments
Open

Odd End semantics #69

andig opened this issue Jan 8, 2025 · 0 comments
Assignees

Comments

@andig
Copy link

andig commented Jan 8, 2025

Your Question

I realise that this library is really old (or rather grown-up) and wide-spread. This make 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants