Skip to content

Commit

Permalink
performing tasks on relative time at timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
merencia committed Mar 15, 2018
1 parent 49b052a commit 69685f3
Show file tree
Hide file tree
Showing 8 changed files with 738 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/node-cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ module.exports = (function() {
* @param {boolean} immediateStart - whether to start the task immediately.
* @returns {ScheduledTask} update function.
*/
function createTask(expression, func, immediateStart) {
var task = new Task(expression, func);
function createTask(expression, func, immediateStart, timezone) {
var task = new Task(expression, func, timezone);

return new ScheduledTask(task, immediateStart);
}
Expand Down
7 changes: 6 additions & 1 deletion src/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var convertExpression = require('./convert-expression');
var validatePattern = require('./pattern-validation');
var tz = require('./timezone/timezone-converter');

module.exports = (function(){
function matchPattern(pattern, value){
Expand All @@ -13,6 +14,9 @@ module.exports = (function(){
}

function mustRun(task, date){
if(task.timezone)
date = tz.atTimezone(date, task.timezone);

var runInSecond = matchPattern(task.expressions[0], date.getSeconds());
var runOnMinute = matchPattern(task.expressions[1], date.getMinutes());
var runOnHour = matchPattern(task.expressions[2], date.getHours());
Expand All @@ -34,8 +38,9 @@ module.exports = (function(){
return runInSecond && runOnMinute && runOnHour && runOnDay && runOnMonth;
}

function Task(pattern, execution){
function Task(pattern, execution, timezone){
validatePattern(pattern);
this.timezone = timezone;
this.initialPattern = pattern.split(' ');
this.pattern = convertExpression(pattern);
this.execution = execution;
Expand Down
26 changes: 26 additions & 0 deletions src/timezone/timezone-converter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
var tz = require('./timezones');

module.exports = (function(){
/**
*
*/

function getUTCTime(date){
var currentOffset = date.getTimezoneOffset() * 60000;
if(currentOffset > 0) {
return date.getTime() + currentOffset;
}
return date.getTime() - currentOffset;
}

function atTimezone(date, timezoneName){
var utcTime = getUTCTime(date);
var time = utcTime + tz.getOffset(timezoneName);
return new Date(time);
}

return {
atTimezone: atTimezone
}
})();
20 changes: 20 additions & 0 deletions src/timezone/timezones.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
var timezones = require('../../timezones');

module.exports = (function(){

var tz = {};

for(var key in timezones){
var value = timezones[key];
tz[key.toLowerCase()] = value;
}

function getOffset(tzName){
return tz[tzName.toLowerCase()] / 100 * 60 * 60000;
}

return {
getOffset: getOffset
}
})();
1 change: 0 additions & 1 deletion test/convert-expression/step-values-conversion-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ describe('step-values-conversion.js', function() {
it('shuld convert step values', function() {
var expressions = '1,2,3,4,5,6,7,8,9,10/2 0,1,2,3,4,5,6,7,8,9/5 * * * *'.split(' ');
expressions = conversion(expressions);
console.log(expressions);
expect(expressions[0]).to.equal('2,4,6,8,10');
expect(expressions[1]).to.equal('0,5');
});
Expand Down
51 changes: 51 additions & 0 deletions test/timezone/task-with-timezone-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

var expect = require('expect.js');
var sinon = require('sinon');
var Task = require('../../src/task');
var cron = require('../../src/node-cron');
var tz = require('../../src/timezone/timezone-converter')

describe('task at timezone', function () {
it('should should perform at timezone', function () {
var task = new Task('* 4 * * *', function () {
this.executed += 1;
}, "America/Virgin");
task.executed = 0;
var date = new Date('August 19, 2018 00:00:00 GMT-03:00');
date.setHours(date.getHours() + 1);
task.update(date);
date.setHours(date.getHours() + 3);
task.update(date);
date.setHours(date.getHours() + 1);
task.update(date);
expect(1).to.equal(task.executed);
});

describe('by node-cron', function () {
beforeEach(function () {
this.now = new Date('August 19, 2018 03:00:00 GMT-04:00');
this.clock = sinon.useFakeTimers(this.now);
});

afterEach(function () {
this.clock.restore();
});

it('should perform at timezone', function () {
var executed = 0;
var executedAt = null;
var executedAt2 = null;
var task = cron.schedule('* 4 * * *', function () {
executed++;
executedAt = new Date();
}, true, "America/Virgin");

this.clock.tick(60 * 60 * 1000);
this.now.setHours(this.now.getHours() + 1);

expect(executedAt.getHours()).to.equal(this.now.getHours());
});

});
});
34 changes: 34 additions & 0 deletions test/timezone/timezone-converter-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

var expect = require('expect.js');
var timezone = require('../../src/timezone/timezone-converter');

describe('convert timezone', function() {

it('should convert a date to greenwich timezone', function() {
var date = new Date('August 19, 2018 23:00:00 GMT-03:00');
var relativeDate = timezone.atTimezone(date, "Greenwich");
expect(relativeDate.getDate()).to.equal(20);
expect(relativeDate.getHours()).to.equal(2);
});

it('should convert a date to greenwich timezone', function() {
var date = new Date('August 19, 2018 00:00:00 GMT-03:00');
var relativeDate = timezone.atTimezone(date, "Greenwich");
expect(relativeDate.getDate()).to.equal(19);
expect(relativeDate.getHours()).to.equal(3);
});

it('should convert a date to Sydney timezone', function() {
var date = new Date('August 19, 2018 14:00:00 GMT+11:00');
var relativeDate = timezone.atTimezone(date, "Greenwich");
expect(relativeDate.getHours()).to.equal(3);
});


it('should convert a date to America/Virgin timezone', function() {
var date = new Date('August 19, 2018 10:00:00 GMT-03:00');
var relativeDate = timezone.atTimezone(date, "America/Virgin")
expect(relativeDate.getHours()).to.equal(9);
});
});

0 comments on commit 69685f3

Please sign in to comment.