27722960485cdf902aac51208afd3a89515f205d802c6599e8f116e3420049d4965c445cad7f763a3d5f93cafe818084f527b098e492ff22852f1c0c3113ba 4.8 KB

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