debian-mirror-gitlab/app/assets/javascripts/lib/utils/datetime_utility.js

394 lines
11 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2017-09-10 17:25:29 +05:30
import timeago from 'timeago.js';
2018-11-08 19:23:39 +05:30
import dateFormat from 'dateformat';
2018-03-17 18:26:18 +05:30
import { pluralize } from './text_utility';
2018-11-08 19:23:39 +05:30
import { languageCode, s__ } from '../../locale';
2017-09-10 17:25:29 +05:30
window.timeago = timeago;
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
/**
* Returns i18n month names array.
* If `abbreviated` is provided, returns abbreviated
* name.
*
* @param {Boolean} abbreviated
*/
2018-11-08 19:23:39 +05:30
const getMonthNames = abbreviated => {
2018-03-17 18:26:18 +05:30
if (abbreviated) {
2018-11-08 19:23:39 +05:30
return [
s__('Jan'),
s__('Feb'),
s__('Mar'),
s__('Apr'),
s__('May'),
s__('Jun'),
s__('Jul'),
s__('Aug'),
s__('Sep'),
s__('Oct'),
s__('Nov'),
s__('Dec'),
];
2018-03-17 18:26:18 +05:30
}
2018-11-08 19:23:39 +05:30
return [
s__('January'),
s__('February'),
s__('March'),
s__('April'),
s__('May'),
s__('June'),
s__('July'),
s__('August'),
s__('September'),
s__('October'),
s__('November'),
s__('December'),
];
2018-03-17 18:26:18 +05:30
};
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
/**
* Given a date object returns the day of the week in English
* @param {date} date
* @returns {String}
*/
2018-11-08 19:23:39 +05:30
export const getDayName = date =>
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
/**
* @example
* dateFormat('2017-12-05','mmm d, yyyy h:MMtt Z' ) -> "Dec 5, 2017 12:00am GMT+0000"
* @param {date} datetime
* @returns {String}
*/
export const formatDate = datetime => dateFormat(datetime, 'mmm d, yyyy h:MMtt Z');
/**
* Timeago uses underscores instead of dashes to separate language from country code.
*
* see https://github.com/hustcc/timeago.js/tree/v3.0.0/locales
*/
const timeagoLanguageCode = languageCode().replace(/-/g, '_');
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
let timeagoInstance;
/**
* Sets a timeago Instance
*/
export function getTimeago() {
if (!timeagoInstance) {
const localeRemaining = function getLocaleRemaining(number, index) {
return [
2018-11-08 19:23:39 +05:30
[s__('Timeago|just now'), s__('Timeago|right now')],
[s__('Timeago|%s seconds ago'), s__('Timeago|%s seconds remaining')],
[s__('Timeago|1 minute ago'), s__('Timeago|1 minute remaining')],
2018-03-17 18:26:18 +05:30
[s__('Timeago|%s minutes ago'), s__('Timeago|%s minutes remaining')],
2018-11-08 19:23:39 +05:30
[s__('Timeago|1 hour ago'), s__('Timeago|1 hour remaining')],
[s__('Timeago|%s hours ago'), s__('Timeago|%s hours remaining')],
[s__('Timeago|1 day ago'), s__('Timeago|1 day remaining')],
2018-03-17 18:26:18 +05:30
[s__('Timeago|%s days ago'), s__('Timeago|%s days remaining')],
2018-11-08 19:23:39 +05:30
[s__('Timeago|1 week ago'), s__('Timeago|1 week remaining')],
2018-03-17 18:26:18 +05:30
[s__('Timeago|%s weeks ago'), s__('Timeago|%s weeks remaining')],
2018-11-08 19:23:39 +05:30
[s__('Timeago|1 month ago'), s__('Timeago|1 month remaining')],
2018-03-17 18:26:18 +05:30
[s__('Timeago|%s months ago'), s__('Timeago|%s months remaining')],
2018-11-08 19:23:39 +05:30
[s__('Timeago|1 year ago'), s__('Timeago|1 year remaining')],
2018-03-17 18:26:18 +05:30
[s__('Timeago|%s years ago'), s__('Timeago|%s years remaining')],
][index];
};
const locale = function getLocale(number, index) {
return [
2018-11-08 19:23:39 +05:30
[s__('Timeago|just now'), s__('Timeago|right now')],
[s__('Timeago|%s seconds ago'), s__('Timeago|in %s seconds')],
[s__('Timeago|1 minute ago'), s__('Timeago|in 1 minute')],
2018-03-17 18:26:18 +05:30
[s__('Timeago|%s minutes ago'), s__('Timeago|in %s minutes')],
2018-11-08 19:23:39 +05:30
[s__('Timeago|1 hour ago'), s__('Timeago|in 1 hour')],
[s__('Timeago|%s hours ago'), s__('Timeago|in %s hours')],
[s__('Timeago|1 day ago'), s__('Timeago|in 1 day')],
2018-03-17 18:26:18 +05:30
[s__('Timeago|%s days ago'), s__('Timeago|in %s days')],
2018-11-08 19:23:39 +05:30
[s__('Timeago|1 week ago'), s__('Timeago|in 1 week')],
2018-03-17 18:26:18 +05:30
[s__('Timeago|%s weeks ago'), s__('Timeago|in %s weeks')],
2018-11-08 19:23:39 +05:30
[s__('Timeago|1 month ago'), s__('Timeago|in 1 month')],
2018-03-17 18:26:18 +05:30
[s__('Timeago|%s months ago'), s__('Timeago|in %s months')],
2018-11-08 19:23:39 +05:30
[s__('Timeago|1 year ago'), s__('Timeago|in 1 year')],
2018-03-17 18:26:18 +05:30
[s__('Timeago|%s years ago'), s__('Timeago|in %s years')],
][index];
2016-09-13 17:45:13 +05:30
};
2018-03-17 18:26:18 +05:30
timeago.register(timeagoLanguageCode, locale);
timeago.register(`${timeagoLanguageCode}-remaining`, localeRemaining);
timeagoInstance = timeago();
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
return timeagoInstance;
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
/**
* For the given element, renders a timeago instance.
* @param {jQuery} $els
*/
2018-11-08 19:23:39 +05:30
export const renderTimeago = $els => {
2018-03-17 18:26:18 +05:30
const timeagoEls = $els || document.querySelectorAll('.js-timeago-render');
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
// timeago.js sets timeouts internally for each timeago value to be updated in real time
getTimeago().render(timeagoEls, timeagoLanguageCode);
};
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
/**
* For the given elements, sets a tooltip with a formatted date.
* @param {jQuery}
* @param {Boolean} setTimeago
*/
export const localTimeAgo = ($timeagoEls, setTimeago = true) => {
$timeagoEls.each((i, el) => {
if (setTimeago) {
// Recreate with custom template
$(el).tooltip({
2018-11-08 19:23:39 +05:30
template:
'<div class="tooltip local-timeago" role="tooltip"><div class="arrow"></div><div class="tooltip-inner"></div></div>',
2018-03-17 18:26:18 +05:30
});
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
el.classList.add('js-timeago-render');
});
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
renderTimeago($timeagoEls);
};
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
/**
* Returns remaining or passed time over the given time.
* @param {*} time
* @param {*} expiredLabel
*/
export const timeFor = (time, expiredLabel) => {
if (!time) {
return '';
}
if (new Date(time) < new Date()) {
return expiredLabel || s__('Timeago|Past due');
}
2018-11-08 19:23:39 +05:30
return getTimeago()
.format(time, `${timeagoLanguageCode}-remaining`)
.trim();
2018-03-17 18:26:18 +05:30
};
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
export const getDayDifference = (a, b) => {
const millisecondsPerDay = 1000 * 60 * 60 * 24;
const date1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
const date2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
2016-09-29 09:46:39 +05:30
2018-03-17 18:26:18 +05:30
return Math.floor((date2 - date1) / millisecondsPerDay);
};
2017-09-10 17:25:29 +05:30
/**
* Port of ruby helper time_interval_in_words.
*
* @param {Number} seconds
* @return {String}
*/
export function timeIntervalInWords(intervalInSeconds) {
const secondsInteger = parseInt(intervalInSeconds, 10);
const minutes = Math.floor(secondsInteger / 60);
2018-11-08 19:23:39 +05:30
const seconds = secondsInteger - minutes * 60;
2017-09-10 17:25:29 +05:30
let text = '';
if (minutes >= 1) {
2018-03-17 18:26:18 +05:30
text = `${minutes} ${pluralize('minute', minutes)} ${seconds} ${pluralize('second', seconds)}`;
2017-09-10 17:25:29 +05:30
} else {
2018-03-17 18:26:18 +05:30
text = `${seconds} ${pluralize('second', seconds)}`;
2017-09-10 17:25:29 +05:30
}
return text;
}
2018-03-17 18:26:18 +05:30
export function dateInWords(date, abbreviated = false, hideYear = false) {
if (!date) return date;
const month = date.getMonth();
const year = date.getFullYear();
2018-11-08 19:23:39 +05:30
const monthNames = [
s__('January'),
s__('February'),
s__('March'),
s__('April'),
s__('May'),
s__('June'),
s__('July'),
s__('August'),
s__('September'),
s__('October'),
s__('November'),
s__('December'),
];
const monthNamesAbbr = [
s__('Jan'),
s__('Feb'),
s__('Mar'),
s__('Apr'),
s__('May'),
s__('Jun'),
s__('Jul'),
s__('Aug'),
s__('Sep'),
s__('Oct'),
s__('Nov'),
s__('Dec'),
];
2018-03-17 18:26:18 +05:30
const monthName = abbreviated ? monthNamesAbbr[month] : monthNames[month];
if (hideYear) {
return `${monthName} ${date.getDate()}`;
}
return `${monthName} ${date.getDate()}, ${year}`;
}
/**
* Returns month name based on provided date.
*
* @param {Date} date
* @param {Boolean} abbreviated
*/
export const monthInWords = (date, abbreviated = false) => {
if (!date) {
return '';
}
return getMonthNames(abbreviated)[date.getMonth()];
};
/**
* Returns number of days in a month for provided date.
* courtesy: https://stacko(verflow.com/a/1185804/414749
*
* @param {Date} date
*/
2018-11-08 19:23:39 +05:30
export const totalDaysInMonth = date => {
2018-03-17 18:26:18 +05:30
if (!date) {
return 0;
}
return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
};
2018-11-08 19:23:39 +05:30
/**
* Returns number of days in a quarter from provided
* months array.
*
* @param {Array} quarter
*/
export const totalDaysInQuarter = quarter =>
quarter.reduce((acc, month) => acc + totalDaysInMonth(month), 0);
2018-03-17 18:26:18 +05:30
/**
* Returns list of Dates referring to Sundays of the month
* based on provided date
*
* @param {Date} date
*/
2018-11-08 19:23:39 +05:30
export const getSundays = date => {
2018-03-17 18:26:18 +05:30
if (!date) {
return [];
}
2018-11-08 19:23:39 +05:30
const daysToSunday = [
'Saturday',
'Friday',
'Thursday',
'Wednesday',
'Tuesday',
'Monday',
'Sunday',
];
2018-03-17 18:26:18 +05:30
const month = date.getMonth();
const year = date.getFullYear();
const sundays = [];
const dateOfMonth = new Date(year, month, 1);
while (dateOfMonth.getMonth() === month) {
const dayName = getDayName(dateOfMonth);
if (dayName === 'Sunday') {
sundays.push(new Date(dateOfMonth.getTime()));
}
const daysUntilNextSunday = daysToSunday.indexOf(dayName) + 1;
dateOfMonth.setDate(dateOfMonth.getDate() + daysUntilNextSunday);
}
return sundays;
};
/**
2018-11-08 19:23:39 +05:30
* Returns list of Dates representing a timeframe of months from startDate and length
2018-03-17 18:26:18 +05:30
*
2018-11-08 19:23:39 +05:30
* @param {Date} startDate
2018-03-17 18:26:18 +05:30
* @param {Number} length
*/
2018-11-08 19:23:39 +05:30
export const getTimeframeWindowFrom = (startDate, length) => {
if (!(startDate instanceof Date) || !length) {
2018-03-17 18:26:18 +05:30
return [];
}
2018-11-08 19:23:39 +05:30
// Iterate and set date for the size of length
2018-03-17 18:26:18 +05:30
// and push date reference to timeframe list
2018-11-08 19:23:39 +05:30
const timeframe = new Array(length)
.fill()
.map((val, i) => new Date(startDate.getFullYear(), startDate.getMonth() + i, 1));
2018-03-17 18:26:18 +05:30
// Change date of last timeframe item to last date of the month
timeframe[length - 1].setDate(totalDaysInMonth(timeframe[length - 1]));
return timeframe;
};
2018-11-08 19:23:39 +05:30
/**
* Returns count of day within current quarter from provided date
* and array of months for the quarter
*
* Eg;
* If date is 15 Feb 2018
* and quarter is [Jan, Feb, Mar]
*
* Then 15th Feb is 46th day of the quarter
* Where 31 (days in Jan) + 15 (date of Feb).
*
* @param {Date} date
* @param {Array} quarter
*/
export const dayInQuarter = (date, quarter) =>
quarter.reduce((acc, month) => {
if (date.getMonth() > month.getMonth()) {
return acc + totalDaysInMonth(month);
} else if (date.getMonth() === month.getMonth()) {
return acc + date.getDate();
}
return acc + 0;
}, 0);
2018-03-17 18:26:18 +05:30
window.gl = window.gl || {};
window.gl.utils = {
...(window.gl.utils || {}),
getTimeago,
localTimeAgo,
};
2018-12-05 23:21:45 +05:30
/**
* Formats milliseconds as timestamp (e.g. 01:02:03).
* This takes durations longer than a day into account (e.g. two days would be 48:00:00).
*
* @param milliseconds
* @returns {string}
*/
export const formatTime = milliseconds => {
const remainingSeconds = Math.floor(milliseconds / 1000) % 60;
const remainingMinutes = Math.floor(milliseconds / 1000 / 60) % 60;
const remainingHours = Math.floor(milliseconds / 1000 / 60 / 60);
let formattedTime = '';
if (remainingHours < 10) formattedTime += '0';
formattedTime += `${remainingHours}:`;
if (remainingMinutes < 10) formattedTime += '0';
formattedTime += `${remainingMinutes}:`;
if (remainingSeconds < 10) formattedTime += '0';
formattedTime += remainingSeconds;
return formattedTime;
};