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