title: Mongodb dealing with opening hours and how to find our if a place is opened or not
body: <p>I'm trying to find the best way to deal with restaurant opening hours. The complexity is due to the fact that:</p>
  
  <ul>
  <li>places may be opened multiple times during the same day (e.g. 7:00-14:30, 18:30-22:00)</li>
  <li>sometimes the closing time goes past the midnight (e.g. 19:00-2:00)</li>
  <li>not everyday shares the same timing</li>
  </ul>
  
  <p>A good approach seemed thus to register arrays of opened/closed times per each day of the week, in the format: <code>seconds since the beginning of the week</code>. Here's an example</p>
  
  <pre><code>{
      "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)
              }
          ],
          ...
      }
  }
  </code></pre>
  
  <p>(In the model I have a static variable rendered using <a href="https://jsfiddle.net/o6q2hg0q/1/" rel="noreferrer">a small piece of code I wrote</a> to do the autoform dropdowns per each field)</p>
  
  <p>In my view the opening hours for the whole week are rendered like so:</p>
  
  <pre><code>Monday: {{#each hours.mon}}
      {{#if @index "&gt;" 0}}&lt;br /&gt;{{/if}} // this just breaks the second array item in a new line
      {{getOpeningTime opened}} - {{getOpeningTime closed}}
  {{/each}}
  &lt;br/&gt;
  Tuesday: ...
  </code></pre>
  
  <p>Where <code>getOpeningTime</code> is a template helper that returns the time in a HH:mm format using <code>moment.js</code>: </p>
  
  <pre><code>getOpeningTime: function(seconds) {
      return moment().startOf('day').seconds(seconds).format('HH:mm');
  }
  </code></pre>
  
  <p>So far all good - although I'm still not sure whether that's the right approach</p>
  
  <p>Now, I'm trying to notice the user whether the restaurant is opened or not at the moment he/she's looking at the page. Here's what I wrote so far:</p>
  
  <pre><code>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() &gt; 0) {
          return "Opened";
      } else {
          return "Closed";
      }
  }
  </code></pre>