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
proposal: time: add binning #54392
Comments
Maybe something like this? https://go.dev/play/p/umMkwm4nBiE If you're able to link up any Go code in the wild (eg from the Go source or other open source projects) that are doing this kind of calculation currently, or any packages out there that implement it, that would probably help further guide discussion and possible implementation. |
Thanks @danp at a glance that looks along the right lines. Unfortunately for all their boasts about being postgres compatible, Cockroach have not implemented I'm a bit busy with $work at the moment, but I'll aim to find some time to both revisit your example and also see if I can find any open source stuff lurking around. |
The proposal is
This is a reasonably simple function. The question is how often it comes up. |
Adding to minutes. The question remains how commonly needed this is. |
This proposal has been added to the active column of the proposals project |
As noted earlier, this function is pretty short and easy to write outside the standard library:
This one does have the downside of not working if t.Sub(origin) overflows or underflows, which happens if the origin time is more than 584 years away from the current time. For any actual use of Bin that's not going to happen, so this code is fine in a third-party library. (If it was in package time we'd have to deal with that case.) I've asked twice for how often this comes up with little response, so it sounds like maybe we should leave this for implementation outside the standard library. |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
At present the nearest thing to this proposal is
func (t Time) Round(d Duration) Time
.The problem with that function is that it is essentially a simple mathematical round. There is no option to take an origin timestamp into account.
What I am suggesting is something closer to, for example, the PostgreSQL
date_bin
function. I have copy/pasted the examples from the PG website to give you a feel:The present Go behaviour is closer to the first example (i.e. 15:44:17 gets rounded to the nearest 15 minute, i.e. 15:30:00).
However Go has no stdlib functionality to achieve what is shown in the second example (i.e. 15:44:17 gets rounded to 15:32:30 because the base "origin" is defined as 00:02:30 and therefore the 15 minute bins are calculated on the basis of 00:02:30 instead of 00:00:00).
The text was updated successfully, but these errors were encountered: