Use date and timestamp classes for calculations etc.

See legacy/trac#24260 (moved) comment 14 for details.

Also try finding a way to add tests here.