Fixed before/after + date_format rules implementation

This fixes PR #3607 to provide missing features described on PR #1824 and PR #4699. Also rewrote testBeforeAndAfterWithFormat() tests.
Fix formatting.