Use date and timestamp classes for calculations etc.

See #24260 (moved) comment 14 for details.

Also try finding a way to add tests here.