corrade-nucleus-nucleons – Blame information for rev 38
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
38 | office | 1 | import { configFromISO, configFromRFC2822 } from './from-string'; |
2 | import { configFromArray } from './from-array'; |
||
3 | import { getParseRegexForToken } from '../parse/regex'; |
||
4 | import { addTimeToArrayFromToken } from '../parse/token'; |
||
5 | import { expandFormat, formatTokenFunctions, formattingTokens } from '../format/format'; |
||
6 | import checkOverflow from './check-overflow'; |
||
7 | import { HOUR } from '../units/constants'; |
||
8 | import { hooks } from '../utils/hooks'; |
||
9 | import getParsingFlags from './parsing-flags'; |
||
10 | |||
11 | // constant that refers to the ISO standard |
||
12 | hooks.ISO_8601 = function () {}; |
||
13 | |||
14 | // constant that refers to the RFC 2822 form |
||
15 | hooks.RFC_2822 = function () {}; |
||
16 | |||
17 | // date from string and format string |
||
18 | export function configFromStringAndFormat(config) { |
||
19 | // TODO: Move this to another part of the creation flow to prevent circular deps |
||
20 | if (config._f === hooks.ISO_8601) { |
||
21 | configFromISO(config); |
||
22 | return; |
||
23 | } |
||
24 | if (config._f === hooks.RFC_2822) { |
||
25 | configFromRFC2822(config); |
||
26 | return; |
||
27 | } |
||
28 | config._a = []; |
||
29 | getParsingFlags(config).empty = true; |
||
30 | |||
31 | // This array is used to make a Date, either with `new Date` or `Date.UTC` |
||
32 | var string = '' + config._i, |
||
33 | i, parsedInput, tokens, token, skipped, |
||
34 | stringLength = string.length, |
||
35 | totalParsedInputLength = 0; |
||
36 | |||
37 | tokens = expandFormat(config._f, config._locale).match(formattingTokens) || []; |
||
38 | |||
39 | for (i = 0; i < tokens.length; i++) { |
||
40 | token = tokens[i]; |
||
41 | parsedInput = (string.match(getParseRegexForToken(token, config)) || [])[0]; |
||
42 | // console.log('token', token, 'parsedInput', parsedInput, |
||
43 | // 'regex', getParseRegexForToken(token, config)); |
||
44 | if (parsedInput) { |
||
45 | skipped = string.substr(0, string.indexOf(parsedInput)); |
||
46 | if (skipped.length > 0) { |
||
47 | getParsingFlags(config).unusedInput.push(skipped); |
||
48 | } |
||
49 | string = string.slice(string.indexOf(parsedInput) + parsedInput.length); |
||
50 | totalParsedInputLength += parsedInput.length; |
||
51 | } |
||
52 | // don't parse if it's not a known token |
||
53 | if (formatTokenFunctions[token]) { |
||
54 | if (parsedInput) { |
||
55 | getParsingFlags(config).empty = false; |
||
56 | } |
||
57 | else { |
||
58 | getParsingFlags(config).unusedTokens.push(token); |
||
59 | } |
||
60 | addTimeToArrayFromToken(token, parsedInput, config); |
||
61 | } |
||
62 | else if (config._strict && !parsedInput) { |
||
63 | getParsingFlags(config).unusedTokens.push(token); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | // add remaining unparsed input length to the string |
||
68 | getParsingFlags(config).charsLeftOver = stringLength - totalParsedInputLength; |
||
69 | if (string.length > 0) { |
||
70 | getParsingFlags(config).unusedInput.push(string); |
||
71 | } |
||
72 | |||
73 | // clear _12h flag if hour is <= 12 |
||
74 | if (config._a[HOUR] <= 12 && |
||
75 | getParsingFlags(config).bigHour === true && |
||
76 | config._a[HOUR] > 0) { |
||
77 | getParsingFlags(config).bigHour = undefined; |
||
78 | } |
||
79 | |||
80 | getParsingFlags(config).parsedDateParts = config._a.slice(0); |
||
81 | getParsingFlags(config).meridiem = config._meridiem; |
||
82 | // handle meridiem |
||
83 | config._a[HOUR] = meridiemFixWrap(config._locale, config._a[HOUR], config._meridiem); |
||
84 | |||
85 | configFromArray(config); |
||
86 | checkOverflow(config); |
||
87 | } |
||
88 | |||
89 | |||
90 | function meridiemFixWrap (locale, hour, meridiem) { |
||
91 | var isPm; |
||
92 | |||
93 | if (meridiem == null) { |
||
94 | // nothing to do |
||
95 | return hour; |
||
96 | } |
||
97 | if (locale.meridiemHour != null) { |
||
98 | return locale.meridiemHour(hour, meridiem); |
||
99 | } else if (locale.isPM != null) { |
||
100 | // Fallback |
||
101 | isPm = locale.isPM(meridiem); |
||
102 | if (isPm && hour < 12) { |
||
103 | hour += 12; |
||
104 | } |
||
105 | if (!isPm && hour === 12) { |
||
106 | hour = 0; |
||
107 | } |
||
108 | return hour; |
||
109 | } else { |
||
110 | // this is not supposed to happen |
||
111 | return hour; |
||
112 | } |
||
113 | } |