fe2a06044c44a95e25c72518efdd61d95bc7c2a2d4408eb7dfd99576ed3b55cf97fa819f5c9caecdca86cee7b88e652aa068773ed8767583848ec7f2439744 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { hooks } from '../utils/hooks';
  2. import { createDate, createUTCDate } from './date-from-array';
  3. import { daysInYear } from '../units/year';
  4. import {
  5. weekOfYear,
  6. weeksInYear,
  7. dayOfYearFromWeeks,
  8. } from '../units/week-calendar-utils';
  9. import {
  10. YEAR,
  11. MONTH,
  12. DATE,
  13. HOUR,
  14. MINUTE,
  15. SECOND,
  16. MILLISECOND,
  17. } from '../units/constants';
  18. import { createLocal } from './local';
  19. import defaults from '../utils/defaults';
  20. import getParsingFlags from './parsing-flags';
  21. function currentDateArray(config) {
  22. // hooks is actually the exported moment object
  23. var nowValue = new Date(hooks.now());
  24. if (config._useUTC) {
  25. return [
  26. nowValue.getUTCFullYear(),
  27. nowValue.getUTCMonth(),
  28. nowValue.getUTCDate(),
  29. ];
  30. }
  31. return [nowValue.getFullYear(), nowValue.getMonth(), nowValue.getDate()];
  32. }
  33. // convert an array to a date.
  34. // the array should mirror the parameters below
  35. // note: all values past the year are optional and will default to the lowest possible value.
  36. // [year, month, day , hour, minute, second, millisecond]
  37. export function configFromArray(config) {
  38. var i,
  39. date,
  40. input = [],
  41. currentDate,
  42. expectedWeekday,
  43. yearToUse;
  44. if (config._d) {
  45. return;
  46. }
  47. currentDate = currentDateArray(config);
  48. //compute day of the year from weeks and weekdays
  49. if (config._w && config._a[DATE] == null && config._a[MONTH] == null) {
  50. dayOfYearFromWeekInfo(config);
  51. }
  52. //if the day of the year is set, figure out what it is
  53. if (config._dayOfYear != null) {
  54. yearToUse = defaults(config._a[YEAR], currentDate[YEAR]);
  55. if (
  56. config._dayOfYear > daysInYear(yearToUse) ||
  57. config._dayOfYear === 0
  58. ) {
  59. getParsingFlags(config)._overflowDayOfYear = true;
  60. }
  61. date = createUTCDate(yearToUse, 0, config._dayOfYear);
  62. config._a[MONTH] = date.getUTCMonth();
  63. config._a[DATE] = date.getUTCDate();
  64. }
  65. // Default to current date.
  66. // * if no year, month, day of month are given, default to today
  67. // * if day of month is given, default month and year
  68. // * if month is given, default only year
  69. // * if year is given, don't default anything
  70. for (i = 0; i < 3 && config._a[i] == null; ++i) {
  71. config._a[i] = input[i] = currentDate[i];
  72. }
  73. // Zero out whatever was not defaulted, including time
  74. for (; i < 7; i++) {
  75. config._a[i] = input[i] =
  76. config._a[i] == null ? (i === 2 ? 1 : 0) : config._a[i];
  77. }
  78. // Check for 24:00:00.000
  79. if (
  80. config._a[HOUR] === 24 &&
  81. config._a[MINUTE] === 0 &&
  82. config._a[SECOND] === 0 &&
  83. config._a[MILLISECOND] === 0
  84. ) {
  85. config._nextDay = true;
  86. config._a[HOUR] = 0;
  87. }
  88. config._d = (config._useUTC ? createUTCDate : createDate).apply(
  89. null,
  90. input
  91. );
  92. expectedWeekday = config._useUTC
  93. ? config._d.getUTCDay()
  94. : config._d.getDay();
  95. // Apply timezone offset from input. The actual utcOffset can be changed
  96. // with parseZone.
  97. if (config._tzm != null) {
  98. config._d.setUTCMinutes(config._d.getUTCMinutes() - config._tzm);
  99. }
  100. if (config._nextDay) {
  101. config._a[HOUR] = 24;
  102. }
  103. // check for mismatching day of week
  104. if (
  105. config._w &&
  106. typeof config._w.d !== 'undefined' &&
  107. config._w.d !== expectedWeekday
  108. ) {
  109. getParsingFlags(config).weekdayMismatch = true;
  110. }
  111. }
  112. function dayOfYearFromWeekInfo(config) {
  113. var w, weekYear, week, weekday, dow, doy, temp, weekdayOverflow, curWeek;
  114. w = config._w;
  115. if (w.GG != null || w.W != null || w.E != null) {
  116. dow = 1;
  117. doy = 4;
  118. // TODO: We need to take the current isoWeekYear, but that depends on
  119. // how we interpret now (local, utc, fixed offset). So create
  120. // a now version of current config (take local/utc/offset flags, and
  121. // create now).
  122. weekYear = defaults(
  123. w.GG,
  124. config._a[YEAR],
  125. weekOfYear(createLocal(), 1, 4).year
  126. );
  127. week = defaults(w.W, 1);
  128. weekday = defaults(w.E, 1);
  129. if (weekday < 1 || weekday > 7) {
  130. weekdayOverflow = true;
  131. }
  132. } else {
  133. dow = config._locale._week.dow;
  134. doy = config._locale._week.doy;
  135. curWeek = weekOfYear(createLocal(), dow, doy);
  136. weekYear = defaults(w.gg, config._a[YEAR], curWeek.year);
  137. // Default to current week.
  138. week = defaults(w.w, curWeek.week);
  139. if (w.d != null) {
  140. // weekday -- low day numbers are considered next week
  141. weekday = w.d;
  142. if (weekday < 0 || weekday > 6) {
  143. weekdayOverflow = true;
  144. }
  145. } else if (w.e != null) {
  146. // local weekday -- counting starts from beginning of week
  147. weekday = w.e + dow;
  148. if (w.e < 0 || w.e > 6) {
  149. weekdayOverflow = true;
  150. }
  151. } else {
  152. // default to beginning of week
  153. weekday = dow;
  154. }
  155. }
  156. if (week < 1 || week > weeksInYear(weekYear, dow, doy)) {
  157. getParsingFlags(config)._overflowWeeks = true;
  158. } else if (weekdayOverflow != null) {
  159. getParsingFlags(config)._overflowWeekday = true;
  160. } else {
  161. temp = dayOfYearFromWeeks(weekYear, week, weekday, dow, doy);
  162. config._a[YEAR] = temp.year;
  163. config._dayOfYear = temp.dayOfYear;
  164. }
  165. }