Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend generateDateTimePlusSeconds to accept optional initialDate #63

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
15 changes: 12 additions & 3 deletions tests/test1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) &&
Expand All @@ -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 {
Expand All @@ -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');
}
Expand Down
13 changes: 9 additions & 4 deletions utils/date-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
Loading