-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
performing tasks on relative time at timezone
- Loading branch information
Showing
8 changed files
with
738 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
Oops, something went wrong.