Working with days which don't start at midnight in NodaTime

故事扮演 提交于 2019-12-14 03:52:18

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/20053150/working-with-days-which-dont-start-at-midnight-in-nodatime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!