I am working on an application with train schedules, where the first train leaves at 0400 while the last train leaves at 0200. The users of this application therefore deal with days starting at 0300 and ending at 0300. In other words, when they say "The Wednesday-train at 0200" they really mean "The train that leaves on Thursday at 0200".
Our application needs to store all of the trains leaving on (for example) Wednesday, which means it should not include the trains that leave before 0300 but it should include the trains that leave the next day until 0300.
How would I represent this in application without going mad? And how should this be stored in a database for easy querying?
I would store the actual date/time value. Then for querying, to search for "anything on Wednesday" you'd go from Wednesday 0400 (inclusive) to Thursday 0400 (exclusive).
In terms of display, you'd probably be best taking the date, and subtracting a day if the time is earlier than some cutoff:
private static readonly LocalTime CutOff = new LocalTime(4, 0, 0);
...
LocalDate date = dateTime.Date;
if (dateTime.TimeOfDay < CutOff)
{
date = date.PlusDays(-1);
}
var dayOfWeek = date.IsoDayOfWeek;
I would try to avoid using the date on its own as far as possible, to avoid going mad. Any date/time will be unambiguous.
See more on this question at Stackoverflow