Aggregating by time should respect the natural boundaries of the argument.

Fix #387
Some refactoring
Merge branch 'groupby-boundaries'