datetime_utility.js 10.1 KB
Newer Older
1
import $ from 'jquery';
2 3
import timeago from 'timeago.js';
import dateFormat from 'vendor/date.format';
F
Filipa Lacerda 已提交
4
import { pluralize } from './text_utility';
5
import { languageCode, s__ } from '../../locale';
6

7 8
window.timeago = timeago;
window.dateFormat = dateFormat;
P
Phil Hughes 已提交
9

K
Kushal Pandya 已提交
10 11 12 13 14 15 16
/**
 * Returns i18n month names array.
 * If `abbreviated` is provided, returns abbreviated
 * name.
 *
 * @param {Boolean} abbreviated
 */
17
const getMonthNames = abbreviated => {
K
Kushal Pandya 已提交
18
  if (abbreviated) {
19 20 21 22 23 24 25 26 27 28 29 30 31 32
    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'),
    ];
K
Kushal Pandya 已提交
33
  }
34 35 36 37 38 39 40 41 42 43 44 45 46 47
  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'),
  ];
K
Kushal Pandya 已提交
48 49
};

50 51 52 53 54
/**
 * Given a date object returns the day of the week in English
 * @param {date} date
 * @returns {String}
 */
55 56
export const getDayName = date =>
  ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];
P
Phil Hughes 已提交
57

58 59 60 61 62 63 64
/**
 * @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');
P
Phil Hughes 已提交
65

66 67 68 69 70 71 72
/**
 * 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, '_');

73
let timeagoInstance;
74

75 76 77 78 79 80 81
/**
 * Sets a timeago Instance
 */
export function getTimeago() {
  if (!timeagoInstance) {
    const localeRemaining = function getLocaleRemaining(number, index) {
      return [
82
        [s__('Timeago|less than a minute ago'), s__('Timeago|right now')],
83 84 85 86 87 88 89 90 91 92 93 94 95 96
        [s__('Timeago|less than a minute ago'), s__('Timeago|%s seconds remaining')],
        [s__('Timeago|about a minute ago'), s__('Timeago|1 minute remaining')],
        [s__('Timeago|%s minutes ago'), s__('Timeago|%s minutes remaining')],
        [s__('Timeago|about an hour ago'), s__('Timeago|1 hour remaining')],
        [s__('Timeago|about %s hours ago'), s__('Timeago|%s hours remaining')],
        [s__('Timeago|a day ago'), s__('Timeago|1 day remaining')],
        [s__('Timeago|%s days ago'), s__('Timeago|%s days remaining')],
        [s__('Timeago|a week ago'), s__('Timeago|1 week remaining')],
        [s__('Timeago|%s weeks ago'), s__('Timeago|%s weeks remaining')],
        [s__('Timeago|a month ago'), s__('Timeago|1 month remaining')],
        [s__('Timeago|%s months ago'), s__('Timeago|%s months remaining')],
        [s__('Timeago|a year ago'), s__('Timeago|1 year remaining')],
        [s__('Timeago|%s years ago'), s__('Timeago|%s years remaining')],
      ][index];
P
Phil Hughes 已提交
97
    };
98 99
    const locale = function getLocale(number, index) {
      return [
100
        [s__('Timeago|less than a minute ago'), s__('Timeago|right now')],
101 102 103 104 105 106 107 108 109 110 111 112 113 114
        [s__('Timeago|less than a minute ago'), s__('Timeago|in %s seconds')],
        [s__('Timeago|about a minute ago'), s__('Timeago|in 1 minute')],
        [s__('Timeago|%s minutes ago'), s__('Timeago|in %s minutes')],
        [s__('Timeago|about an hour ago'), s__('Timeago|in 1 hour')],
        [s__('Timeago|about %s hours ago'), s__('Timeago|in %s hours')],
        [s__('Timeago|a day ago'), s__('Timeago|in 1 day')],
        [s__('Timeago|%s days ago'), s__('Timeago|in %s days')],
        [s__('Timeago|a week ago'), s__('Timeago|in 1 week')],
        [s__('Timeago|%s weeks ago'), s__('Timeago|in %s weeks')],
        [s__('Timeago|a month ago'), s__('Timeago|in 1 month')],
        [s__('Timeago|%s months ago'), s__('Timeago|in %s months')],
        [s__('Timeago|a year ago'), s__('Timeago|in 1 year')],
        [s__('Timeago|%s years ago'), s__('Timeago|in %s years')],
      ][index];
P
Phil Hughes 已提交
115 116
    };

117 118
    timeago.register(timeagoLanguageCode, locale);
    timeago.register(`${timeagoLanguageCode}-remaining`, localeRemaining);
119 120
    timeagoInstance = timeago();
  }
P
Phil Hughes 已提交
121

122 123
  return timeagoInstance;
}
P
Phil Hughes 已提交
124

125 126 127 128
/**
 * For the given element, renders a timeago instance.
 * @param {jQuery} $els
 */
129
export const renderTimeago = $els => {
130
  const timeagoEls = $els || document.querySelectorAll('.js-timeago-render');
P
Phil Hughes 已提交
131

132
  // timeago.js sets timeouts internally for each timeago value to be updated in real time
133
  getTimeago().render(timeagoEls, timeagoLanguageCode);
134
};
P
Phil Hughes 已提交
135

136 137 138 139 140 141 142 143 144 145
/**
 * 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({
C
Clement Ho 已提交
146
        template: '<div class="tooltip local-timeago" role="tooltip"><div class="arrow"></div><div class="tooltip-inner"></div></div>',
147 148
      });
    }
P
Phil Hughes 已提交
149

150 151
    el.classList.add('js-timeago-render');
  });
P
Phil Hughes 已提交
152

153 154
  renderTimeago($timeagoEls);
};
P
Phil Hughes 已提交
155

156 157 158 159 160 161 162 163 164 165 166 167
/**
 * 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');
  }
168 169 170
  return getTimeago()
    .format(time, `${timeagoLanguageCode}-remaining`)
    .trim();
171
};
P
Phil Hughes 已提交
172

173 174 175 176
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());
P
Phil Hughes 已提交
177

178 179
  return Math.floor((date2 - date1) / millisecondsPerDay);
};
180 181 182 183 184 185 186 187 188 189

/**
 * 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);
190
  const seconds = secondsInteger - minutes * 60;
191 192 193
  let text = '';

  if (minutes >= 1) {
F
Filipa Lacerda 已提交
194
    text = `${minutes} ${pluralize('minute', minutes)} ${seconds} ${pluralize('second', seconds)}`;
195
  } else {
F
Filipa Lacerda 已提交
196
    text = `${seconds} ${pluralize('second', seconds)}`;
197 198 199
  }
  return text;
}
C
Clement Ho 已提交
200

K
Kushal Pandya 已提交
201
export function dateInWords(date, abbreviated = false, hideYear = false) {
C
Clement Ho 已提交
202 203 204 205 206
  if (!date) return date;

  const month = date.getMonth();
  const year = date.getFullYear();

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  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'),
  ];
C
Clement Ho 已提交
235 236 237

  const monthName = abbreviated ? monthNamesAbbr[month] : monthNames[month];

K
Kushal Pandya 已提交
238 239 240 241
  if (hideYear) {
    return `${monthName} ${date.getDate()}`;
  }

C
Clement Ho 已提交
242 243
  return `${monthName} ${date.getDate()}, ${year}`;
}
244

K
Kushal Pandya 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
/**
 * 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
 */
265
export const totalDaysInMonth = date => {
K
Kushal Pandya 已提交
266 267 268 269 270 271
  if (!date) {
    return 0;
  }
  return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
};

K
Kushal Pandya 已提交
272 273 274 275 276 277 278 279 280 281 282
/**
 * 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,
);

K
Kushal Pandya 已提交
283 284 285 286 287 288
/**
 * Returns list of Dates referring to Sundays of the month
 * based on provided date
 *
 * @param {Date} date
 */
289
export const getSundays = date => {
K
Kushal Pandya 已提交
290 291 292 293
  if (!date) {
    return [];
  }

294 295 296 297 298 299 300 301 302
  const daysToSunday = [
    'Saturday',
    'Friday',
    'Thursday',
    'Wednesday',
    'Tuesday',
    'Monday',
    'Sunday',
  ];
K
Kushal Pandya 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

  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;
};

/**
K
Kushal Pandya 已提交
323
 * Returns list of Dates representing a timeframe of months from startDate and length
K
Kushal Pandya 已提交
324
 *
K
Kushal Pandya 已提交
325
 * @param {Date} startDate
K
Kushal Pandya 已提交
326 327
 * @param {Number} length
 */
K
Kushal Pandya 已提交
328 329
export const getTimeframeWindowFrom = (startDate, length) => {
  if (!(startDate instanceof Date) || !length) {
K
Kushal Pandya 已提交
330 331 332
    return [];
  }

K
Kushal Pandya 已提交
333
  // Iterate and set date for the size of length
K
Kushal Pandya 已提交
334
  // and push date reference to timeframe list
K
Kushal Pandya 已提交
335 336 337 338 339 340 341 342 343
  const timeframe = new Array(length)
                      .fill()
                      .map(
                        (val, i) => new Date(
                          startDate.getFullYear(),
                          startDate.getMonth() + i,
                          1,
                        ),
                      );
K
Kushal Pandya 已提交
344 345 346 347 348 349 350

  // Change date of last timeframe item to last date of the month
  timeframe[length - 1].setDate(totalDaysInMonth(timeframe[length - 1]));

  return timeframe;
};

K
Kushal Pandya 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
/**
 * 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);

374 375 376 377 378 379
window.gl = window.gl || {};
window.gl.utils = {
  ...(window.gl.utils || {}),
  getTimeago,
  localTimeAgo,
};