Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
timezone-mock
Advanced tools
The 'timezone-mock' npm package allows developers to mock the system timezone in their Node.js applications. This is particularly useful for testing code that is sensitive to timezones without having to change the system timezone.
Mocking the System Timezone
This feature allows you to set a specific timezone for your Node.js application. The code sample demonstrates how to register the 'US/Pacific' timezone and then log the current date and time in that timezone.
const timezoneMock = require('timezone-mock');
timezoneMock.register('US/Pacific');
console.log(new Date().toString()); // Outputs date in US/Pacific timezone
Unregistering the Mocked Timezone
This feature allows you to unregister the mocked timezone and revert back to the system's default timezone. The code sample demonstrates how to unregister the 'US/Pacific' timezone and log the current date and time in the system's default timezone.
const timezoneMock = require('timezone-mock');
timezoneMock.register('US/Pacific');
console.log(new Date().toString()); // Outputs date in US/Pacific timezone
timezoneMock.unregister();
console.log(new Date().toString()); // Outputs date in the system's default timezone
The 'mockdate' package allows you to mock the current date and time in your Node.js applications. Unlike 'timezone-mock', which focuses on timezones, 'mockdate' is more about setting a specific date and time for testing purposes.
The 'sinon' package is a comprehensive library for creating spies, stubs, and mocks in JavaScript. While it is not specifically focused on timezones, it can be used to mock dates and times, among other functionalities. It is more versatile but also more complex compared to 'timezone-mock'.
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: 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).
var assert = require('assert');
var timezone_mock = require('timezone-mock');
function buggyCode() {
// This function is potentially a bug since it's interpreting a string in
// the local timezone, which will behave differently depending on which
// system it is ran on.
return new Date('2015-01-01 12:00:00').getTime();
}
var result_local = buggyCode();
timezone_mock.register('US/Pacific');
var result_pacific = buggyCode();
timezone_mock.register('US/Eastern');
var result_eastern = buggyCode();
assert.equal(result_local, result_pacific); // Might fail
assert.equal(result_pacific, result_eastern); // Definitely fails
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 testingCurrently supported timezones are:
I found that testing on these three were enough to ensure code worked in all timezones (import 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.
Most Date member functions are supported except for some conversions to locale-specific date strings.
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).
FAQs
A JavaScript library to mock the local timezone.
The npm package timezone-mock receives a total of 104,221 weekly downloads. As such, timezone-mock popularity was classified as popular.
We found that timezone-mock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.