From f9decef38781f24de3b0f80c109200b37f1da36b Mon Sep 17 00:00:00 2001 From: Marketionist Date: Mon, 23 Sep 2024 00:24:23 -0400 Subject: [PATCH 1/3] Extend generateDateTimePlusSeconds to accept optional initialDate --- utils/date-time.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/utils/date-time.js b/utils/date-time.js index a9aaffe..064caa6 100644 --- a/utils/date-time.js +++ b/utils/date-time.js @@ -89,15 +89,20 @@ let dateTime = { return process.env.DATETIME_PLUS_MINUTES; }, + /* eslint-disable max-len */ /** - * Generates current date and time plus number of seconds (for example: '2024-03-14T00:14:26'). - * @param {Number} seconds number of seconds that will be added to current date and time. + * Generates current (if no initial date is provided) date and time plus number of seconds (for example: '2024-03-14T00:14:26'). + * @param {Number} seconds number of seconds that will be added to current (if no initial date is provided) date and time. + * @param {String=} initialDate string with initial date and time that you want to add a number of seconds to (for example: '2024-03-14T00:14:25'). * @returns {String} string with date and time plus number of seconds (for example: '2024-03-14T00:14:26'). */ - generateDateTimePlusSeconds: function (seconds) { + generateDateTimePlusSeconds: function (seconds, initialDate) { + /* eslint-enable max-len */ + const date = initialDate ? initialDate : Date.now(); + process.env.DATETIME_PLUS_SECONDS = new Date( new Date( - Date.now() + (seconds * _millisecondsInSecond) + new Date(date).getTime() + (seconds * _millisecondsInSecond) ).toString().split('GMT')[0] + ' UTC' ).toISOString().split('.')[0]; From 020255859c5331b919665e7916f281e3a26327b6 Mon Sep 17 00:00:00 2001 From: Marketionist Date: Mon, 23 Sep 2024 00:25:04 -0400 Subject: [PATCH 2/3] Add test for generateDateTimePlusSeconds with initialDate --- tests/test1.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/test1.js b/tests/test1.js index a85136f..536478f 100644 --- a/tests/test1.js +++ b/tests/test1.js @@ -91,11 +91,19 @@ console.log( const currentDateTimePlusSecond = dateTime.generateDateTimePlusSeconds(1); console.log( - 'process.env.DATETIME_PLUS_SECONDS: ' + + 'currentDateTimePlusSecond in process.env.DATETIME_PLUS_SECONDS: ' + `${currentDateTimePlusSecond === process.env.DATETIME_PLUS_SECONDS} ` + process.env.DATETIME_PLUS_SECONDS ); +const pastDateTimePlusSecond = dateTime.generateDateTimePlusSeconds(1, '2024-03-14T00:14:25'); + +console.log( + 'pastDateTimePlusSecond in process.env.DATETIME_PLUS_SECONDS: ' + + `${pastDateTimePlusSecond === process.env.DATETIME_PLUS_SECONDS} ` + + process.env.DATETIME_PLUS_SECONDS +); + if ( (Date.parse(currentDateTimePlusDay) > Date.parse(currentDateTimePlusHour)) && (Date.parse(currentDateTimePlusHour) > Date.parse(currentDateTimePlusMinute)) && @@ -110,7 +118,8 @@ if ( if ( (Date.parse(currentDateTime) > Date.parse(pastDateTimePlusDay)) && (Date.parse(pastDateTimePlusDay) > Date.parse(pastDateTimePlusHour)) && - (Date.parse(pastDateTimePlusHour) > Date.parse(pastDateTimePlusMinute)) + (Date.parse(pastDateTimePlusHour) > Date.parse(pastDateTimePlusMinute)) && + (Date.parse(pastDateTimePlusMinute) > Date.parse(pastDateTimePlusSecond)) ) { console.log('pastDateTimePlus Day/Hour/Minute/Second looks ok!'); } else { @@ -128,7 +137,7 @@ console.log( if ( Date.parse(currentDateTimeMinusHour) < Date.parse(currentDateTime) ) { - console.log('dateTime minus looks ok!'); + console.log('currentDateTimeMinus Hour looks ok!'); } else { throw new Error('currentDateTimeMinus Hour should be less than currentDateTime'); } From c726b6c0c5776f892862a83670b423b1077bf0a8 Mon Sep 17 00:00:00 2001 From: Marketionist Date: Mon, 23 Sep 2024 00:25:43 -0400 Subject: [PATCH 3/3] Add example for generateDateTimePlusSeconds with initialDate --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e9ddc48..0214e3d 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ const { dateTime } = require('js-automation-tools'); const pastDateTimePlusDay = dateTime.generateDateTimePlusDays(1, '2024-03-14T00:14:25'); // '2024-03-15T00:14:25' const pastDateTimePlusHour = dateTime.generateDateTimePlusHours(1, '2024-03-14T00:14:25'); // '2024-03-14T01:14:25' const pastDateTimePlusMinute = dateTime.generateDateTimePlusMinutes(1, '2024-03-14T00:14:25'); // '2024-03-14T00:15:25' +const pastDateTimePlusSecond = dateTime.generateDateTimePlusSeconds(1, '2024-03-14T00:14:25'); // '2024-03-14T00:14:26' ``` It will also write generated date and time to a global environment variable `process.env.DATETIME` and `process.env.DATETIME_PLUS_DAYS`,