Skip to content

[feature] RFC2822 parsing - #3708

Closed
TracyGJG wants to merge 35 commits into
moment:developfrom
TracyGJG:develop
Closed

TracyGJG wants to merge 35 commits into
moment:developfrom
TracyGJG:develop

Conversation

@TracyGJG

@TracyGJG TracyGJG commented Jan 8, 2017 •

Copy link
Copy Markdown
Contributor

I have corrected the issue with the failing test but I still have a test I am unable to perform with out some advice. The test case 'RFC2822 datetime with mismatching Day (week v date)' confirms the day is consistent when given as both a day of week and day of month. This is a stipulation of the RFC so needs to be correct but I cannot achieve this without using a JS Date object, unless the core team have an alternative solution.

Work in Progress - Do not Merge, Advice required. Issue #2530

@TracyGJG TracyGJG changed the title Work in Progress - Do Not Merge (updated 8th Jan 2017) RFC2822 Parsing implementation - Near-candidate solution, tests working Jan 9, 2017
Comment thread src/lib/create/from-string.js Outdated
' PDT': ' -0700',
' PST': ' -0800'
};
var rfc2822Military = 'YXWVUTSRQPONZABCDEFGHIKLM';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please remove rfc2822 from the names of things inside the method named configFromRFC2822 :)

@ichernev

ichernev commented Feb 4, 2017

Copy link
Copy Markdown
Contributor

@TracyGJG that looks pretty clean to me. Can you please summarize what is the current state, are there any places where this differs from the spec ((a) we parse it but its not spec or (b) we don't parse it but its spec), we can drop most comments and it should be good to go.

@TracyGJG

TracyGJG commented Feb 4, 2017

Copy link
Copy Markdown
Contributor Author

@ichernev, as requested I have 'Removed rfc2822 prefix from internal variables.' in my latest commit. To answer your question regarding compliance with the spec. To be fully compliant (afaik) we have to ensure the any day given in the sting as a weekday is consistent with the date given. I have not been able to perform this check and I have disabled the test. [TODO: src/lib/create/from-string.js line 163, src/test/create.js line 448]. I had tried creating a moment from the date only elements and extract the weekday but this caused a conflict - any suggestions.

@TracyGJG

Copy link
Copy Markdown
Contributor Author

@ichernev, @maggiepint and @mj1856, I finally have a fully compliant (AFAIK) implementation of RFC2822. In order to validate the weekday against the date I have had to use the native JavaScript Date object. I have left in the code a TODO to replace with an independent approach. However, as I have not managed to discover an alternative after several months, this approach will have to suffice.

@ichernev

Copy link
Copy Markdown
Contributor

@TracyGJG the proper way to do the check about weekday matching is using the validation/flag system, take a look at src/lib/create/from-string-and-array.js and more notably the modification of getParsingFlags() (its just lazily returning an object). src/lib/create/parsing-flags.js has all of them -- you can add one like weekdayMismatch which defaults to false, but is set to true in your RFC parsing function. If someone wants to parse RFC and calls your function and you parse it and figure out the weekday is mismatching, doesn't mean you abandon the parse, quite the opposite -- you set the flag and call it done (that is -- you parsed correctly and produced an invalid moment).

Whether you first produce a moment object, then figure out its bad, or you first figure out its bad (by using a Date) ... there is no difference for the end user, and we can safely switch one to the other without breaking compatibility in the future.

@TracyGJG

Copy link
Copy Markdown
Contributor Author

@ichernev, thanks for the steer. I have added the weekdayMismatch flag as suggested, defaulted to false and set to true should the check fails. I have retained setting the _isValid flag and exit (return).

if (match[1].substr(0,3) !== momentDay) {
    getParsingFlags(config).weekdayMismatch = true;
    config._isValid = false;
    return;
}

Is the above code appropriate or should it be simplified?

@ichernev ichernev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost ready to ship ;-)

Comment thread src/lib/create/from-string.js Outdated
(\:(?:60|[0-5]\d))?
(\s(?:UT|GMT|[ECMP][SD]T|[A-IK-Z]|(?:[+-](?:1[012]|0\d)[03]0)))
$/;
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please shorten this comment, remove the expanded regex, if possible just link to the source (RFC).

Comment thread src/lib/create/from-string.js Outdated
' PDT': ' -0700',
' PST': ' -0800'
};
var Military = 'YXWVUTSRQPONZABCDEFGHIKLM';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be lowercase military.

Comment thread src/lib/create/from-string.js Outdated
return;
}
}
getParsingFlags(config).rfc2822 = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to set this flag after a successful parse at the bottom, otherwise it might get out-of-sync

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I rechecked -- the ISO parsing has the same issue. We need to set the flag only after a fully successful parse.

configFromISO(config);
if (config._isValid === false) {
delete config._isValid;
hooks.createFromInputFallback(config);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code should be linear but is instead nested. I'd return in case isValid becomes true, so more parsers can be added without increasing indentation.

Comment thread src/test/moment/create.js Outdated
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');
assert.ok(!moment('Mon, 01 Nov 2016 01:23:45 GMT', moment.RFC_2822, true).isValid(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also check if the proper parsing flag is set.

Comment thread src/test/moment/relative_time.js Outdated
moment.relativeTimeThreshold('M', 11);
});

/* TGJG Disabled

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to reenable this code.

Comment thread src/lib/create/parsing-flags.js Outdated
parsedDateParts : [],
meridiem : null
meridiem : null,
rfc2822 : false,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spacing.

TracyGJG and others added 11 commits March 6, 2017 12:15
Test case re-enabled.
Spacing of the rfc2822 parse flag syntax aligned.
configFromString() made linear, Military variable made lowercase and RegExp comment replaced with tools.ietf reference.
Trailing white-space removed
Issue with configFromString()
@TracyGJG

TracyGJG commented Mar 6, 2017

Copy link
Copy Markdown
Contributor Author

@ichernev, all comments addressed including the syntax issue raised by @westy92.
All tests pass and no issues remain. Now ready to merge.

@TracyGJG TracyGJG changed the title RFC2822 Parsing implementation - Near-candidate solution, tests working RFC2822 Parsing implementation - Ready for merge Mar 8, 2017
@ichernev ichernev added this to the 2.18.0 milestone Mar 12, 2017
@ichernev

Copy link
Copy Markdown
Contributor

Merged in 7589bb8

ichernev added a commit that referenced this pull request Mar 12, 2017
@ichernev ichernev changed the title RFC2822 Parsing implementation - Ready for merge [feature] RFC2822 parsing Mar 12, 2017
@ichernev ichernev closed this Mar 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants