mock-browser-timezone
v1.0.2
Published
A Javascript library to mock the browser's timezone using ECMAScript Internationalization API.
Downloads
134
Maintainers
Readme
mock-browser-timezone
A Javascript library to mock the browser's timezone using ECMAScript Internationalization API.
What's different?
Many modern browsers use the Intl API to get the desired IANA timezone string which depicts the current browser timezone.
This library is specifically designed to mock the browser timezone, assisting to test the relevant code which uses the Intl
API.
Installation
Using npm:
npm install mock-browser-timezone --save-dev
Using yarn:
yarn add mock-browser-timezone -D
API
Usage
For a better implementation of mock-browser-timezone
, have a look at mock-browser-timezone.js file.
const {
registerTimezone,
registerAbbreviatedTimezone,
reset,
} = require('mock-browser-timezone');
// Mocking the timezone using IANA timezone string.
registerTimezone('Europe/Paris');
reset(); // Default back to the orginal value.
new Intl.DateTimeFormat().resolvedOptions().timeZone; // Europe/Paris
registerAbbreviatedTimezone('CET');
new Intl.DateTimeFormat([], {
timeZoneName: 'short',
})
.formatToParts(date)
.find((locale) => locale.type === 'timeZoneName').value; // CET
reset(); // Default back to the orginal abbreviated timezone value.
Supported Timezones
A list of supported IANA formatted timezones as well as abbreviated timezones can be found at timezones.json.