123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- import { MILLISECONDS_A_DAY, MILLISECONDS_A_HOUR, MILLISECONDS_A_MINUTE, MILLISECONDS_A_SECOND, MILLISECONDS_A_WEEK } from '../../constant';
- var MILLISECONDS_A_YEAR = MILLISECONDS_A_DAY * 365;
- var MILLISECONDS_A_MONTH = MILLISECONDS_A_DAY * 30;
- var durationRegex = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/;
- var unitToMS = {
- years: MILLISECONDS_A_YEAR,
- months: MILLISECONDS_A_MONTH,
- days: MILLISECONDS_A_DAY,
- hours: MILLISECONDS_A_HOUR,
- minutes: MILLISECONDS_A_MINUTE,
- seconds: MILLISECONDS_A_SECOND,
- milliseconds: 1,
- weeks: MILLISECONDS_A_WEEK
- };
- var isDuration = function isDuration(d) {
- return d instanceof Duration;
- }; // eslint-disable-line no-use-before-define
- var $d;
- var $u;
- var wrapper = function wrapper(input, instance, unit) {
- return new Duration(input, unit, instance.$l);
- }; // eslint-disable-line no-use-before-define
- var prettyUnit = function prettyUnit(unit) {
- return $u.p(unit) + "s";
- };
- var Duration = /*#__PURE__*/function () {
- function Duration(input, unit, locale) {
- var _this = this;
- this.$d = {};
- this.$l = locale;
- if (unit) {
- return wrapper(input * unitToMS[prettyUnit(unit)], this);
- }
- if (typeof input === 'number') {
- this.$ms = input;
- this.parseFromMilliseconds();
- return this;
- }
- if (typeof input === 'object') {
- Object.keys(input).forEach(function (k) {
- _this.$d[prettyUnit(k)] = input[k];
- });
- this.calMilliseconds();
- return this;
- }
- if (typeof input === 'string') {
- var d = input.match(durationRegex);
- if (d) {
- this.$d.years = d[2];
- this.$d.months = d[3];
- this.$d.weeks = d[4];
- this.$d.days = d[5];
- this.$d.hours = d[6];
- this.$d.minutes = d[7];
- this.$d.seconds = d[8];
- this.calMilliseconds();
- return this;
- }
- }
- return this;
- }
- var _proto = Duration.prototype;
- _proto.calMilliseconds = function calMilliseconds() {
- var _this2 = this;
- this.$ms = Object.keys(this.$d).reduce(function (total, unit) {
- return total + (_this2.$d[unit] || 0) * (unitToMS[unit] || 1);
- }, 0);
- };
- _proto.parseFromMilliseconds = function parseFromMilliseconds() {
- var $ms = this.$ms;
- this.$d.years = Math.floor($ms / MILLISECONDS_A_YEAR);
- $ms %= MILLISECONDS_A_YEAR;
- this.$d.months = Math.floor($ms / MILLISECONDS_A_MONTH);
- $ms %= MILLISECONDS_A_MONTH;
- this.$d.days = Math.floor($ms / MILLISECONDS_A_DAY);
- $ms %= MILLISECONDS_A_DAY;
- this.$d.hours = Math.floor($ms / MILLISECONDS_A_HOUR);
- $ms %= MILLISECONDS_A_HOUR;
- this.$d.minutes = Math.floor($ms / MILLISECONDS_A_MINUTE);
- $ms %= MILLISECONDS_A_MINUTE;
- this.$d.seconds = Math.floor($ms / MILLISECONDS_A_SECOND);
- $ms %= MILLISECONDS_A_SECOND;
- this.$d.milliseconds = $ms;
- };
- _proto.toISOString = function toISOString() {
- var Y = this.$d.years ? this.$d.years + "Y" : '';
- var M = this.$d.months ? this.$d.months + "M" : '';
- var days = +this.$d.days || 0;
- if (this.$d.weeks) {
- days += this.$d.weeks * 7;
- }
- var D = days ? days + "D" : '';
- var H = this.$d.hours ? this.$d.hours + "H" : '';
- var m = this.$d.minutes ? this.$d.minutes + "M" : '';
- var seconds = this.$d.seconds || 0;
- if (this.$d.milliseconds) {
- seconds += this.$d.milliseconds / 1000;
- }
- var S = seconds ? seconds + "S" : '';
- var T = H || m || S ? 'T' : '';
- var result = "P" + Y + M + D + T + H + m + S;
- return result === 'P' ? 'P0D' : result;
- };
- _proto.toJSON = function toJSON() {
- return this.toISOString();
- };
- _proto.as = function as(unit) {
- return this.$ms / (unitToMS[prettyUnit(unit)] || 1);
- };
- _proto.get = function get(unit) {
- var base = this.$ms;
- var pUnit = prettyUnit(unit);
- if (pUnit === 'milliseconds') {
- base %= 1000;
- } else if (pUnit === 'weeks') {
- base = Math.floor(base / unitToMS[pUnit]);
- } else {
- base = this.$d[pUnit];
- }
- return base;
- };
- _proto.add = function add(input, unit, isSubtract) {
- var another;
- if (unit) {
- another = input * unitToMS[prettyUnit(unit)];
- } else if (isDuration(input)) {
- another = input.$ms;
- } else {
- another = wrapper(input, this).$ms;
- }
- return wrapper(this.$ms + another * (isSubtract ? -1 : 1), this);
- };
- _proto.subtract = function subtract(input, unit) {
- return this.add(input, unit, true);
- };
- _proto.locale = function locale(l) {
- var that = this.clone();
- that.$l = l;
- return that;
- };
- _proto.clone = function clone() {
- return wrapper(this.$ms, this);
- };
- _proto.humanize = function humanize(withSuffix) {
- return $d().add(this.$ms, 'ms').locale(this.$l).fromNow(!withSuffix);
- };
- _proto.milliseconds = function milliseconds() {
- return this.get('milliseconds');
- };
- _proto.asMilliseconds = function asMilliseconds() {
- return this.as('milliseconds');
- };
- _proto.seconds = function seconds() {
- return this.get('seconds');
- };
- _proto.asSeconds = function asSeconds() {
- return this.as('seconds');
- };
- _proto.minutes = function minutes() {
- return this.get('minutes');
- };
- _proto.asMinutes = function asMinutes() {
- return this.as('minutes');
- };
- _proto.hours = function hours() {
- return this.get('hours');
- };
- _proto.asHours = function asHours() {
- return this.as('hours');
- };
- _proto.days = function days() {
- return this.get('days');
- };
- _proto.asDays = function asDays() {
- return this.as('days');
- };
- _proto.weeks = function weeks() {
- return this.get('weeks');
- };
- _proto.asWeeks = function asWeeks() {
- return this.as('weeks');
- };
- _proto.months = function months() {
- return this.get('months');
- };
- _proto.asMonths = function asMonths() {
- return this.as('months');
- };
- _proto.years = function years() {
- return this.get('years');
- };
- _proto.asYears = function asYears() {
- return this.as('years');
- };
- return Duration;
- }();
- export default (function (option, Dayjs, dayjs) {
- $d = dayjs;
- $u = dayjs().$utils();
- dayjs.duration = function (input, unit) {
- var $l = dayjs.locale();
- return wrapper(input, {
- $l: $l
- }, unit);
- };
- dayjs.isDuration = isDuration;
- });
|