Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

Commit

Permalink
Implemented timeRange function
Browse files Browse the repository at this point in the history
  • Loading branch information
utsavkesharwani committed Sep 25, 2016
1 parent e0ddf63 commit 58a3f9e
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 3 deletions.
57 changes: 57 additions & 0 deletions test/timeRange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

/**
* Module dependencies.
*/

var assert = require('assert');
var timeRange = require('../timeRange');
var vanillaGetHours = Date.prototype.getHours;
var vanillaGetMinutes = Date.prototype.getMinutes;
var vanillaGetSeconds = Date.prototype.getSeconds;
var vanillaGetUTCHours = Date.prototype.getUTCHours;
var vanillaGetUTCMinutes = Date.prototype.getUTCMinutes;
var vanillaGetUTCSeconds = Date.prototype.getUTCSeconds;

describe('hooks', function() {

before(function() {
// Setting local time as 01:24:30
Date.prototype.getHours = function() { return 1; }
Date.prototype.getMinutes = function() { return 24; }
Date.prototype.getSeconds = function() { return 30; }

// Setting UTC time as 19:54:30
Date.prototype.getUTCHours = function() { return 19; }
Date.prototype.getUTCMinutes = function() { return 54; }
Date.prototype.getUTCSeconds = function() { return 30; }
});

after(function() {
Date.prototype.getHours = vanillaGetHours;
Date.prototype.getUTCHours = vanillaGetUTCHours;
Date.prototype.getUTCMinutes = vanillaGetUTCMinutes;
Date.prototype.getUTCSeconds = vanillaGetUTCSeconds;
});


describe('timeRange()', function () {


var tests = [
[1, true],
[1, 2, true],
[0, 0, 0, 30, false],
[0, 0, 0, 0, 30, 0, false],
[0, 0, 0, 0, 30, 0, 'GMT', false],
[0, 0, 0, 20, 0, 0, 'GMT', true],
];

tests.forEach(function (test) {
var expected = test.pop();
it('should return `' + expected +'` for "' + test.join('", "') + '"', function () {
assert.equal(expected, timeRange.apply(this, test));
});
});

});
});
69 changes: 66 additions & 3 deletions timeRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,70 @@ module.exports = timeRange;
* @return {Boolean}
*/

function timeRange (wd1, wd2, gmt) {
// TODO: implement me!
return false;
function timeRange () {
var args = Array.prototype.slice.call(arguments),
lastArg = args.pop(),
useGMTzone = (lastArg == "GMT"),
currentDate = new Date();

if (!useGMTzone) { args.push(lastArg); }

var noOfArgs = args.length,
result = false,
numericArgs = args.map(function(n) { return parseInt(n) });

// timeRange(hour)
if (noOfArgs == 1) {
result = getCurrentHour(useGMTzone, currentDate) == numericArgs[0];

// timeRange(hour1, hour2)
} else if (noOfArgs == 2) {
var currentHour = getCurrentHour(useGMTzone, currentDate);
result = (numericArgs[0] <= currentHour) && (currentHour < numericArgs[1]);

// timeRange(hour1, min1, hour2, min2)
} else if (noOfArgs == 4) {
result =
valueInRange(
secondsElapsedToday(numericArgs[0], numericArgs[1], 0),
secondsElapsedToday(getCurrentHour(useGMTzone, currentDate), getCurrentMinute(useGMTzone, currentDate), 0),
secondsElapsedToday(numericArgs[2], numericArgs[3], 59)
);

// timeRange(hour1, min1, sec1, hour2, min2, sec2)
} else if (noOfArgs == 6) {
result =
valueInRange(
secondsElapsedToday(numericArgs[0], numericArgs[1], numericArgs[2]),
secondsElapsedToday(
getCurrentHour(useGMTzone, currentDate),
getCurrentMinute(useGMTzone, currentDate),
getCurrentSecond(useGMTzone, currentDate)
),
secondsElapsedToday(numericArgs[3], numericArgs[4], numericArgs[5])
);
}

return result;
}

function secondsElapsedToday (hh, mm, ss) {
return ((hh*3600) + (mm*60) + ss);
}

function getCurrentHour (gmt, currentDate) {
return (gmt ? currentDate.getUTCHours() : currentDate.getHours());
}

function getCurrentMinute (gmt, currentDate) {
return (gmt ? currentDate.getUTCMinutes() : currentDate.getMinutes());
}

function getCurrentSecond (gmt, currentDate) {
return (gmt ? currentDate.getUTCSeconds() : currentDate.getSeconds());
}

// start <= value <= finish
function valueInRange (start, value, finish) {
return (start <= value) && (value <= finish);
}

0 comments on commit 58a3f9e

Please sign in to comment.