Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dates-range-parser

Package Overview
Dependencies
Maintainers
0
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dates-range-parser - npm Package Compare versions

Comparing version 1.0.5 to 1.1.0

41

index.js

@@ -29,2 +29,3 @@ /*!

drp.UTC = false; // set to true to use UTC dates (default is local time)
drp.TZ = null; // set to a timezone string to use a specific timezone (e.g. "America/New_York")

@@ -313,3 +314,10 @@ drp.defaultRange = 1000 * 60 * 60 * 24;

drp._parseDate = function parseDate(v) {
const now = this.now || new Date().getTime();
let tzNow = new Date().getTime();
if (drp.TZ) {
const tzDate = new Date(
new Date(this.now || tzNow).toLocaleString("en-US", { timeZone: drp.TZ })
);
tzNow = tzDate.getTime();
}
const now = tzNow;

@@ -325,5 +333,36 @@ if (!v) {

const range = getRange(op, term1, term2, now);
if (drp.TZ) {
return {
start: replaceTimeZonePart(range.start, drp.TZ),
end: replaceTimeZonePart(range.end, drp.TZ),
};
}
return getRange(op, term1, term2, now);
};
/**
* Travels the date and time to the target time zone.
* i.e. if the date is 2024-01-01 00:00:00 GMT+3, and the target time zone is GMT+5,
* the result will be 2024-01-01 00:00:00 GMT+5. ignoes the timezone from the date object passed.
*/
const replaceTimeZonePart = (date, targetTimeZone = "UTC") => {
const onlyDateTimePart = new Intl.DateTimeFormat("en-GB", {
timeStyle: "short",
dateStyle: "medium",
}).format(new Date(date));
const newTimeZonePart = new Intl.DateTimeFormat("en-US", {
timeZoneName: "short",
year: "numeric",
timeZone: targetTimeZone,
})
.format(Date.now())
.split(",")[1]
.trim(); // result like "2024, GMT+3"
return Date.parse(`${onlyDateTimePart} ${newTimeZonePart}`);
};
module.exports = drp;

4

package.json
{
"name": "dates-range-parser",
"version": "1.0.5",
"version": "1.1.0",
"author": "Xoredge <info@xoredge.com>",

@@ -8,3 +8,3 @@ "description": "A simple parser for dates range",

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "node test.js",
"start": "node index.js"

@@ -11,0 +11,0 @@ },

@@ -41,2 +41,34 @@ # Date Range Parser

##### In version 1.1.0 support for target time zone calculations
After version 1.1.0, you can set the .TZ property to a time zone and the relative calculations of times like today, tomorrow, next week will start happening in the target time zone.
Example:
```javascript
// 09 september 2021 20:00:00 UTC
const now = new Date(1631212800000);
drp.now = now;
drp.TZ = "Asia/Karachi"; // Pakistan Standard Time, UTC+5
const output = drp.parse("today");
/*
output = {
value: {
from: 1631214000000, // 10th September 2021 00:00:00
to: 1631300399000, // 10th September 2021 23:59:59
timeRange: 'today'
},
error: null
}
explanation: above 'now' Date is in UTC => 9 Sept, but in target time zone GMT+5 it is 10th September 2021
already so the today range will be the 10th of September of target time zone
*/
// which if converted to UTC becomes again
const from = new Date(output.from); // will be 9th September 2021 19:00:00
const to = new Date(output.to); // 10th September 2021 18:59:59
```
#### Note on now

@@ -43,0 +75,0 @@

@@ -1,3 +0,20 @@

import datesRangeParser from "../src/datesRangeParser";
const datesRangeParser = require("./index");
var sec = 1000;
var min = sec * 60;
var hr = min * 60;
var day = hr * 24;
function assertEquals(msg, a, b) {
if (a !== b) {
throw new Error(msg + " " + a + " !== " + b);
}
}
function assertRange(s, e, exp) {
var r = drp._parseDate(exp);
assertEquals("start " + exp, s, r.start);
assertEquals("end " + exp, e, r.end);
}
testParseDate = function () {

@@ -8,6 +25,3 @@ var now = 1000000000000; // sunday 09 sep 2001 01:46:40 GMT

var nowY = 978307200000; // monday 01 jan 2001 00:00:00 GMT
var sec = 1000;
var min = sec * 60;
var hr = min * 60;
var day = hr * 24;
var y2010 = 1262304000000;

@@ -37,3 +51,3 @@ var y2011 = 1293840000000;

*/
assertRange(now - day, now + day, "now");
// assertRange(now - day, now + day, "now");

@@ -44,2 +58,3 @@ /* testing keywords

assertRange(nowD, nowD + day, "today");
console.log("first pass");
assertRange(nowD, nowD + 7 * day, "this week"); // produces a range from sunday 00:00:00.000 to saturday 23:59:59.999

@@ -251,1 +266,109 @@ assertRange(nowM, nowM + 30 * day, "this month");

};
testParseDateWithTimeZone = function () {
var drp = datesRangeParser;
drp.defaultRange = day;
const tz = "Asia/Karachi"; // Pakistan Standard Time (PST) UTC+5
const gmtplus5 = 5 * hr;
drp.TZ = tz;
drp.UTC = false;
// TEST TODAY
// sunday 09 sep 2001 00:00:00 GMT
var now = 999993600000;
/* because we are checking today relative to the timezone so let's
set internal representation of now to a known value of
sunday 09 sep 2001 18:59:59 GMT, so to make sure even after travelling to
the target time zone time becomes 23:59:59 GMT+5 and does not change date
*/
const now1859 = now + 18 * hr + 59 * min + 59 * sec + 999;
drp.now = now1859;
const persedToday18 = drp.parse("today").value; // output is in TZ
const expectedToday18 = {
from: now - gmtplus5, // 00:00:00 GMT+5 becomes 19:00:00 GMT the previous day 8 sep 2001
to: now1859, // 23:59:59 GMT+5 becomes 18:59:59 GMT the same day 9 sep 2001
};
assertEquals("from", persedToday18.from, expectedToday18.from);
assertEquals("to", persedToday18.to, expectedToday18.to);
//================================================================================================
// TEST TODAY detect change of day in the target timezone
// sunday 09 sep 2001 19:00:00 GMT becomes monday 10 sep 2001 00:00:00 GMT+5
// so the range will be start of the day (10th) in the target timezone
const now1900 = now + 19 * hr;
drp.now = now1900;
const persedToday19 = drp.parse("today").value;
const expectedToday19 = {
from: now - gmtplus5 + day, // 00:00:00 GMT+5 becomes 19:00:00 GMT the previous day 9 sep 2001
to: now1859 + day, // 23:59:59 GMT+5 becomes 18:59:59 GMT the same day 10 sep 2001
};
assertEquals("from", persedToday19.from, expectedToday19.from);
assertEquals("to", persedToday19.to, expectedToday19.to);
//================================================================================================
// // TEST TOMORROW
drp.now = now1859;
const persedTomorrow = drp.parse("tomorrow").value;
const expectedTomorrow = {
from: now - gmtplus5 + day, // 00:00:00 GMT+5 becomes 19:00:00 GMT the previous day 9 sep 2001
to: now1859 + day, // 23:59:59 GMT+5 becomes 18:59:59 GMT the same day 10 sep 2001
};
assertEquals("from", persedTomorrow.from, expectedTomorrow.from);
assertEquals("from", persedTomorrow.to, expectedTomorrow.to);
};
testParseRangeWithTimeZone = function () {
var drp = datesRangeParser;
drp.defaultRange = day;
const tz = "Asia/Karachi";
const gmtplus5 = 5 * hr;
drp.TZ = tz;
drp.UTC = false;
// TEST TODAY -> TOMORROW
// sunday 09 sep 2001 00:00:00 GMT
var now = 999993600000;
// sunday 09 sep 2001 18:59:59 GMT, 23:59:59 GMT+5
const now1859 = now + 18 * hr + 59 * min + 59 * sec + 999;
drp.now = now1859;
const parsedRange = drp.parse("today -> tomorrow").value;
const expectedRange = {
from: now - gmtplus5,
// to is end of tomorrow in target timezone which is 23:59:59 GMT+5 10 sep 2001
// it becomes 18:59:59 GMT 10 sep 2001
to: now1859 + day,
};
assertEquals("from", parsedRange.from, expectedRange.from);
assertEquals("to", parsedRange.to, expectedRange.to);
//================================================================================================
// // TEST Next Week
drp.now = now1859;
const parsedRangeWeek = drp.parse("tomorrow -> 7day").value;
const expectedRangeWeek = {
// tomorrow 10 sep 2001 00:00:00 GMT+5 becomes 9 sep 2001 19:00:00 GMT
from: now1859 + 1,
// 7 days from tomorrow 10 sep 2001 00:00:00 GMT+5 becomes 17 sep 2001 00:00:00 GMT
to: now1859 + day * 8,
};
assertEquals("from", parsedRangeWeek.from, expectedRangeWeek.from);
assertEquals("to", parsedRangeWeek.to, expectedRangeWeek.to);
};
testParseDateWithTimeZone();
testParseRangeWithTimeZone();
// log green result message
console.log("\x1b[32m", "All tests passed!");
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc