8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Google Places API のデータで営業中か判定するアルゴリズム

Last updated at Posted at 2020-03-27

Places API を使えば、Google Map に登録されているデータを取得できます。
Places API は取得したいデータに応じて API が複数ありますが、Place Details で fieldsopening_hours を指定すれば、そのお店の開店・閉店時間を取得できます。

営業時間が次のお店なら、

  • 日: 11:30 AM – 2:00 PM, 5:00 PM – 3:00 AM
  • 月: 11:30 AM – 2:00 PM, 5:00 PM – 3:00 AM
  • 火: 11:30 AM – 2:00 PM, 5:00 PM – 3:00 AM
  • 水: 閉店
  • 木: 11:30 AM – 2:00 PM, 5:00 PM – 3:00 AM
  • 金: 11:30 AM – 2:00 PM, 5:00 PM – 3:00 AM
  • 土: 11:30 AM – 2:00 PM, 5:00 PM – 3:00 AM

API で取得できるデータは次の通りです。なかなか複雑ですね!

{
  "periods": [
    {
      "close": {
        "day": 0,
        "time": "1400"
      },
      "open": {
        "day": 0,
        "time": "1130"
      }
    },
    {
      "close": {
        "day": 1,
        "time": "0300"
      },
      "open": {
        "day": 0,
        "time": "1700"
      }
    },
    {
      "close": {
        "day": 1,
        "time": "1400"
      },
      "open": {
        "day": 1,
        "time": "1130"
      }
    },
    {
      "close": {
        "day": 2,
        "time": "0300"
      },
      "open": {
        "day": 1,
        "time": "1700"
      }
    },
    {
      "close": {
        "day": 2,
        "time": "1400"
      },
      "open": {
        "day": 2,
        "time": "1130"
      }
    },
    {
      "close": {
        "day": 3,
        "time": "0300"
      },
      "open": {
        "day": 2,
        "time": "1700"
      }
    },
    {
      "close": {
        "day": 4,
        "time": "1400"
      },
      "open": {
        "day": 4,
        "time": "1130"
      }
    },
    {
      "close": {
        "day": 5,
        "time": "0300"
      },
      "open": {
        "day": 4,
        "time": "1700"
      }
    },
    {
      "close": {
        "day": 5,
        "time": "1400"
      },
      "open": {
        "day": 5,
        "time": "1130"
      }
    },
    {
      "close": {
        "day": 6,
        "time": "0300"
      },
      "open": {
        "day": 5,
        "time": "1700"
      }
    },
    {
      "close": {
        "day": 6,
        "time": "1400"
      },
      "open": {
        "day": 6,
        "time": "1130"
      }
    },
    {
      "close": {
        "day": 0,
        "time": "0300"
      },
      "open": {
        "day": 6,
        "time": "1700"
      }
    }
  ]
}

このデータを使って、店舗が営業中かどうかを判定するアルゴリズムは次のようになります。


/**
 * 営業中か判定する
 * @param {Date} d 日付オブジェクト
 * @param {object} openingHours 営業時間のデータ
 * @return {boolean} 営業中なら true、閉店なら false
 */
const isOpen = (d, openingHours) => {
  const day = d.getDay();
  const h = String(d.getHours()).padStart(2, '0');
  const m = String(d.getMinutes()).padStart(2, '0');
  const time = h + m;
  return openingHours.periods.filter(p => p.open.day === day || p.close.day === day).some(p => {
    const s = p.open.time;
    const t = p.close.time;
    if (p.open.day === p.close.day) return s <= time && time <= t;
    return (s <= time && time <= '2400') || ('0000' <= time && time <= t);
  });
};

コーディング例を https://codepen.io/takatama/pen/vYOvEQP に置きました。

8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?