2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2018-10-24 07:02:23 -04:00
|
|
|
import _ from 'underscore';
|
2017-05-16 16:52:40 -04:00
|
|
|
import timeago from 'timeago.js';
|
2018-07-02 03:46:40 -04:00
|
|
|
import dateFormat from 'dateformat';
|
2019-07-24 13:39:18 -04:00
|
|
|
import { languageCode, s__, __, n__ } from '../../locale';
|
2017-06-07 16:13:44 -04:00
|
|
|
|
2017-05-16 16:52:40 -04:00
|
|
|
window.timeago = timeago;
|
2016-12-21 11:12:33 -05:00
|
|
|
|
2019-01-08 08:10:41 -05:00
|
|
|
/**
|
|
|
|
* This method allows you to create new Date instance from existing
|
|
|
|
* date instance without keeping the reference.
|
|
|
|
*
|
|
|
|
* @param {Date} date
|
|
|
|
*/
|
|
|
|
export const newDate = date => (date instanceof Date ? new Date(date.getTime()) : new Date());
|
|
|
|
|
2018-02-03 03:04:02 -05:00
|
|
|
/**
|
|
|
|
* Returns i18n month names array.
|
|
|
|
* If `abbreviated` is provided, returns abbreviated
|
|
|
|
* name.
|
|
|
|
*
|
|
|
|
* @param {Boolean} abbreviated
|
|
|
|
*/
|
2018-11-07 12:22:15 -05:00
|
|
|
export const getMonthNames = abbreviated => {
|
2018-02-03 03:04:02 -05:00
|
|
|
if (abbreviated) {
|
2018-05-22 13:41:53 -04:00
|
|
|
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-02-03 03:04:02 -05:00
|
|
|
}
|
2018-05-22 13:41:53 -04:00
|
|
|
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-02-03 03:04:02 -05:00
|
|
|
};
|
|
|
|
|
2018-10-24 06:50:55 -04:00
|
|
|
export const pad = (val, len = 2) => `0${val}`.slice(-len);
|
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
/**
|
|
|
|
* Given a date object returns the day of the week in English
|
|
|
|
* @param {date} date
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2018-05-22 13:41:53 -04:00
|
|
|
export const getDayName = date =>
|
2019-05-23 16:08:18 -04:00
|
|
|
[
|
|
|
|
__('Sunday'),
|
|
|
|
__('Monday'),
|
|
|
|
__('Tuesday'),
|
|
|
|
__('Wednesday'),
|
|
|
|
__('Thursday'),
|
|
|
|
__('Friday'),
|
|
|
|
__('Saturday'),
|
|
|
|
][date.getDay()];
|
2016-12-21 11:12:33 -05:00
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
/**
|
|
|
|
* @example
|
|
|
|
* dateFormat('2017-12-05','mmm d, yyyy h:MMtt Z' ) -> "Dec 5, 2017 12:00am GMT+0000"
|
|
|
|
* @param {date} datetime
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2019-05-28 03:53:44 -04:00
|
|
|
export const formatDate = datetime => {
|
|
|
|
if (_.isString(datetime) && datetime.match(/\d+-\d+\d+ /)) {
|
2019-06-05 05:28:31 -04:00
|
|
|
throw new Error(__('Invalid date'));
|
2019-05-28 03:53:44 -04:00
|
|
|
}
|
|
|
|
return dateFormat(datetime, 'mmm d, yyyy h:MMtt Z');
|
|
|
|
};
|
2016-12-21 11:12:33 -05:00
|
|
|
|
2017-12-21 06:22:47 -05:00
|
|
|
/**
|
|
|
|
* 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, '_');
|
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
let timeagoInstance;
|
2017-12-21 06:22:47 -05:00
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
/**
|
|
|
|
* Sets a timeago Instance
|
|
|
|
*/
|
2018-10-24 06:43:00 -04:00
|
|
|
export const getTimeago = () => {
|
2017-12-07 06:09:17 -05:00
|
|
|
if (!timeagoInstance) {
|
2019-01-21 18:14:21 -05:00
|
|
|
const memoizedLocaleRemaining = () => {
|
|
|
|
const cache = [];
|
|
|
|
|
2019-01-22 10:07:27 -05:00
|
|
|
const timeAgoLocaleRemaining = [
|
|
|
|
() => [s__('Timeago|just now'), s__('Timeago|right now')],
|
2019-04-03 14:21:15 -04:00
|
|
|
() => [s__('Timeago|just now'), s__('Timeago|%s seconds remaining')],
|
2019-01-22 10:07:27 -05:00
|
|
|
() => [s__('Timeago|1 minute ago'), s__('Timeago|1 minute remaining')],
|
|
|
|
() => [s__('Timeago|%s minutes ago'), s__('Timeago|%s minutes remaining')],
|
|
|
|
() => [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')],
|
|
|
|
() => [s__('Timeago|%s days ago'), s__('Timeago|%s days remaining')],
|
|
|
|
() => [s__('Timeago|1 week ago'), s__('Timeago|1 week remaining')],
|
|
|
|
() => [s__('Timeago|%s weeks ago'), s__('Timeago|%s weeks remaining')],
|
|
|
|
() => [s__('Timeago|1 month ago'), s__('Timeago|1 month remaining')],
|
|
|
|
() => [s__('Timeago|%s months ago'), s__('Timeago|%s months remaining')],
|
|
|
|
() => [s__('Timeago|1 year ago'), s__('Timeago|1 year remaining')],
|
|
|
|
() => [s__('Timeago|%s years ago'), s__('Timeago|%s years remaining')],
|
|
|
|
];
|
|
|
|
|
2019-01-21 18:14:21 -05:00
|
|
|
return (number, index) => {
|
|
|
|
if (cache[index]) {
|
|
|
|
return cache[index];
|
|
|
|
}
|
2019-01-22 10:07:27 -05:00
|
|
|
cache[index] = timeAgoLocaleRemaining[index] && timeAgoLocaleRemaining[index]();
|
|
|
|
return cache[index];
|
2019-01-21 18:14:21 -05:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const memoizedLocale = () => {
|
|
|
|
const cache = [];
|
|
|
|
|
2019-01-22 10:07:27 -05:00
|
|
|
const timeAgoLocale = [
|
|
|
|
() => [s__('Timeago|just now'), s__('Timeago|right now')],
|
2019-04-03 14:21:15 -04:00
|
|
|
() => [s__('Timeago|just now'), s__('Timeago|in %s seconds')],
|
2019-01-22 10:07:27 -05:00
|
|
|
() => [s__('Timeago|1 minute ago'), s__('Timeago|in 1 minute')],
|
|
|
|
() => [s__('Timeago|%s minutes ago'), s__('Timeago|in %s minutes')],
|
|
|
|
() => [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')],
|
|
|
|
() => [s__('Timeago|%s days ago'), s__('Timeago|in %s days')],
|
|
|
|
() => [s__('Timeago|1 week ago'), s__('Timeago|in 1 week')],
|
|
|
|
() => [s__('Timeago|%s weeks ago'), s__('Timeago|in %s weeks')],
|
|
|
|
() => [s__('Timeago|1 month ago'), s__('Timeago|in 1 month')],
|
|
|
|
() => [s__('Timeago|%s months ago'), s__('Timeago|in %s months')],
|
|
|
|
() => [s__('Timeago|1 year ago'), s__('Timeago|in 1 year')],
|
|
|
|
() => [s__('Timeago|%s years ago'), s__('Timeago|in %s years')],
|
|
|
|
];
|
|
|
|
|
2019-01-21 18:14:21 -05:00
|
|
|
return (number, index) => {
|
|
|
|
if (cache[index]) {
|
|
|
|
return cache[index];
|
|
|
|
}
|
2019-01-22 10:07:27 -05:00
|
|
|
cache[index] = timeAgoLocale[index] && timeAgoLocale[index]();
|
|
|
|
return cache[index];
|
2019-01-21 18:14:21 -05:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
timeago.register(timeagoLanguageCode, memoizedLocale());
|
|
|
|
timeago.register(`${timeagoLanguageCode}-remaining`, memoizedLocaleRemaining());
|
2019-01-15 10:00:05 -05:00
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
timeagoInstance = timeago();
|
|
|
|
}
|
2016-12-21 11:12:33 -05:00
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
return timeagoInstance;
|
2018-10-24 07:30:39 -04:00
|
|
|
};
|
2016-12-21 11:12:33 -05:00
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
/**
|
|
|
|
* For the given elements, sets a tooltip with a formatted date.
|
2019-01-21 18:10:30 -05:00
|
|
|
* @param {JQuery} $timeagoEls
|
2017-12-07 06:09:17 -05:00
|
|
|
* @param {Boolean} setTimeago
|
|
|
|
*/
|
|
|
|
export const localTimeAgo = ($timeagoEls, setTimeago = true) => {
|
2019-04-03 14:21:15 -04:00
|
|
|
getTimeago();
|
|
|
|
|
|
|
|
$timeagoEls.each((i, el) => {
|
|
|
|
$(el).text(timeagoInstance.format($(el).attr('datetime'), timeagoLanguageCode));
|
|
|
|
});
|
2016-12-21 11:12:33 -05:00
|
|
|
|
2019-01-21 18:10:30 -05:00
|
|
|
if (!setTimeago) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-15 10:00:05 -05:00
|
|
|
|
2019-01-21 18:10:30 -05:00
|
|
|
function addTimeAgoTooltip() {
|
|
|
|
$timeagoEls.each((i, el) => {
|
|
|
|
// Recreate with custom template
|
|
|
|
$(el).tooltip({
|
|
|
|
template:
|
|
|
|
'<div class="tooltip local-timeago" role="tooltip"><div class="arrow"></div><div class="tooltip-inner"></div></div>',
|
|
|
|
});
|
|
|
|
});
|
2019-01-15 10:00:05 -05:00
|
|
|
}
|
2019-01-21 18:10:30 -05:00
|
|
|
|
|
|
|
requestIdleCallback(addTimeAgoTooltip);
|
2017-12-07 06:09:17 -05:00
|
|
|
};
|
2016-12-21 11:12:33 -05:00
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
/**
|
|
|
|
* 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-05-22 13:41:53 -04:00
|
|
|
return getTimeago()
|
|
|
|
.format(time, `${timeagoLanguageCode}-remaining`)
|
|
|
|
.trim();
|
2017-12-07 06:09:17 -05:00
|
|
|
};
|
2016-12-21 11:12:33 -05:00
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
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-12-21 11:12:33 -05:00
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
return Math.floor((date2 - date1) / millisecondsPerDay);
|
|
|
|
};
|
2017-06-12 05:20:19 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Port of ruby helper time_interval_in_words.
|
|
|
|
*
|
|
|
|
* @param {Number} seconds
|
|
|
|
* @return {String}
|
|
|
|
*/
|
2018-10-24 07:30:39 -04:00
|
|
|
export const timeIntervalInWords = intervalInSeconds => {
|
2017-06-12 05:20:19 -04:00
|
|
|
const secondsInteger = parseInt(intervalInSeconds, 10);
|
|
|
|
const minutes = Math.floor(secondsInteger / 60);
|
2018-05-22 13:41:53 -04:00
|
|
|
const seconds = secondsInteger - minutes * 60;
|
2019-07-24 13:39:18 -04:00
|
|
|
const secondsText = n__('%d second', '%d seconds', seconds);
|
|
|
|
return minutes >= 1
|
|
|
|
? [n__('%d minute', '%d minutes', minutes), secondsText].join(' ')
|
|
|
|
: secondsText;
|
2018-10-24 07:30:39 -04:00
|
|
|
};
|
2017-11-10 18:41:04 -05:00
|
|
|
|
2018-10-24 06:43:00 -04:00
|
|
|
export const dateInWords = (date, abbreviated = false, hideYear = false) => {
|
2017-11-10 18:41:04 -05:00
|
|
|
if (!date) return date;
|
|
|
|
|
|
|
|
const month = date.getMonth();
|
|
|
|
const year = date.getFullYear();
|
|
|
|
|
2018-05-22 13:41:53 -04:00
|
|
|
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'),
|
|
|
|
];
|
2017-11-10 18:41:04 -05:00
|
|
|
|
|
|
|
const monthName = abbreviated ? monthNamesAbbr[month] : monthNames[month];
|
|
|
|
|
2018-02-03 03:04:02 -05:00
|
|
|
if (hideYear) {
|
|
|
|
return `${monthName} ${date.getDate()}`;
|
|
|
|
}
|
|
|
|
|
2017-11-10 18:41:04 -05:00
|
|
|
return `${monthName} ${date.getDate()}, ${year}`;
|
2018-10-24 07:30:39 -04:00
|
|
|
};
|
2017-12-07 06:09:17 -05:00
|
|
|
|
2018-02-03 03:04:02 -05:00
|
|
|
/**
|
|
|
|
* 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-05-22 13:41:53 -04:00
|
|
|
export const totalDaysInMonth = date => {
|
2018-02-03 03:04:02 -05:00
|
|
|
if (!date) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
|
|
|
|
};
|
|
|
|
|
2018-06-07 12:08:27 -04:00
|
|
|
/**
|
|
|
|
* Returns number of days in a quarter from provided
|
|
|
|
* months array.
|
|
|
|
*
|
|
|
|
* @param {Array} quarter
|
|
|
|
*/
|
2018-07-02 03:46:40 -04:00
|
|
|
export const totalDaysInQuarter = quarter =>
|
|
|
|
quarter.reduce((acc, month) => acc + totalDaysInMonth(month), 0);
|
2018-06-07 12:08:27 -04:00
|
|
|
|
2018-02-03 03:04:02 -05:00
|
|
|
/**
|
|
|
|
* Returns list of Dates referring to Sundays of the month
|
|
|
|
* based on provided date
|
|
|
|
*
|
|
|
|
* @param {Date} date
|
|
|
|
*/
|
2018-05-22 13:41:53 -04:00
|
|
|
export const getSundays = date => {
|
2018-02-03 03:04:02 -05:00
|
|
|
if (!date) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-05-22 13:41:53 -04:00
|
|
|
const daysToSunday = [
|
2019-05-23 16:08:18 -04:00
|
|
|
__('Saturday'),
|
|
|
|
__('Friday'),
|
|
|
|
__('Thursday'),
|
|
|
|
__('Wednesday'),
|
|
|
|
__('Tuesday'),
|
|
|
|
__('Monday'),
|
|
|
|
__('Sunday'),
|
2018-05-22 13:41:53 -04:00
|
|
|
];
|
2018-02-03 03:04:02 -05:00
|
|
|
|
|
|
|
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);
|
2019-05-23 16:08:18 -04:00
|
|
|
if (dayName === __('Sunday')) {
|
2018-02-03 03:04:02 -05:00
|
|
|
sundays.push(new Date(dateOfMonth.getTime()));
|
|
|
|
}
|
|
|
|
|
|
|
|
const daysUntilNextSunday = daysToSunday.indexOf(dayName) + 1;
|
|
|
|
dateOfMonth.setDate(dateOfMonth.getDate() + daysUntilNextSunday);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sundays;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2018-06-07 12:08:27 -04:00
|
|
|
* Returns list of Dates representing a timeframe of months from startDate and length
|
2019-01-08 08:10:41 -05:00
|
|
|
* This method also supports going back in time when `length` is negative number
|
2018-02-03 03:04:02 -05:00
|
|
|
*
|
2019-01-08 08:10:41 -05:00
|
|
|
* @param {Date} initialStartDate
|
2018-02-03 03:04:02 -05:00
|
|
|
* @param {Number} length
|
|
|
|
*/
|
2019-01-08 08:10:41 -05:00
|
|
|
export const getTimeframeWindowFrom = (initialStartDate, length) => {
|
|
|
|
if (!(initialStartDate instanceof Date) || !length) {
|
2018-02-03 03:04:02 -05:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-01-08 08:10:41 -05:00
|
|
|
const startDate = newDate(initialStartDate);
|
|
|
|
const moveMonthBy = length > 0 ? 1 : -1;
|
|
|
|
|
|
|
|
startDate.setDate(1);
|
|
|
|
startDate.setHours(0, 0, 0, 0);
|
|
|
|
|
2018-06-07 12:08:27 -04:00
|
|
|
// Iterate and set date for the size of length
|
2018-02-03 03:04:02 -05:00
|
|
|
// and push date reference to timeframe list
|
2019-01-08 08:10:41 -05:00
|
|
|
const timeframe = new Array(Math.abs(length)).fill().map(() => {
|
|
|
|
const currentMonth = startDate.getTime();
|
|
|
|
startDate.setMonth(startDate.getMonth() + moveMonthBy);
|
|
|
|
return new Date(currentMonth);
|
|
|
|
});
|
2018-02-03 03:04:02 -05:00
|
|
|
|
|
|
|
// Change date of last timeframe item to last date of the month
|
2019-01-08 08:10:41 -05:00
|
|
|
// when length is positive
|
|
|
|
if (length > 0) {
|
|
|
|
timeframe[timeframe.length - 1].setDate(totalDaysInMonth(timeframe[timeframe.length - 1]));
|
|
|
|
}
|
2018-02-03 03:04:02 -05:00
|
|
|
|
|
|
|
return timeframe;
|
|
|
|
};
|
|
|
|
|
2018-06-07 12:08:27 -04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2018-07-02 03:46:40 -04:00
|
|
|
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-06-07 12:08:27 -04:00
|
|
|
|
2017-12-07 06:09:17 -05:00
|
|
|
window.gl = window.gl || {};
|
|
|
|
window.gl.utils = {
|
|
|
|
...(window.gl.utils || {}),
|
|
|
|
getTimeago,
|
|
|
|
localTimeAgo,
|
|
|
|
};
|
2018-10-01 09:21:33 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats milliseconds as timestamp (e.g. 01:02:03).
|
2018-10-04 08:53:15 -04:00
|
|
|
* This takes durations longer than a day into account (e.g. two days would be 48:00:00).
|
2018-10-01 09:21:33 -04: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;
|
|
|
|
};
|
2018-10-24 06:50:55 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats dates in Pickaday
|
|
|
|
* @param {String} dateString Date in yyyy-mm-dd format
|
|
|
|
* @return {Date} UTC format
|
|
|
|
*/
|
|
|
|
export const parsePikadayDate = dateString => {
|
|
|
|
const parts = dateString.split('-');
|
|
|
|
const year = parseInt(parts[0], 10);
|
|
|
|
const month = parseInt(parts[1] - 1, 10);
|
|
|
|
const day = parseInt(parts[2], 10);
|
|
|
|
|
|
|
|
return new Date(year, month, day);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used `onSelect` method in pickaday
|
|
|
|
* @param {Date} date UTC format
|
|
|
|
* @return {String} Date formated in yyyy-mm-dd
|
|
|
|
*/
|
|
|
|
export const pikadayToString = date => {
|
|
|
|
const day = pad(date.getDate());
|
|
|
|
const month = pad(date.getMonth() + 1);
|
|
|
|
const year = date.getFullYear();
|
|
|
|
|
|
|
|
return `${year}-${month}-${day}`;
|
|
|
|
};
|
2018-10-24 07:02:23 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Accepts seconds and returns a timeObject { weeks: #, days: #, hours: #, minutes: # }
|
|
|
|
* Seconds can be negative or positive, zero or non-zero. Can be configured for any day
|
|
|
|
* or week length.
|
|
|
|
*/
|
2019-06-11 05:48:42 -04:00
|
|
|
export const parseSeconds = (
|
|
|
|
seconds,
|
|
|
|
{ daysPerWeek = 5, hoursPerDay = 8, limitToHours = false } = {},
|
|
|
|
) => {
|
2018-10-24 07:02:23 -04:00
|
|
|
const DAYS_PER_WEEK = daysPerWeek;
|
|
|
|
const HOURS_PER_DAY = hoursPerDay;
|
2019-06-11 06:40:01 -04:00
|
|
|
const SECONDS_PER_MINUTE = 60;
|
2018-10-24 07:02:23 -04:00
|
|
|
const MINUTES_PER_HOUR = 60;
|
|
|
|
const MINUTES_PER_WEEK = DAYS_PER_WEEK * HOURS_PER_DAY * MINUTES_PER_HOUR;
|
|
|
|
const MINUTES_PER_DAY = HOURS_PER_DAY * MINUTES_PER_HOUR;
|
|
|
|
|
|
|
|
const timePeriodConstraints = {
|
|
|
|
weeks: MINUTES_PER_WEEK,
|
|
|
|
days: MINUTES_PER_DAY,
|
|
|
|
hours: MINUTES_PER_HOUR,
|
|
|
|
minutes: 1,
|
|
|
|
};
|
|
|
|
|
2019-06-11 06:40:01 -04:00
|
|
|
if (limitToHours) {
|
|
|
|
timePeriodConstraints.weeks = 0;
|
|
|
|
timePeriodConstraints.days = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let unorderedMinutes = Math.abs(seconds / SECONDS_PER_MINUTE);
|
2018-10-24 07:02:23 -04:00
|
|
|
|
|
|
|
return _.mapObject(timePeriodConstraints, minutesPerPeriod => {
|
2019-06-11 06:40:01 -04:00
|
|
|
if (minutesPerPeriod === 0) {
|
2019-06-11 05:48:42 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-24 07:02:23 -04:00
|
|
|
const periodCount = Math.floor(unorderedMinutes / minutesPerPeriod);
|
|
|
|
|
|
|
|
unorderedMinutes -= periodCount * minutesPerPeriod;
|
|
|
|
|
|
|
|
return periodCount;
|
|
|
|
});
|
2018-10-24 07:30:39 -04:00
|
|
|
};
|
2018-10-24 07:02:23 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Accepts a timeObject (see parseSeconds) and returns a condensed string representation of it
|
|
|
|
* (e.g. '1w 2d 3h 1m' or '1h 30m'). Zero value units are not included.
|
2018-11-07 12:20:17 -05:00
|
|
|
* If the 'fullNameFormat' param is passed it returns a non condensed string eg '1 week 3 days'
|
2018-10-24 07:02:23 -04:00
|
|
|
*/
|
2018-11-07 12:20:17 -05:00
|
|
|
export const stringifyTime = (timeObject, fullNameFormat = false) => {
|
2018-10-24 07:02:23 -04:00
|
|
|
const reducedTime = _.reduce(
|
|
|
|
timeObject,
|
|
|
|
(memo, unitValue, unitName) => {
|
2019-06-03 18:51:02 -04:00
|
|
|
const isNonZero = Boolean(unitValue);
|
2018-11-07 12:20:17 -05:00
|
|
|
|
|
|
|
if (fullNameFormat && isNonZero) {
|
|
|
|
// Remove traling 's' if unit value is singular
|
|
|
|
const formatedUnitName = unitValue > 1 ? unitName : unitName.replace(/s$/, '');
|
|
|
|
return `${memo} ${unitValue} ${formatedUnitName}`;
|
|
|
|
}
|
|
|
|
|
2018-10-24 07:02:23 -04:00
|
|
|
return isNonZero ? `${memo} ${unitValue}${unitName.charAt(0)}` : memo;
|
|
|
|
},
|
|
|
|
'',
|
|
|
|
).trim();
|
|
|
|
return reducedTime.length ? reducedTime : '0m';
|
2018-10-24 07:30:39 -04:00
|
|
|
};
|
2018-10-24 07:02:23 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Accepts a time string of any size (e.g. '1w 2d 3h 5m' or '1w 2d') and returns
|
|
|
|
* the first non-zero unit/value pair.
|
|
|
|
*/
|
2018-10-24 07:30:39 -04:00
|
|
|
export const abbreviateTime = timeStr =>
|
|
|
|
timeStr.split(' ').filter(unitStr => unitStr.charAt(0) !== '0')[0];
|
2018-10-19 13:53:41 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the milliseconds between now and a given date string.
|
|
|
|
* The result cannot become negative.
|
|
|
|
*
|
|
|
|
* @param endDate date string that the time difference is calculated for
|
|
|
|
* @return {number} number of milliseconds remaining until the given date
|
|
|
|
*/
|
|
|
|
export const calculateRemainingMilliseconds = endDate => {
|
|
|
|
const remainingMilliseconds = new Date(endDate).getTime() - Date.now();
|
|
|
|
return Math.max(remainingMilliseconds, 0);
|
|
|
|
};
|