Skip to content

Commit

Permalink
feat: remove redundant string support in utcOffset param
Browse files Browse the repository at this point in the history
use timeZone param instead
  • Loading branch information
sheerlox committed Sep 23, 2023
1 parent a3fa57d commit edbca18
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 56 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ Parameter Based
- `onTick` - [REQUIRED] - The function to fire at the specified time. If an `onComplete` callback was provided, `onTick` will receive it as an argument. `onTick` may call `onComplete` when it has finished its work.
- `onComplete` - [OPTIONAL] - A function that will fire when the job is stopped with `job.stop()`, and may also be called by `onTick` at the end of each run.
- `start` - [OPTIONAL] - Specifies whether to start the job just before exiting the constructor. By default this is set to false. If left at default you will need to call `job.start()` in order to start the job (assuming `job` is the variable you set the cronjob to). This does not immediately fire your `onTick` function, it just gives you more control over the behavior of your jobs.
- `timeZone` - [OPTIONAL] - Specify the time zone for the execution. This will modify the actual time relative to your time zone. If the time zone is invalid, an error is thrown. By default (if this is omitted) the local time zone will be used. You can check all time zones available at [Moment Timezone Website](http://momentjs.com/timezone/). Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
- `timeZone` - [OPTIONAL] - Specify the time zone for the execution. This will modify the actual time relative to your time zone. If the time zone is invalid, an error is thrown. By default (if this is omitted) the local time zone will be used. You can check the various time zones format accepted in the [Luxon documentation](https://github.com/moment/luxon/blob/master/docs/zones.md#specifying-a-zone). Note: This parameter supports minutes offsets, e.g. `UTC+5:30`. **Warning**: Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
- `context` - [OPTIONAL] - The context within which to execute the onTick method. This defaults to the cronjob itself allowing you to call `this.stop()`. However, if you change this you'll have access to the functions and values within your context object.
- `runOnInit` - [OPTIONAL] - This will immediately fire your `onTick` function as soon as the requisite initialization has happened. This option is set to `false` by default for backwards compatibility.
- `utcOffset` - [OPTIONAL] - This allows you to specify the offset of your time zone rather than using the `timeZone` param. This should be an integer representing the number of minutes offset or a string representing the time in HH:MM format (like `120` / `"+2:00"` for +2 hours or `-90` / `"-1:30"` for -1.5 hours). Note: minute offsets < 60 and >-60 will be treated as an offset in hours. This means a minute offset of `30` means an offset of +30 hours. Use string offset of `00:30` in this case. This behavior [is planned to be removed in V3](https://github.com/kelektiv/node-cron/pull/685#issuecomment-1676394391). Note 2: Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
- `utcOffset` - [OPTIONAL] - This allows you to specify the offset of your time zone rather than using the `timeZone` param. This should be an integer representing the number of minutes offset (like `120` for +2 hours or `-90` for -1.5 hours). **Warning**: Minutes offsets < 60 and >-60 will be treated as an offset in hours. This means a minute offset of `30` means an offset of +30 hours. Use the `timeZone` param in this case. This behavior [is planned to be removed in V3](https://github.com/kelektiv/node-cron/pull/685#issuecomment-1676417917). **Warning**: Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
- `unrefTimeout` - [OPTIONAL] - If you have code that keeps the event loop running and want to stop the node process when that finishes regardless of the state of your cronjob, you can do so making use of this parameter. This is off by default and cron will run as if it needs to control the event loop. For more information take a look at [timers#timers_timeout_unref](https://nodejs.org/api/timers.html#timers_timeout_unref) from the NodeJS docs.
- `start` - Runs your job.
- `stop` - Stops your job.
Expand Down
8 changes: 0 additions & 8 deletions lib/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,6 @@ function CronTime(luxon) {
}

if (typeof this.utcOffset !== 'undefined') {
if (typeof this.utcOffset === 'string') {
const parts = this.utcOffset.split(':');
const hours = parseInt(parts[0]);
const minutes = parseInt(parts[1] ? parts[1] : '0');
if (this.utcOffset[0] === '-') this.utcOffset = hours * 60 - minutes;
else this.utcOffset = hours * 60 + minutes;
}

let offsetHours =
this.utcOffset >= 60 || this.utcOffset <= -60
? this.utcOffset / 60
Expand Down
53 changes: 7 additions & 46 deletions tests/cron.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ describe('cron', () => {
* in order to avoid the minute offset being treated as hours (when `-60 < utcOffset < 60`) regardless of the local timezone,
* and the maximum possible offset being +14:00, we simply add 80 minutes to that offset.
* this implicit & undocumented behavior is planned to be removed in V3 anyway:
* https://github.com/kelektiv/node-cron/pull/685#issuecomment-1676394391
* https://github.com/kelektiv/node-cron/pull/685#issuecomment-1676417917
*/
const minutesOffset = 14 * 60 + 80; // 920

Expand Down Expand Up @@ -878,6 +878,12 @@ describe('cron', () => {
expect(callback).toHaveBeenCalledTimes(1);
});

/**
* this still works implicitly (without minute support) because the string conversion
* to integer removes everything after the colon, i.e. '(+/-)HH:mm' becomes (+/-)HH,
* but this is an undocumented behavior that will be removed in V3:
* https://github.com/kelektiv/node-cron/pull/685#issuecomment-1676394391
*/
it('should run a job using cron syntax with string format utcOffset', () => {
const clock = sinon.useFakeTimers();
const callback = jest.fn();
Expand Down Expand Up @@ -912,51 +918,6 @@ describe('cron', () => {
expect(callback).toHaveBeenCalledTimes(1);
});

it('should run a job using cron syntax with string format utcOffset with minute support', () => {
const clock = sinon.useFakeTimers();
const callback = jest.fn();

const luxon = require('luxon');
// Current time
const t = luxon.DateTime.local();
/**
* in order to avoid the minute offset being treated as hours (when `-60 < utcOffset < 60`) regardless of the local timezone,
* and the maximum possible offset being +14:00, we simply add 80 minutes to that offset.
* this implicit & undocumented behavior is planned to be removed in V3 anyway:
* https://github.com/kelektiv/node-cron/pull/685#issuecomment-1676394391
*/
const minutesOffset = 14 * 60 + 80; // 920

// UTC Offset decreased by minutesOffset (string format '(+/-)HH:mm')
const utcOffset = t.offset - minutesOffset;

const utcOffsetString = `${utcOffset > 0 ? '+' : '-'}${(
'0' + Math.floor(Math.abs(utcOffset) / 60)
).slice(-2)}:${('0' + (utcOffset % 60)).slice(-2)}`;

const job = new cron.CronJob(
t.second + ' ' + t.minute + ' ' + t.hour + ' * * *',
callback,
null,
true,
null,
null,
null,
utcOffsetString
);
// tick to 1s before minutesOffset
clock.tick(1000 * 60 * minutesOffset - 1000);
expect(callback).toHaveBeenCalledTimes(0);

// tick 1s
clock.tick(1000);
expect(callback).toHaveBeenCalledTimes(1);

clock.restore();

job.stop();
});

it('should run a job using cron syntax with number format utcOffset that is 0', () => {
const clock = sinon.useFakeTimers();
const callback = jest.fn();
Expand Down

0 comments on commit edbca18

Please sign in to comment.