Fixes to date handling

Please review before merging - some of the stuff overlaps with other teams' code, and may not be necessary.