存储全天的开放时间(24 小时)
我正在编写一个程序,将商店的营业时间存储在数据库 time
字段中,并且想知道如果我想表达商店在特定日期 24 小时营业,那么正确的时间范围是什么小时。是0:00-23:59吗?还是0:01-0:00?两种形式都有一分钟的间隙,商店关门。是0:00-0:00吗? 一天的开始时间是否存在因地区而异的差异?
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我见过一些用例,商店在早上和晚上营业,但在下午营业。
无论商店是否有多个班次,我都会存储一个时间间隔为 30 分钟的时间表(您也可以执行 1 小时间隔或 15 分钟间隔),并使用它来标记商店营业或关闭的任何时间。
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.
该条件本身并不真正适合您的存储概念,但由于您正在完成的任务有许多无效值(结束<开始,结束==开始),因此指定其中任何一个都没有问题。因此,例如,使用 0:00 - 0:00 并在心里决定结束 > >开始,因此这必须代表一整天。 0:00 - 23:59 有合理的含义,所以我会避免使用这个。
另一种选择是使用开始时间+长度而不是两次。我想我会优先选择这个。
我认为一天的开始是无关紧要的;如果您保持数据存储和表示层独立,那么您可以在表示层中进行本地化(即打印时间时),并且数据存储不会关心一天的开始时间(因此使用 UTC 或存储时区)。
实施时需要注意的一点是:您缺乏任何方法让商店每天营业两次(例如午餐时间)或更长时间。
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.
根据您的数据库引擎,这可能需要几秒钟,这样您的商店就会在 00:00:00 到 23:59:59 之间营业。那么 1 秒的重叠也不算太糟糕。
处理这个问题时,最重要的是要记住,有些商店会 24 小时营业,但会在特定时间开始营业。例如早上 6 点,到第二天早上 6 点。
存储它的一种方法是先有开放时间,然后是某种时间跨度,可能只是开放时间后的秒数。这样,你就可以24小时拥有84600。
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.