2017-06-03 12:25:21 +00:00
|
|
|
/**
|
|
|
|
* The date library
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
2021-03-08 17:09:56 +00:00
|
|
|
const ComunicDate = {
|
2017-06-03 12:25:21 +00:00
|
|
|
/**
|
|
|
|
* Get current timestamp
|
|
|
|
*
|
|
|
|
* @return {Integer} The current timestamp
|
|
|
|
*/
|
|
|
|
time: function(){
|
|
|
|
return Math.floor(new Date().getTime()/1000);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a difference in second into a date
|
|
|
|
*
|
|
|
|
* @param {Integer} difference The difference between two values
|
|
|
|
* @return {String} The generated date
|
|
|
|
*/
|
|
|
|
diffToStr: function(difference){
|
2019-02-02 09:34:50 +00:00
|
|
|
|
|
|
|
//Check if difference is less than one second
|
|
|
|
if(difference < 0)
|
|
|
|
difference = 0;
|
|
|
|
|
2017-06-03 12:25:21 +00:00
|
|
|
//Calculate seconds
|
|
|
|
var seconds = difference-Math.floor(difference/60)*60;
|
|
|
|
var difference = (difference - seconds)/60;
|
|
|
|
|
|
|
|
//Check there was less than one minute
|
|
|
|
if(difference == 0)
|
2018-08-05 14:40:14 +00:00
|
|
|
return lang("dates_s", [seconds]);
|
2017-06-03 12:25:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
//Calculate minutes
|
|
|
|
var minutes = difference-Math.floor(difference/60)*60;
|
|
|
|
var difference = (difference - minutes)/60;
|
|
|
|
|
|
|
|
//Check there was less than one hour
|
|
|
|
if(difference == 0)
|
2018-08-05 14:40:14 +00:00
|
|
|
return lang("dates_min", [minutes]);
|
2017-06-03 12:25:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
//Calculate hours
|
|
|
|
var hours = difference-Math.floor(difference/24)*24;
|
|
|
|
var difference = (difference - hours)/24;
|
|
|
|
|
|
|
|
//Check there was less than a day
|
|
|
|
if(difference == 0)
|
2018-08-05 14:40:14 +00:00
|
|
|
return lang("dates_h", [hours]);
|
2017-06-03 12:25:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
//Calculate days
|
|
|
|
var days = difference-Math.floor(difference/30)*30;
|
|
|
|
var difference = (difference - days)/30;
|
|
|
|
|
|
|
|
//Check there was less than a month
|
|
|
|
if(difference == 0){
|
|
|
|
if(days == 1)
|
2018-08-05 14:40:14 +00:00
|
|
|
return lang("dates_one_day");
|
2017-06-03 12:25:21 +00:00
|
|
|
else
|
2018-08-05 14:40:14 +00:00
|
|
|
return lang("dates_days", [days]);
|
2017-06-03 12:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Calculate months
|
|
|
|
var months = difference-Math.floor(difference/12)*12;
|
|
|
|
var difference = (difference - months)/12;
|
|
|
|
|
|
|
|
//Check there was less than a year
|
|
|
|
if(difference == 0){
|
|
|
|
if(months == 1)
|
2018-08-05 14:40:14 +00:00
|
|
|
return lang("dates_one_month");
|
2017-06-03 12:25:21 +00:00
|
|
|
else
|
2018-08-05 14:40:14 +00:00
|
|
|
return lang("dates_months", [months]);
|
2017-06-03 12:25:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Calculate years
|
|
|
|
var years = difference;
|
|
|
|
if(years == 1){
|
2018-08-05 14:40:14 +00:00
|
|
|
return lang("dates_one_year");
|
2017-06-03 12:25:21 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-08-05 14:40:14 +00:00
|
|
|
return lang("dates_years", [years]);
|
2017-06-03 12:25:21 +00:00
|
|
|
}
|
|
|
|
},
|
2017-12-23 17:32:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the difference of time from now to a specified
|
|
|
|
* timestamp and return it as a string
|
|
|
|
*
|
|
|
|
* @param {Integer} time The base time
|
|
|
|
* @return {String} Computed difference
|
|
|
|
*/
|
|
|
|
timeDiffToStr: function(time){
|
|
|
|
return this.diffToStr(this.time() - time);
|
|
|
|
},
|
2021-03-08 17:09:56 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 15:49:47 +00:00
|
|
|
ComunicWeb.common.date = ComunicDate;
|
|
|
|
|
2021-04-21 16:27:42 +00:00
|
|
|
/**
|
|
|
|
* Add a day to a date
|
|
|
|
*
|
|
|
|
* @param {Date} date Target date
|
|
|
|
*/
|
|
|
|
function addOneDay(date) {
|
|
|
|
return new Date(date.getTime() + 1000*60*60*24);
|
|
|
|
}
|
|
|
|
|
2021-04-21 15:49:47 +00:00
|
|
|
/**
|
|
|
|
* Get all the days of a specified range
|
|
|
|
*
|
|
|
|
* @param {Date} start
|
|
|
|
* @param {Date} end (exclusive)
|
|
|
|
*/
|
|
|
|
function getDaysOfRange(start, end) {
|
|
|
|
let curr = start;
|
|
|
|
let list = [];
|
|
|
|
|
|
|
|
while (curr < end) {
|
|
|
|
list.push(curr);
|
2021-04-21 16:27:42 +00:00
|
|
|
curr = addOneDay(curr);
|
2021-04-21 15:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
2021-04-21 16:27:42 +00:00
|
|
|
}
|