b863fb46a3c154f74e0ba23004c4d99f20bc169b6fc1977f07d053202d499c7fefccfc7ee1f06f2035b73ed0074f88dee1a3454fd62968eb27c0bb581a5f1a 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { normalizeUnits, normalizeObjectUnits } from '../units/aliases';
  2. import { getPrioritizedUnits } from '../units/priorities';
  3. import { hooks } from '../utils/hooks';
  4. import isFunction from '../utils/is-function';
  5. import { isLeapYear } from '../units/year';
  6. export function makeGetSet(unit, keepTime) {
  7. return function (value) {
  8. if (value != null) {
  9. set(this, unit, value);
  10. hooks.updateOffset(this, keepTime);
  11. return this;
  12. } else {
  13. return get(this, unit);
  14. }
  15. };
  16. }
  17. export function get(mom, unit) {
  18. if (!mom.isValid()) {
  19. return NaN;
  20. }
  21. var d = mom._d,
  22. isUTC = mom._isUTC;
  23. switch (unit) {
  24. case 'Milliseconds':
  25. return isUTC ? d.getUTCMilliseconds() : d.getMilliseconds();
  26. case 'Seconds':
  27. return isUTC ? d.getUTCSeconds() : d.getSeconds();
  28. case 'Minutes':
  29. return isUTC ? d.getUTCMinutes() : d.getMinutes();
  30. case 'Hours':
  31. return isUTC ? d.getUTCHours() : d.getHours();
  32. case 'Date':
  33. return isUTC ? d.getUTCDate() : d.getDate();
  34. case 'Day':
  35. return isUTC ? d.getUTCDay() : d.getDay();
  36. case 'Month':
  37. return isUTC ? d.getUTCMonth() : d.getMonth();
  38. case 'FullYear':
  39. return isUTC ? d.getUTCFullYear() : d.getFullYear();
  40. default:
  41. return NaN; // Just in case
  42. }
  43. }
  44. export function set(mom, unit, value) {
  45. var d, isUTC, year, month, date;
  46. if (!mom.isValid() || isNaN(value)) {
  47. return;
  48. }
  49. d = mom._d;
  50. isUTC = mom._isUTC;
  51. switch (unit) {
  52. case 'Milliseconds':
  53. return void (isUTC
  54. ? d.setUTCMilliseconds(value)
  55. : d.setMilliseconds(value));
  56. case 'Seconds':
  57. return void (isUTC ? d.setUTCSeconds(value) : d.setSeconds(value));
  58. case 'Minutes':
  59. return void (isUTC ? d.setUTCMinutes(value) : d.setMinutes(value));
  60. case 'Hours':
  61. return void (isUTC ? d.setUTCHours(value) : d.setHours(value));
  62. case 'Date':
  63. return void (isUTC ? d.setUTCDate(value) : d.setDate(value));
  64. // case 'Day': // Not real
  65. // return void (isUTC ? d.setUTCDay(value) : d.setDay(value));
  66. // case 'Month': // Not used because we need to pass two variables
  67. // return void (isUTC ? d.setUTCMonth(value) : d.setMonth(value));
  68. case 'FullYear':
  69. break; // See below ...
  70. default:
  71. return; // Just in case
  72. }
  73. year = value;
  74. month = mom.month();
  75. date = mom.date();
  76. date = date === 29 && month === 1 && !isLeapYear(year) ? 28 : date;
  77. void (isUTC
  78. ? d.setUTCFullYear(year, month, date)
  79. : d.setFullYear(year, month, date));
  80. }
  81. // MOMENTS
  82. export function stringGet(units) {
  83. units = normalizeUnits(units);
  84. if (isFunction(this[units])) {
  85. return this[units]();
  86. }
  87. return this;
  88. }
  89. export function stringSet(units, value) {
  90. if (typeof units === 'object') {
  91. units = normalizeObjectUnits(units);
  92. var prioritized = getPrioritizedUnits(units),
  93. i,
  94. prioritizedLen = prioritized.length;
  95. for (i = 0; i < prioritizedLen; i++) {
  96. this[prioritized[i].unit](units[prioritized[i].unit]);
  97. }
  98. } else {
  99. units = normalizeUnits(units);
  100. if (isFunction(this[units])) {
  101. return this[units](value);
  102. }
  103. }
  104. return this;
  105. }