Storing opening hours for a full day (24 hours)
I' writing a program that stores opening hours for a store in a database time
field and was wondering what the correct time range would be, if I want to express that on a开发者_StackOverflow particular day the store is open 24 hours. Is it 0:00-23:59 ? Or is it 0:01-0:00 ? Both formats have a one-minute gap where the store is closed. Is it 0:00-0:00?
Are there locale-dependent differences on when a day starts?
I have seen use cases where stores are open in the morning and in the evening, but no during afternoon hours.
Regardless of whether the store has multiple shifts or not, I would have a Hours table with 30 min intervals (you could also do 1 hour intervals or 15 min intervals) stored and use that to mark any time that the store is open or closed.
The condition itself doesn't really fit into your storage concept, but since you've got many invalid values for what you're accomplishing (end < start, end == start) there's nothing wrong with nominating any one of them. So, for instance, use 0:00 - 0:00 and mentally decide that end > start and therefore this must represent a full day. 0:00 - 23:59 has a legitimate meaning so I would avoid that one.
Another option would be to use start time + length rather than two times. I think I would choose this one for preference.
I think the start-of-day is irrelevant; if you keep your datastore and presentation layers independent then you can localise in your presentation layer (i.e. when printing the time) and the datastore won't care when the day starts (so use UTC or store the timezone).
A point to note on implementation: you lack any means for a shop to open twice a day (e.g. lunch hour) or more.
Depending on your database engine, that could go to seconds, so that you'd have a store open from 00:00:00 to 23:59:59. Then the 1 second overlap isn't too bad.
When dealing with this, the most important thing to remember is that some stores will open 24 hours, but operation starts at a certain time. For example, at 6 AM, to 6 AM the next day.
One way to store it would be to have the open time then a timespan of some kind, maybe just the number of seconds after open time. That way, you could have 84600 for 24 hours.
精彩评论