智慧水务管理系统 - 精河县供水工程综合管理平台

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import { MILLISECONDS_A_DAY, MILLISECONDS_A_HOUR, MILLISECONDS_A_MINUTE, MILLISECONDS_A_SECOND, MILLISECONDS_A_WEEK } from '../../constant';
  2. var MILLISECONDS_A_YEAR = MILLISECONDS_A_DAY * 365;
  3. var MILLISECONDS_A_MONTH = MILLISECONDS_A_YEAR / 12;
  4. var DURATION_REGEX_PARSE = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/;
  5. var DURATION_REGEX_FORMAT = /\[([^\]]+)]|YYYY|YY|Y|M{1,2}|D{1,2}|H{1,2}|m{1,2}|s{1,2}|SSS/g;
  6. var unitToMS = {
  7. years: MILLISECONDS_A_YEAR,
  8. months: MILLISECONDS_A_MONTH,
  9. days: MILLISECONDS_A_DAY,
  10. hours: MILLISECONDS_A_HOUR,
  11. minutes: MILLISECONDS_A_MINUTE,
  12. seconds: MILLISECONDS_A_SECOND,
  13. milliseconds: 1,
  14. weeks: MILLISECONDS_A_WEEK
  15. };
  16. var isDuration = function isDuration(d) {
  17. return d instanceof Duration;
  18. }; // eslint-disable-line no-use-before-define
  19. var $d;
  20. var $u;
  21. var wrapper = function wrapper(input, instance, unit) {
  22. return new Duration(input, unit, instance.$l);
  23. }; // eslint-disable-line no-use-before-define
  24. var prettyUnit = function prettyUnit(unit) {
  25. return $u.p(unit) + "s";
  26. };
  27. var isNegative = function isNegative(number) {
  28. return number < 0;
  29. };
  30. var roundNumber = function roundNumber(number) {
  31. return isNegative(number) ? Math.ceil(number) : Math.floor(number);
  32. };
  33. var absolute = function absolute(number) {
  34. return Math.abs(number);
  35. };
  36. var getNumberUnitFormat = function getNumberUnitFormat(number, unit) {
  37. if (!number) {
  38. return {
  39. negative: false,
  40. format: ''
  41. };
  42. }
  43. if (isNegative(number)) {
  44. return {
  45. negative: true,
  46. format: "" + absolute(number) + unit
  47. };
  48. }
  49. return {
  50. negative: false,
  51. format: "" + number + unit
  52. };
  53. };
  54. var Duration = /*#__PURE__*/function () {
  55. function Duration(input, unit, locale) {
  56. var _this = this;
  57. this.$d = {};
  58. this.$l = locale;
  59. if (input === undefined) {
  60. this.$ms = 0;
  61. this.parseFromMilliseconds();
  62. }
  63. if (unit) {
  64. return wrapper(input * unitToMS[prettyUnit(unit)], this);
  65. }
  66. if (typeof input === 'number') {
  67. this.$ms = input;
  68. this.parseFromMilliseconds();
  69. return this;
  70. }
  71. if (typeof input === 'object') {
  72. Object.keys(input).forEach(function (k) {
  73. _this.$d[prettyUnit(k)] = input[k];
  74. });
  75. this.calMilliseconds();
  76. return this;
  77. }
  78. if (typeof input === 'string') {
  79. var d = input.match(DURATION_REGEX_PARSE);
  80. if (d) {
  81. var properties = d.slice(2);
  82. var numberD = properties.map(function (value) {
  83. return value != null ? Number(value) : 0;
  84. });
  85. this.$d.years = numberD[0];
  86. this.$d.months = numberD[1];
  87. this.$d.weeks = numberD[2];
  88. this.$d.days = numberD[3];
  89. this.$d.hours = numberD[4];
  90. this.$d.minutes = numberD[5];
  91. this.$d.seconds = numberD[6];
  92. this.calMilliseconds();
  93. return this;
  94. }
  95. }
  96. return this;
  97. }
  98. var _proto = Duration.prototype;
  99. _proto.calMilliseconds = function calMilliseconds() {
  100. var _this2 = this;
  101. this.$ms = Object.keys(this.$d).reduce(function (total, unit) {
  102. return total + (_this2.$d[unit] || 0) * unitToMS[unit];
  103. }, 0);
  104. };
  105. _proto.parseFromMilliseconds = function parseFromMilliseconds() {
  106. var $ms = this.$ms;
  107. this.$d.years = roundNumber($ms / MILLISECONDS_A_YEAR);
  108. $ms %= MILLISECONDS_A_YEAR;
  109. this.$d.months = roundNumber($ms / MILLISECONDS_A_MONTH);
  110. $ms %= MILLISECONDS_A_MONTH;
  111. this.$d.days = roundNumber($ms / MILLISECONDS_A_DAY);
  112. $ms %= MILLISECONDS_A_DAY;
  113. this.$d.hours = roundNumber($ms / MILLISECONDS_A_HOUR);
  114. $ms %= MILLISECONDS_A_HOUR;
  115. this.$d.minutes = roundNumber($ms / MILLISECONDS_A_MINUTE);
  116. $ms %= MILLISECONDS_A_MINUTE;
  117. this.$d.seconds = roundNumber($ms / MILLISECONDS_A_SECOND);
  118. $ms %= MILLISECONDS_A_SECOND;
  119. this.$d.milliseconds = $ms;
  120. };
  121. _proto.toISOString = function toISOString() {
  122. var Y = getNumberUnitFormat(this.$d.years, 'Y');
  123. var M = getNumberUnitFormat(this.$d.months, 'M');
  124. var days = +this.$d.days || 0;
  125. if (this.$d.weeks) {
  126. days += this.$d.weeks * 7;
  127. }
  128. var D = getNumberUnitFormat(days, 'D');
  129. var H = getNumberUnitFormat(this.$d.hours, 'H');
  130. var m = getNumberUnitFormat(this.$d.minutes, 'M');
  131. var seconds = this.$d.seconds || 0;
  132. if (this.$d.milliseconds) {
  133. seconds += this.$d.milliseconds / 1000;
  134. seconds = Math.round(seconds * 1000) / 1000;
  135. }
  136. var S = getNumberUnitFormat(seconds, 'S');
  137. var negativeMode = Y.negative || M.negative || D.negative || H.negative || m.negative || S.negative;
  138. var T = H.format || m.format || S.format ? 'T' : '';
  139. var P = negativeMode ? '-' : '';
  140. var result = P + "P" + Y.format + M.format + D.format + T + H.format + m.format + S.format;
  141. return result === 'P' || result === '-P' ? 'P0D' : result;
  142. };
  143. _proto.toJSON = function toJSON() {
  144. return this.toISOString();
  145. };
  146. _proto.format = function format(formatStr) {
  147. var str = formatStr || 'YYYY-MM-DDTHH:mm:ss';
  148. var matches = {
  149. Y: this.$d.years,
  150. YY: $u.s(this.$d.years, 2, '0'),
  151. YYYY: $u.s(this.$d.years, 4, '0'),
  152. M: this.$d.months,
  153. MM: $u.s(this.$d.months, 2, '0'),
  154. D: this.$d.days,
  155. DD: $u.s(this.$d.days, 2, '0'),
  156. H: this.$d.hours,
  157. HH: $u.s(this.$d.hours, 2, '0'),
  158. m: this.$d.minutes,
  159. mm: $u.s(this.$d.minutes, 2, '0'),
  160. s: this.$d.seconds,
  161. ss: $u.s(this.$d.seconds, 2, '0'),
  162. SSS: $u.s(this.$d.milliseconds, 3, '0')
  163. };
  164. return str.replace(DURATION_REGEX_FORMAT, function (match, $1) {
  165. return $1 || String(matches[match]);
  166. });
  167. };
  168. _proto.as = function as(unit) {
  169. return this.$ms / unitToMS[prettyUnit(unit)];
  170. };
  171. _proto.get = function get(unit) {
  172. var base = this.$ms;
  173. var pUnit = prettyUnit(unit);
  174. if (pUnit === 'milliseconds') {
  175. base %= 1000;
  176. } else if (pUnit === 'weeks') {
  177. base = roundNumber(base / unitToMS[pUnit]);
  178. } else {
  179. base = this.$d[pUnit];
  180. }
  181. return base || 0; // a === 0 will be true on both 0 and -0
  182. };
  183. _proto.add = function add(input, unit, isSubtract) {
  184. var another;
  185. if (unit) {
  186. another = input * unitToMS[prettyUnit(unit)];
  187. } else if (isDuration(input)) {
  188. another = input.$ms;
  189. } else {
  190. another = wrapper(input, this).$ms;
  191. }
  192. return wrapper(this.$ms + another * (isSubtract ? -1 : 1), this);
  193. };
  194. _proto.subtract = function subtract(input, unit) {
  195. return this.add(input, unit, true);
  196. };
  197. _proto.locale = function locale(l) {
  198. var that = this.clone();
  199. that.$l = l;
  200. return that;
  201. };
  202. _proto.clone = function clone() {
  203. return wrapper(this.$ms, this);
  204. };
  205. _proto.humanize = function humanize(withSuffix) {
  206. return $d().add(this.$ms, 'ms').locale(this.$l).fromNow(!withSuffix);
  207. };
  208. _proto.valueOf = function valueOf() {
  209. return this.asMilliseconds();
  210. };
  211. _proto.milliseconds = function milliseconds() {
  212. return this.get('milliseconds');
  213. };
  214. _proto.asMilliseconds = function asMilliseconds() {
  215. return this.as('milliseconds');
  216. };
  217. _proto.seconds = function seconds() {
  218. return this.get('seconds');
  219. };
  220. _proto.asSeconds = function asSeconds() {
  221. return this.as('seconds');
  222. };
  223. _proto.minutes = function minutes() {
  224. return this.get('minutes');
  225. };
  226. _proto.asMinutes = function asMinutes() {
  227. return this.as('minutes');
  228. };
  229. _proto.hours = function hours() {
  230. return this.get('hours');
  231. };
  232. _proto.asHours = function asHours() {
  233. return this.as('hours');
  234. };
  235. _proto.days = function days() {
  236. return this.get('days');
  237. };
  238. _proto.asDays = function asDays() {
  239. return this.as('days');
  240. };
  241. _proto.weeks = function weeks() {
  242. return this.get('weeks');
  243. };
  244. _proto.asWeeks = function asWeeks() {
  245. return this.as('weeks');
  246. };
  247. _proto.months = function months() {
  248. return this.get('months');
  249. };
  250. _proto.asMonths = function asMonths() {
  251. return this.as('months');
  252. };
  253. _proto.years = function years() {
  254. return this.get('years');
  255. };
  256. _proto.asYears = function asYears() {
  257. return this.as('years');
  258. };
  259. return Duration;
  260. }();
  261. var manipulateDuration = function manipulateDuration(date, duration, k) {
  262. return date.add(duration.years() * k, 'y').add(duration.months() * k, 'M').add(duration.days() * k, 'd').add(duration.hours() * k, 'h').add(duration.minutes() * k, 'm').add(duration.seconds() * k, 's').add(duration.milliseconds() * k, 'ms');
  263. };
  264. export default (function (option, Dayjs, dayjs) {
  265. $d = dayjs;
  266. $u = dayjs().$utils();
  267. dayjs.duration = function (input, unit) {
  268. var $l = dayjs.locale();
  269. return wrapper(input, {
  270. $l: $l
  271. }, unit);
  272. };
  273. dayjs.isDuration = isDuration;
  274. var oldAdd = Dayjs.prototype.add;
  275. var oldSubtract = Dayjs.prototype.subtract;
  276. Dayjs.prototype.add = function (value, unit) {
  277. if (isDuration(value)) {
  278. return manipulateDuration(this, value, 1);
  279. }
  280. return oldAdd.bind(this)(value, unit);
  281. };
  282. Dayjs.prototype.subtract = function (value, unit) {
  283. if (isDuration(value)) {
  284. return manipulateDuration(this, value, -1);
  285. }
  286. return oldSubtract.bind(this)(value, unit);
  287. };
  288. });