ngx-bootstrap
Version:
Native Angular Bootstrap Components
95 lines • 11.3 kB
JavaScript
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
import { cloneWithOffset, getDateOffset, getUTCOffset } from '../units/offset';
import { absFloor } from '../utils';
import { isDateValid, isNumber } from '../utils/type-checks';
import { getFullYear, getMonth } from '../utils/date-getters';
import { add } from './add-subtract';
import { cloneDate } from '../create/clone';
/**
* @param {?} date
* @param {?} input
* @param {?} units
* @param {?} asFloat
* @param {?=} config
* @return {?}
*/
export function diff(date, input, units, asFloat, config = {}) {
if (!isDateValid(date)) {
return NaN;
}
/** @type {?} */
const that = cloneWithOffset(input, date, config);
if (!isDateValid(that)) {
return NaN;
}
/** @type {?} */
const zoneOffset = (getDateOffset(input) - getDateOffset(date)) * 6e4;
/** @type {?} */
const zoneDelta = isNumber(config._zoneDelta)
? config._zoneDelta * 6e4
: (getUTCOffset(input, config) - getUTCOffset(date, config)) * 6e4;
/** @type {?} */
let output;
switch (units) {
case 'year':
output = monthDiff(date, that) / 12;
break;
case 'month':
output = monthDiff(date, that);
break;
case 'quarter':
output = monthDiff(date, that) / 3;
break;
case 'seconds':
output = (date.valueOf() - that.valueOf()) / 1e3;
break; // 1000
case 'minutes':
output = (date.valueOf() - that.valueOf()) / 6e4;
break; // 1000 * 60
case 'hours':
output = (date.valueOf() - that.valueOf()) / 36e5;
break; // 1000 * 60 * 60
case 'day':
output = (date.valueOf() - that.valueOf() - (zoneDelta === 0 ? zoneOffset : zoneDelta)) / 864e5;
break; // 1000 * 60 * 60 * 24, negate dst
case 'week':
output = (date.valueOf() - that.valueOf() - zoneDelta) / 6048e5;
break; // 1000 * 60 * 60 * 24 * 7, negate dst
default:
output = date.valueOf() - that.valueOf();
}
return asFloat ? output : absFloor(output);
}
/**
* @param {?} a
* @param {?} b
* @return {?}
*/
function monthDiff(a, b) {
// difference in months
/** @type {?} */
const wholeMonthDiff = ((getFullYear(b) - getFullYear(a)) * 12) + (getMonth(b) - getMonth(a));
// b is in (anchor - 1 month, anchor + 1 month)
/** @type {?} */
const anchor = add(cloneDate(a), wholeMonthDiff, 'month');
/** @type {?} */
let anchor2;
/** @type {?} */
let adjust;
if (b.valueOf() - anchor.valueOf() < 0) {
anchor2 = add(cloneDate(a), wholeMonthDiff - 1, 'month');
// linear across the month
adjust = (b.valueOf() - anchor.valueOf()) / (anchor.valueOf() - anchor2.valueOf());
}
else {
anchor2 = add(cloneDate(a), wholeMonthDiff + 1, 'month');
// linear across the month
adjust = (b.valueOf() - anchor.valueOf()) / (anchor2.valueOf() - anchor.valueOf());
}
// check for negative zero, return zero if negative zero
return -(wholeMonthDiff + adjust) || 0;
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGlmZi5qcyIsInNvdXJjZVJvb3QiOiJuZzovL25neC1ib290c3RyYXAvY2hyb25vcy8iLCJzb3VyY2VzIjpbIm1vbWVudC9kaWZmLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7QUFBQSxPQUFPLEVBQUUsZUFBZSxFQUFFLGFBQWEsRUFBRSxZQUFZLEVBQUUsTUFBTSxpQkFBaUIsQ0FBQztBQUcvRSxPQUFPLEVBQUUsUUFBUSxFQUFFLE1BQU0sVUFBVSxDQUFDO0FBQ3BDLE9BQU8sRUFBRSxXQUFXLEVBQUUsUUFBUSxFQUFFLE1BQU0sc0JBQXNCLENBQUM7QUFDN0QsT0FBTyxFQUFFLFdBQVcsRUFBRSxRQUFRLEVBQUUsTUFBTSx1QkFBdUIsQ0FBQztBQUM5RCxPQUFPLEVBQUUsR0FBRyxFQUFFLE1BQU0sZ0JBQWdCLENBQUM7QUFDckMsT0FBTyxFQUFFLFNBQVMsRUFBRSxNQUFNLGlCQUFpQixDQUFDOzs7Ozs7Ozs7QUFFNUMsTUFBTSxVQUFVLElBQUksQ0FBQyxJQUFVLEVBQUUsS0FBVyxF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