-import { configFromISO } from './from-string';
+import { configFromISO, configFromRFC2822 } from './from-string';
import { configFromArray } from './from-array';
import { getParseRegexForToken } from '../parse/regex';
import { addTimeToArrayFromToken } from '../parse/token';
// constant that refers to the ISO standard
hooks.ISO_8601 = function () {};
+// constant that refers to the RFC 2822 form
+hooks.RFC_2822 = function () {};
+
// date from string and format string
export function configFromStringAndFormat(config) {
// TODO: Move this to another part of the creation flow to prevent circular deps
configFromISO(config);
return;
}
-
+ if (config._f === hooks.RFC_2822) {
+ configFromRFC2822(config);
+ return;
+ }
config._a = [];
getParsingFlags(config).empty = true;
}
}
+// rfc 2822 regex
+// [Group 1: optional][Group 2][Group 3: optional][Group 4]
+//----
+// Group 1: "Day[,] "
+// Day= Day of Week ('Mon','Tue','Wed','Thu','Fri','Sat','Sun')
+// Group 2: "dD Mon [CC]YY HH:MM"
+// dD= Day of Month (1-2-digits) - Strict: 1 to 31 with optional leading zero
+// Mon= Month of Year ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')
+// CC: Century [optional] (2-digits) - Strict: 19 to 99
+// YY: Year in Century (2-digits)
+// HH: Hour of Day (2-digits) - Strict: 00 to 23
+// MM: Minute in Hour (2-digits) - Strict: 00 to 59
+// Group 3: ":SS "
+// SS: Seconds in Minute [optional] (2-digits) - Strict: 00 to 60
+// Group 4: " (TZ|MIL|TO)"
+// TZ: Timezone ('UT','GMT','EST','EDT','CST','CDT','MST','MDT','PST','PDT')
+// MIL: Military timezone code (A-Z excluding J)
+// TO: Time Offset (+|- 4-digits) - Strict: 0000 to 9959 (as per spec)
+//====
+var detailedRfcRegex = /^((?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),?\s)?((?:0?[1-9]|[1-2]?\d|3[01])\s(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s(?:[2-9]\d|19)?\d\d\s(?:[01]\d|2[0-3]):[0-5]\d)(\:(?:60|[0-5]\d))?(\s(?:UT|GMT|(?:[ECMP][SD]T)|[A-IK-Z]|(?:[+-](?:[0-8]\d\d|9\d[0-5])\d)))$/;
+var basicRfcRegex = /^((?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),?\s)?(\d?\d\s(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s(?:\d\d)?\d\d\s\d\d:\d\d)(\:\d\d)?(\s(?:UT|GMT|([ECMP][SD]T)|[A-IK-Z]|(?:[+-]\d{4})))$/;
+var rfcStringFormat = 'ddd, D MMM YYYY HH:mm:ss [GMT]';
+
+// date and time from ref 2822 format
+export function configFromRFC2822(config) {
+ var string = config._i,
+ match = basicRfcRegex.exec(string);
+
+ if (match) {
+ getParsingFlags(config).rfc2822 = true;
+
+ config._f = string;
+ configFromStringAndFormat(config);
+ } else {
+ config._isValid = false;
+ }
+}
+
// date from iso format or fallback
export function configFromString(config) {
var matched = aspNetJsonRegex.exec(config._i);
configFromISO(config);
if (config._isValid === false) {
delete config._isValid;
- hooks.createFromInputFallback(config);
+
+ configFromRFC2822(config);
+ if (config._isValid === false) {
+ delete config._isValid;
+ hooks.createFromInputFallback(config);
+ }
}
}
hooks.createFromInputFallback = deprecate(
- 'value provided is not in a recognized ISO format. moment construction falls back to js Date(), ' +
- 'which is not reliable across all browsers and versions. Non ISO date formats are ' +
+ 'value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), ' +
+ 'which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are ' +
'discouraged and will be removed in an upcoming major release. Please refer to ' +
'http://momentjs.com/guides/#/warnings/js-date/ for more info.',
function (config) {
assert.equal(moment(moment.utc())._isUTC, true, 'An implicit cloned utc moment should have _isUTC == true');
});
+test('parsing rfc 2822', function (assert) {
+ assert.ok(moment('2011-10-08T18:04:20', moment.ISO_8601, true).isValid(),
+ 'CONTROL-WORKS complete ISO date and time');
+
+ assert.ok(moment('Tue, 01 Nov 2016 01:23:45 GMT', moment.RFC_2822, true).isValid(),
+ 'clean RFC2822 datetime with all options');
+});
+
+test('non rfc 2822 strings', function (assert) {
+ assert.ok(!moment('Tue. 01 Nov 2016 01:23:45 GMT', moment.RFC_2822, true).isValid(),
+ 'RFC2822 datetime with all options but invalid day delimiter');
+});
+
test('parsing iso', function (assert) {
var offset = moment([2011, 9, 8]).utcOffset(),
pad = function (input) {