npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

openinghours.js

v0.1.1

Published

Given an opening hours structure as defined by [schema.org/OpeningHoursSpecification](https://schema.org/OpeningHoursSpecification), and as used by [Google](https://developers.google.com/search/docs/data-types/local-business#business_hours), this can tell

Downloads

2

Readme

openinghours.js

Given an opening hours structure as defined by schema.org/OpeningHoursSpecification, and as used by Google, this can tell you if at a particular point in time the entity is open, and when it will close, or the reverse.

This is still a work on progress and might fail to return the right result in edge cases, but seems to handle most common cases, and comes with a test suite.

Install

$ yarn add openinghours.js
$ npm install --save openinghours.js

Documentation

The data structure

Define your opening hours using this data structure:

const rules = [
  // This applies to every day
  {
    opens: '08:00',
    closes: '18:00'
  },
  {
    // This applies to any monday or tuesday.
    dayOfWeek: ['monday', 'tuesday'],
    // 00:00 - 00:00 means closed the whole day.
    opens: '00:00',
    closes: '00:00',
  },
  {
    // This applies to any sunday in December 2019
    dayOfWeek: ['sunday'],
    validFrom: '2019-12-01',
    validThrough: '2019-12-21',
    // 00:00 - 23:59 means open the whole day.
    opens: '00:00',
    closes: '23:59',
  },
];

The main query function

Query the state at a given point in time:

import {getCurrentState} from 'openinghours.js';

getCurrentState(
  rules,
  {
    date: new Date()
  }
);

The return value is an object such as:

const result = {
  isOpen: true,
  closesAt: new Date("2019-12-05T14:00:00.000Z")
}
const result = {
  isOpen: false,
  opensAt: new Date("2019-12-05T14:00:00.000Z")
}

A note on timezones

The library fully supports timezones.

To declare which timezone the open/close time rules refer to, you can pass a rulesTimezone option:

getCurrentState(
  [
    {
      opens: '08:00',
      closes: '18:00'
    }
  ],
  {
    date: new Date("2019-12-05T07:30:00"),
    rulesTimezone: 'Europe/Berlin'
  }
);

If you run the above on a GMT machine (so the time queried will be 07:30 GMT) while Europe/Berlin is GMT+1, then the result will be "is open", because the shop opened at 7am in GMT time (and at 8am in Europe/Berlin time).

It is strongly recommended that you pass this option, as the results you get will otherwise depend on the host machine timezone you run the query on: If not given, we assume 06:00 means 6am in the local timezone. If your code runs in a browser on a visitor's machine in America/New_York at 6am New York time, we'll assume the entity is just opening, which will not be true if it is situated in a different timezone.

For the date you want to query, you can pass a luxon.DateTime object instead of a native Date:

import {getCurrentState} from 'openinghours.js';
import {DateTime} from 'luxon';

getCurrentState(
  rules,
  {
    date: DateTime.fromObject({ hour: 10, minute: 26, second: 6, year: 2019, month: 5, day: 25, zone: 'America/New_York' }),
    rulesTimezone: 'Europe/Berlin'
  }
);

Other libraries

I could not find another library dealing with opening hours that does the following: