duration.js
4.08 KB
!function(s,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):s.dayjs_plugin_duration=t()}(this,function(){"use strict";var s,t,i=/\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,n=/^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/,e={years:31536e6,months:2592e6,days:864e5,hours:36e5,minutes:6e4,seconds:1e3,milliseconds:1,weeks:6048e5},r=function(s){return s instanceof u},o=function(s,t,i){return new u(s,i,t.$l)},h=function(s){return t.p(s)+"s"},u=function(){function u(s,t,i){var r=this;if(this.$d={},this.$l=i,t)return o(s*e[h(t)],this);if("number"==typeof s)return this.$ms=s,this.parseFromMilliseconds(),this;if("object"==typeof s)return Object.keys(s).forEach(function(t){r.$d[h(t)]=s[t]}),this.calMilliseconds(),this;if("string"==typeof s){var u=s.match(n);if(u)return this.$d.years=u[2],this.$d.months=u[3],this.$d.weeks=u[4],this.$d.days=u[5],this.$d.hours=u[6],this.$d.minutes=u[7],this.$d.seconds=u[8],this.calMilliseconds(),this}return this}var d=u.prototype;return d.calMilliseconds=function(){var s=this;this.$ms=Object.keys(this.$d).reduce(function(t,i){return t+(s.$d[i]||0)*e[i]},0)},d.parseFromMilliseconds=function(){var s=this.$ms;this.$d.years=Math.floor(s/31536e6),s%=31536e6,this.$d.months=Math.floor(s/2592e6),s%=2592e6,this.$d.days=Math.floor(s/864e5),s%=864e5,this.$d.hours=Math.floor(s/36e5),s%=36e5,this.$d.minutes=Math.floor(s/6e4),s%=6e4,this.$d.seconds=Math.floor(s/1e3),s%=1e3,this.$d.milliseconds=s},d.toISOString=function(){var s=this.$d.years?this.$d.years+"Y":"",t=this.$d.months?this.$d.months+"M":"",i=+this.$d.days||0;this.$d.weeks&&(i+=7*this.$d.weeks);var n=i?i+"D":"",e=this.$d.hours?this.$d.hours+"H":"",r=this.$d.minutes?this.$d.minutes+"M":"",o=this.$d.seconds||0;this.$d.milliseconds&&(o+=this.$d.milliseconds/1e3);var h=o?o+"S":"",u="P"+s+t+n+(e||r||h?"T":"")+e+r+h;return"P"===u?"P0D":u},d.toJSON=function(){return this.toISOString()},d.format=function(s){var n=s||"YYYY-MM-DDTHH:mm:ss",e={Y:this.$d.years,YY:t.s(this.$d.years,2,"0"),YYYY:t.s(this.$d.years,4,"0"),M:this.$d.months,MM:t.s(this.$d.months,2,"0"),D:this.$d.days,DD:t.s(this.$d.days,2,"0"),H:this.$d.hours,HH:t.s(this.$d.hours,2,"0"),m:this.$d.minutes,mm:t.s(this.$d.minutes,2,"0"),s:this.$d.seconds,ss:t.s(this.$d.seconds,2,"0"),SSS:t.s(this.$d.milliseconds,3,"0")};return n.replace(i,function(s,t){return t||String(e[s])})},d.as=function(s){return this.$ms/e[h(s)]},d.get=function(s){var t=this.$ms,i=h(s);return"milliseconds"===i?t%=1e3:t="weeks"===i?Math.floor(t/e[i]):this.$d[i],t},d.add=function(s,t,i){var n;return n=t?s*e[h(t)]:r(s)?s.$ms:o(s,this).$ms,o(this.$ms+n*(i?-1:1),this)},d.subtract=function(s,t){return this.add(s,t,!0)},d.locale=function(s){var t=this.clone();return t.$l=s,t},d.clone=function(){return o(this.$ms,this)},d.humanize=function(t){return s().add(this.$ms,"ms").locale(this.$l).fromNow(!t)},d.milliseconds=function(){return this.get("milliseconds")},d.asMilliseconds=function(){return this.as("milliseconds")},d.seconds=function(){return this.get("seconds")},d.asSeconds=function(){return this.as("seconds")},d.minutes=function(){return this.get("minutes")},d.asMinutes=function(){return this.as("minutes")},d.hours=function(){return this.get("hours")},d.asHours=function(){return this.as("hours")},d.days=function(){return this.get("days")},d.asDays=function(){return this.as("days")},d.weeks=function(){return this.get("weeks")},d.asWeeks=function(){return this.as("weeks")},d.months=function(){return this.get("months")},d.asMonths=function(){return this.as("months")},d.years=function(){return this.get("years")},d.asYears=function(){return this.as("years")},u}();return function(i,n,e){s=e,t=e().$utils(),e.duration=function(s,t){var i=e.locale();return o(s,{$l:i},t)},e.isDuration=r;var h=n.prototype.add,u=n.prototype.subtract;n.prototype.add=function(s,t){return r(s)&&(s=s.asMilliseconds()),h.bind(this)(s,t)},n.prototype.subtract=function(s,t){return r(s)&&(s=s.asMilliseconds()),u.bind(this)(s,t)}}});