unit test and refactor database.py part 2

Add unit tests and refactor/fix/document issues related to the database.
(Part 2, because #619 grew too large.)

Related #484
Related #619

This can probably be removed, unless a feature is desired where time is displayed as hh:mm ap again.