2015-08-13 21:32:02 +02:00
|
|
|
//time.js
|
2015-08-16 13:18:01 +02:00
|
|
|
|
|
|
|
//Importing self-written modules.
|
2015-08-13 22:00:36 +02:00
|
|
|
var config = require('./configuration');
|
2015-08-13 21:32:02 +02:00
|
|
|
|
2015-08-16 13:18:01 +02:00
|
|
|
/**
|
|
|
|
* Function for getting the time, with minutes as a fracture.
|
|
|
|
* @return {Float} time - The time, fractured (20.5 instead of 20:30)
|
|
|
|
*/
|
2015-08-13 21:32:02 +02:00
|
|
|
function get() {
|
|
|
|
var time = new Date();
|
|
|
|
return time.getHours() + (time.getMinutes() / 60);
|
|
|
|
}
|
|
|
|
|
2015-08-16 13:18:01 +02:00
|
|
|
/**
|
|
|
|
* Function for convert hh:mm to fractured time (hh.mm)
|
|
|
|
* @param {String} timestr - A string containing a time from and to (e.g "9:15 - 10:00")
|
|
|
|
* @return {Array} array - An array containing the time string split in two.
|
|
|
|
*/
|
2015-08-13 22:00:36 +02:00
|
|
|
function parse(timestr) {
|
|
|
|
var parsed = timestr.match(/\d{1,2}:\d+/g);
|
|
|
|
var array = [];
|
|
|
|
|
|
|
|
for (time of parsed) {
|
|
|
|
array.push(parseInt(time.split(':')[0]) + (parseInt(time.split(':')[1]) / 60));
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2015-08-16 13:18:01 +02:00
|
|
|
/**
|
|
|
|
* Function for parsing and checking if the currrent time is within the parsed string.
|
|
|
|
* @param {String} timespan - A string containing a time from and to (e.g "9:15 - 10:00")
|
|
|
|
* @return {Boolean} - Returns true if the current time is within the timespan or false when it's not.
|
|
|
|
*/
|
2015-08-15 21:07:22 +02:00
|
|
|
function withinTimespan(timespan) {
|
2015-08-13 22:15:44 +02:00
|
|
|
if (get() > parse(timespan)[0] && get() < parse(timespan)[1]) return true;
|
|
|
|
else return false;
|
2015-08-13 21:32:02 +02:00
|
|
|
}
|
|
|
|
|
2015-08-16 13:18:01 +02:00
|
|
|
/**
|
|
|
|
* Function that uses withinTimespan() to determine if the current time is
|
|
|
|
* within the earliest and the latest time strings.
|
|
|
|
* @return {Boolean} - Returns true if the current time is within the timespan or false when it's not.
|
|
|
|
*/
|
2015-08-15 21:07:22 +02:00
|
|
|
function duringSchool() {
|
|
|
|
var start = parse(config().times[0])[0];
|
|
|
|
var end = parse(config().times[config().times.length - 1])[1];
|
2015-08-13 22:00:36 +02:00
|
|
|
|
|
|
|
if (get() > start && get() < end) return true;
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
2015-08-16 13:18:01 +02:00
|
|
|
//Export the functions as a module.
|
2015-08-13 21:32:02 +02:00
|
|
|
module.exports = {
|
|
|
|
'get': get,
|
2015-08-15 21:07:22 +02:00
|
|
|
'withinTimespan': withinTimespan,
|
|
|
|
'duringSchool': duringSchool
|
2015-08-13 21:32:02 +02:00
|
|
|
}
|