edf40f372863bdfa6193fab56b6ff5459da6502194893ab888f0946eb78459146947eb86162cbae54033ad55b1315185901846eaed669382b69a98a0848c84 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { Duration, isDuration } from './constructor';
  2. import toInt from '../utils/to-int';
  3. import hasOwnProp from '../utils/has-own-prop';
  4. import { DATE, HOUR, MINUTE, SECOND, MILLISECOND } from '../units/constants';
  5. import { cloneWithOffset } from '../units/offset';
  6. import { createLocal } from '../create/local';
  7. // ASP.NET json date format regex
  8. var aspNetRegex = /^(\-)?(?:(\d*)[. ])?(\d+)\:(\d+)(?:\:(\d+)\.?(\d{3})?\d*)?$/;
  9. // from http://docs.closure-library.googlecode.com/git/closure_goog_date_date.js.source.html
  10. // somewhat more in line with 4.4.3.2 2004 spec, but allows decimal anywhere
  11. // and further modified to allow for strings containing both week and day
  12. var isoRegex = /^(-)?P(?:(-?[0-9,.]*)Y)?(?:(-?[0-9,.]*)M)?(?:(-?[0-9,.]*)W)?(?:(-?[0-9,.]*)D)?(?:T(?:(-?[0-9,.]*)H)?(?:(-?[0-9,.]*)M)?(?:(-?[0-9,.]*)S)?)?$/;
  13. export function createDuration (input, key) {
  14. var duration = input,
  15. // matching against regexp is expensive, do it on demand
  16. match = null,
  17. sign,
  18. ret,
  19. diffRes;
  20. if (isDuration(input)) {
  21. duration = {
  22. ms : input._milliseconds,
  23. d : input._days,
  24. M : input._months
  25. };
  26. } else if (typeof input === 'number') {
  27. duration = {};
  28. if (key) {
  29. duration[key] = input;
  30. } else {
  31. duration.milliseconds = input;
  32. }
  33. } else if (!!(match = aspNetRegex.exec(input))) {
  34. sign = (match[1] === '-') ? -1 : 1;
  35. duration = {
  36. y : 0,
  37. d : toInt(match[DATE]) * sign,
  38. h : toInt(match[HOUR]) * sign,
  39. m : toInt(match[MINUTE]) * sign,
  40. s : toInt(match[SECOND]) * sign,
  41. ms : toInt(match[MILLISECOND]) * sign
  42. };
  43. } else if (!!(match = isoRegex.exec(input))) {
  44. sign = (match[1] === '-') ? -1 : 1;
  45. duration = {
  46. y : parseIso(match[2], sign),
  47. M : parseIso(match[3], sign),
  48. w : parseIso(match[4], sign),
  49. d : parseIso(match[5], sign),
  50. h : parseIso(match[6], sign),
  51. m : parseIso(match[7], sign),
  52. s : parseIso(match[8], sign)
  53. };
  54. } else if (duration == null) {// checks for null or undefined
  55. duration = {};
  56. } else if (typeof duration === 'object' && ('from' in duration || 'to' in duration)) {
  57. diffRes = momentsDifference(createLocal(duration.from), createLocal(duration.to));
  58. duration = {};
  59. duration.ms = diffRes.milliseconds;
  60. duration.M = diffRes.months;
  61. }
  62. ret = new Duration(duration);
  63. if (isDuration(input) && hasOwnProp(input, '_locale')) {
  64. ret._locale = input._locale;
  65. }
  66. return ret;
  67. }
  68. createDuration.fn = Duration.prototype;
  69. function parseIso (inp, sign) {
  70. // We'd normally use ~~inp for this, but unfortunately it also
  71. // converts floats to ints.
  72. // inp may be undefined, so careful calling replace on it.
  73. var res = inp && parseFloat(inp.replace(',', '.'));
  74. // apply sign while we're at it
  75. return (isNaN(res) ? 0 : res) * sign;
  76. }
  77. function positiveMomentsDifference(base, other) {
  78. var res = {milliseconds: 0, months: 0};
  79. res.months = other.month() - base.month() +
  80. (other.year() - base.year()) * 12;
  81. if (base.clone().add(res.months, 'M').isAfter(other)) {
  82. --res.months;
  83. }
  84. res.milliseconds = +other - +(base.clone().add(res.months, 'M'));
  85. return res;
  86. }
  87. function momentsDifference(base, other) {
  88. var res;
  89. if (!(base.isValid() && other.isValid())) {
  90. return {milliseconds: 0, months: 0};
  91. }
  92. other = cloneWithOffset(other, base);
  93. if (base.isBefore(other)) {
  94. res = positiveMomentsDifference(base, other);
  95. } else {
  96. res = positiveMomentsDifference(other, base);
  97. res.milliseconds = -res.milliseconds;
  98. res.months = -res.months;
  99. }
  100. return res;
  101. }