Best means of rounding times to nearest 6-min increment?

Does anybody have suggestions for the most efficient way to round the current time to the nearest 6-minute increment (i.e. XX:06, XX:12, XX:18, etc.)?

I could write a very long shortcut to do this with built-in calculate steps, but I’m wondering if PCalc, or some other app (or built-in action) can do this more easily. Suggestions welcome.

I wrote a time calculator in python a while back - if you have pythonista this might be a starting point:

Divide minutes by 6, round up result and then multiply it by six

Shouldn’t you sometimes round down to get the nearest a well as rounding up?

E.g.
Nearest 6 minute interval to 7 minutes is 6 minutes, but:
7/6 = 1.166666…
Rounding up, would be 2.
2*6=12.

If it really is the nearest, the it should be rounding off to the nearest whole number, not rounding up.

In addition times closest to 60 minutes would also need to be accounted for as they would need to round “up” to zero.

1 Like

That’s right.

You also have to deal with adding leading zeros back in when re-assembling the time following the calculations.

It can be done pretty efficiently in most any code (I’ve written some javascript versions myself), But replicating that code in Shortcuts takes a ton of steps, so I was wondering if there was some app that did it more efficiently.

If anybody knows of one, please let me know. (Otherwise, I’ll either write it up as a separate shortcut, or maybe a scriptable script or something).

Thanks.

In short, it very doable, but a big pain. That’s why I’m wondering if there’s some app that would make this trivial.

Just out of curiosity I tried to solve this problem. It took me only 8 actions with without any zero padding or handling the full hour on its own. I simply add the difference between the wanted 6 minute interval and the saved time to the saved time: https://www.icloud.com/shortcuts/07a76ded3d9d43e0a9ed11ad36406004 (Warning, iOS 12 shortcut)

In short: get minutes, divide by 6, round, multiply by 6, subtract initial minutes, add that as minutes to the original date

1 Like

I just suggested to PCalc on Twitter that this might be a good shortcut action for them to add - rounding X to the nearest multiple of Y :slight_smile:

1 Like

Just for completeness then, try this. It’s written on iOS12, but hopefully it’s 13 compatible, and it may even be possible to streamline further in 13. But as it stands I’ve got it down to three native Shortcuts steps to do the rounding plus a fourth to ask for the time to run the example :wink:

https://www.icloud.com/shortcuts/da7e711f9d4f44849ae22c24503017d6

I don’t know if it’ll qualify as “best”, but relatively brief and entirely native I think is at least reasonable - assuming it works as expected of course :laughing:

1 Like

Now that’s elegant. I like it.

wow I have never seen that technique of embedding javascript before - clever!

Thank you for this. I have no interest in rounding times, but the data URL technique in your shortcut just solved a problem I’ve been chewing on all day. Making a <script> to run through Safari is a great way to get JavaScript solutions that rely on large external libraries.

1 Like