Mongodb 处理营业时间以及如何查找某个地方是否营业

cel*_*osa 5 mongodb

我正在努力寻找处理餐厅营业时间的最佳方法。复杂性是由于以下事实:

  • 场所可能在同一天内多次开放(例如7:00-14:30、18:30-22:00)
  • 有时关闭时间会超过午夜(例如19:00-2:00)
  • 并非每天都有相同的时间

因此,一个好的方法似乎是记录一周中每一天的开放/关闭时间数组,格式为:seconds since the beginning of the week。这是一个例子

{
    "address": "street 1, city, postcode",
    "hours" : {
        "mon" : [
            {
                "opened" : 25200, // 07:00
                "closed" : 52200 // 14:30
            }
        ],
        "tue" : [
            {
                "opened" : 111600, // 07:00
                "closed" : 138600 // 14:30
            },
            {
                "opened" : 153000, // 18:30
                "closed" : 180000 // 02:00 (of the day after)
            }
        ],
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

(在模型中,我有一个静态变量,它使用我编写的一小段代码来渲染每个字段的自动表单下拉列表)

在我看来,整周的开放时间是这样的:

Monday: {{#each hours.mon}}
    {{#if @index ">" 0}}<br />{{/if}} // this just breaks the second array item in a new line
    {{getOpeningTime opened}} - {{getOpeningTime closed}}
{{/each}}
<br/>
Tuesday: ...
Run Code Online (Sandbox Code Playgroud)

其中getOpeningTime有一个模板助手,它使用以下命令以 HH:mm 格式返回时间moment.js

getOpeningTime: function(seconds) {
    return moment().startOf('day').seconds(seconds).format('HH:mm');
}
Run Code Online (Sandbox Code Playgroud)

到目前为止一切都很好 - 尽管我仍然不确定这是否是正确的方法

现在,我试图注意到用户在查看页面时餐厅是否营业。这是我到目前为止所写的:

openedOrClosed: function() {
    now = moment();
    ddd = now.format('ddd').toLowerCase(); // I now I will look into the field hours.day - the first three letters all lowercase

    day = now.day();
    day = (day === 0 ? 7 : day); // This is just a conversion I do because I want to have Sunday at the end of the week

    seconds = now.clone().diff(now.clone().startOf('day'), 'seconds') + (86400 * day) - 86400; // Minutes since midnight in Momentjs (http://stackoverflow.com/a/25390707/1435476) plus the sum of all the other days of the week to work out the global seconds since the beginning of the week

    query = {};

    query._id = this._id;

    // here I have available all I need. but I'm not sure how to build the query

    if (Restaurants.find(query).count() > 0) {
        return "Opened";
    } else {
        return "Closed";
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 2

首先:处理日期和时间是一个挑战:)。

我认为你的“自本周开始以来的秒数”格式是没有必要的。Momentjs 可以将“现在”解析为天和小时(以及更多)。

您还应该考虑圣诞节等例外情况。除此之外,餐厅每天可以营业多次(例如朝九晚二、五到十一点)。

我会去做这样的事情:

restaurantname:{
     //special days array of objects
      specialdays: 
           [{date: date, 
             openinghours: 
            //again array of objects
                  [{start: sometime, end: sometime},
                   {start: somtime, end: sometime},
                   //etc
                   ]
             }]
            },

        normaldays: {mon: //openinghours like above, again array of objects with start and end, tue, wed etc}
Run Code Online (Sandbox Code Playgroud)

之后你可以先检查一下:今天是特殊的一天吗?如果是=>检查当前时间是否在开始之后和结束之前(循环对象数组)。否则,如果不是特殊的一天,请检查特定的“正常日期”并执行相同的操作。

我认为切换是理想的选择。你可以先从具体到一般,让你的情况先从具体情况入手。

这只是一个大纲,我知道这很有挑战性:)。

希望这可以帮助...