Gathering detailed insights and metrics for timezone-mock
Gathering detailed insights and metrics for timezone-mock
Gathering detailed insights and metrics for timezone-mock
Gathering detailed insights and metrics for timezone-mock
moment-timezone
Parse and display moments in any timezone.
ebx-timezone-mock
A JavaScript library which mocks the Date object and lets you specify a local date/time/timezone in order to enable consistent testing of date/time-related code independently of your local system time settings.
jest-mock
**Note:** More details on user side API can be found in [Jest documentation](https://jestjs.io/docs/mock-function-api).
time-zone
Pretty time zone: `+2` or `-9:30`
npm install timezone-mock
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
107 Stars
126 Commits
35 Forks
2 Watching
4 Branches
22 Contributors
Updated on 19 Sept 2024
JavaScript (98.06%)
Dockerfile (1.94%)
Cumulative downloads
Total Downloads
Last day
-7.6%
83,463
Compared to previous day
Last week
-1%
401,065
Compared to previous week
Last month
19.2%
1,731,912
Compared to previous month
Last year
-0.9%
16,262,886
Compared to previous year
1
A JavaScript library to mock the local timezone.
This module is useful for testing that code works correctly when run in other timezones, especially those which have Daylight Saving Time if the timezone of your test system does not.
When register
is called, it replaces the global Date constructor with
a mocked Date object which behaves as if it is in the specified timezone.
Note: Future timezone transitions are likely to change due to laws, etc. Make
sure to always test using specific dates in the past. The timezone data used by
timezone-mock 1.0.4+
should be up accurate for all times through the end of 2018.
Note: Node v8.0.0 changed how the string "YYYY-MM-DDTHH:MM:SS" is interpreted. It was previously interpreted as a UTC date, but now is a local date. If your code is using dates of this format, results will be inconsistent. timezone-mock treats them as a local date, so that it behaves consistently with new versions of Node, but that means if you run the tests in here on old versions of node, or use the mock on old versions of node, the tests may not be accurate (just for parsing dates in the aforementioned format).
1var assert = require('assert'); 2var timezone_mock = require('timezone-mock'); 3 4function buggyCode() { 5 // This function is potentially a bug since it's interpreting a string in 6 // the local timezone, which will behave differently depending on which 7 // system it is ran on. 8 return new Date('2015-01-01 12:00:00').getTime(); 9} 10var result_local = buggyCode(); 11timezone_mock.register('US/Pacific'); 12var result_pacific = buggyCode(); 13timezone_mock.register('US/Eastern'); 14var result_eastern = buggyCode(); 15assert.equal(result_local, result_pacific); // Might fail 16assert.equal(result_pacific, result_eastern); // Definitely fails 17
timezone_mock.register(timezone)
- Replace the global Date object with a mocked one for
the specified timezone. Defaults to 'US/Pacific' if no timezone is specified.timezone_mock.unregister()
- Return to normal Date object behaviortimezone_mock._Date
- access to the original Date object for testing. Available after register
is called, and it's the Date
object of the provided global/window context.timezone_mock.options({ fallbackFn })
- Option to provide a fallback function when timezone-mock
fails to parse.Currently supported timezones are:
I found that testing on these were enough to ensure code worked in all timezones (important factor is to test on a timezone with Daylight Saving Time if your local timezone does not). Brazil/East has the unique characteristic of having the DST transition happen right at midnight, so code that sets a Date object to midnight on a particular day and then does operations on that Date object is especially vulnerable in that timezone. Europe/London is included as a timezone that is a positive offset from UTC, and Australia/Adelaide as one that has a large positive and non-integral offset (+9.5/+10.5).
Note: the Etc/GMT
timezones work the opposite of how one might expect, the Etc/GMT+5
timezone is equivalent to US Eastern Standard Time (UTC-5).
Currently supported GMT offsets are:
GMT offsets can be used to test if UTC times fall on particular local calendar days.
Note: Etc/GMT+0
, Etc/GMT
, and Etc/GMT-0
all represent the same offset and are
equivalent to the UTC
time zone.
The behavior of toLocaleString()
has changed in v1.3.0 to more closely match Node's
behavior, but only works on Node v14+. Use v1.2.2 for the old behavior. See note below.
Most Date member functions are supported except for some conversions to locale-specific date strings. These are mocked in a way that may only work reliably on Node v14+. Note that using locale-specific formatting is often device- and browser-dependant, so any use of these in tests may indicate a potential bug.
With non-DST timezones, it should behave identically to the native Javascript Date object. With DST timezones, it may sometimes behave slightly differently when given an ambiguous date string (e.g. "2014-11-02 01:00:00" in "US/Pacific", is treated as 1AM PDT instead of 1AM PST - same clock time, utc timestamp off by an hour).
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 8/23 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
13 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More