From ea388b34d0661bf66c112bd2a398d6a2ac737f0b Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Sat, 7 Nov 2015 06:58:48 -0800 Subject: [PATCH] feat(dateparser): add new format support - Adds `ww` support - Adds `w` support - Adds `G`, `GG`, `GGG` support - Adds `GGGG` support Closes #3418 Closes #4833 --- src/dateparser/dateparser.js | 24 ++++++++++++ src/dateparser/docs/README.md | 15 +++++++ src/dateparser/test/dateparser.spec.js | 54 ++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/src/dateparser/dateparser.js b/src/dateparser/dateparser.js index 2f57328856..6e0e643138 100644 --- a/src/dateparser/dateparser.js +++ b/src/dateparser/dateparser.js @@ -145,6 +145,30 @@ angular.module('ui.bootstrap.dateparser', []) this.hours += toInt(sign + hours); this.minutes += toInt(sign + minutes); } + }, + { + key: 'ww', + regex: '[0-4][0-9]|5[0-3]' + }, + { + key: 'w', + regex: '[0-9]|[1-4][0-9]|5[0-3]' + }, + { + key: 'GGGG', + regex: $locale.DATETIME_FORMATS.ERANAMES.join('|').replace(/\s/g, '\\s') + }, + { + key: 'GGG', + regex: $locale.DATETIME_FORMATS.ERAS.join('|') + }, + { + key: 'GG', + regex: $locale.DATETIME_FORMATS.ERAS.join('|') + }, + { + key: 'G', + regex: $locale.DATETIME_FORMATS.ERAS.join('|') } ]; }; diff --git a/src/dateparser/docs/README.md b/src/dateparser/docs/README.md index 7518fee346..56003eae5a 100644 --- a/src/dateparser/docs/README.md +++ b/src/dateparser/docs/README.md @@ -121,6 +121,21 @@ Certain format codes support i18n. Check this [guide](https://docs.angularjs.org * `Z` _(Example: `-0800`)_ - Parses the timezone offset in a signed 4 digit representation + +* `ww` + _(Example: `03`, Leading 0)_ - + Parses the week number + +* `w` + _(Example: `03`)_ - + Parses the week number + +* `G`, `GG`, `GGG` + _(Example: `AD`)_ - + Parses the era (`AD` or `BC`) +* `GGGG` + _(Example: `Anno Domini`)_ - + Parses the long form of the era (`Anno Domini` or `Before Christ`) \* The ones marked with `Leading 0`, needs a leading 0 for values less than 10. Exception being milliseconds which needs it for values under 100. diff --git a/src/dateparser/test/dateparser.spec.js b/src/dateparser/test/dateparser.spec.js index 3ad8341332..1391c0e563 100644 --- a/src/dateparser/test/dateparser.spec.js +++ b/src/dateparser/test/dateparser.spec.js @@ -234,6 +234,60 @@ describe('date parser', function() { expectParse('22.March.15.22:33:4 -1200', 'd.MMMM.yy.HH:mm:s Z', new Date(2015, 2, 22, 10, 33, 4)); expectParse('22.March.15.22:3:4 +1500', 'd.MMMM.yy.HH:m:s Z', new Date(2015, 2, 23, 13, 3, 4)); }); + + it('should work correctly for `ww`', function() { + expectParse('17.November.13.45', 'd.MMMM.yy.ww', new Date(2013, 10, 17, 0)); + expectParse('8-March-1991-09', 'd-MMMM-yyyy-ww', new Date(1991, 2, 8, 0)); + expectParse('February/5/1980/05', 'MMMM/d/yyyy/ww', new Date(1980, 1, 5, 0)); + expectParse('1955/February/5/04', 'yyyy/MMMM/d/ww', new Date(1955, 1, 5, 0)); + expectParse('11-08-13 44', 'd-MM-yy ww', new Date(2013, 7, 11, 0)); + expectParse('0001/03/6 10', 'yyyy/MM/d ww', oldDate); + }); + + it('should work correctly for `w`', function() { + expectParse('17.November.13.45', 'd.MMMM.yy.w', new Date(2013, 10, 17, 0)); + expectParse('8-March-1991-9', 'd-MMMM-yyyy-w', new Date(1991, 2, 8, 0)); + expectParse('February/5/1980/5', 'MMMM/d/yyyy/w', new Date(1980, 1, 5, 0)); + expectParse('1955/February/5/4', 'yyyy/MMMM/d/w', new Date(1955, 1, 5, 0)); + expectParse('11-08-13 44', 'd-MM-yy w', new Date(2013, 7, 11, 0)); + expectParse('0001/03/6 10', 'yyyy/MM/d w', oldDate); + }); + + it('should work correctly for `G`', function() { + expectParse('17.November.13.AD', 'd.MMMM.yy.G', new Date(2013, 10, 17, 0)); + expectParse('8-March-1991-BC', 'd-MMMM-yyyy-G', new Date(1991, 2, 8, 0)); + expectParse('February/5/1980/AD', 'MMMM/d/yyyy/G', new Date(1980, 1, 5, 0)); + expectParse('1955/February/5/BC', 'yyyy/MMMM/d/G', new Date(1955, 1, 5, 0)); + expectParse('11-08-13 AD', 'd-MM-yy G', new Date(2013, 7, 11, 0)); + expectParse('0001/03/6 BC', 'yyyy/MM/d G', oldDate); + }); + + it('should work correctly for `GG`', function() { + expectParse('17.November.13.AD', 'd.MMMM.yy.GG', new Date(2013, 10, 17, 0)); + expectParse('8-March-1991-BC', 'd-MMMM-yyyy-GG', new Date(1991, 2, 8, 0)); + expectParse('February/5/1980/AD', 'MMMM/d/yyyy/GG', new Date(1980, 1, 5, 0)); + expectParse('1955/February/5/BC', 'yyyy/MMMM/d/GG', new Date(1955, 1, 5, 0)); + expectParse('11-08-13 AD', 'd-MM-yy GG', new Date(2013, 7, 11, 0)); + expectParse('0001/03/6 BC', 'yyyy/MM/d GG', oldDate); + }); + + it('should work correctly for `GGG`', function() { + expectParse('17.November.13.AD', 'd.MMMM.yy.GGG', new Date(2013, 10, 17, 0)); + expectParse('8-March-1991-BC', 'd-MMMM-yyyy-GGG', new Date(1991, 2, 8, 0)); + expectParse('February/5/1980/AD', 'MMMM/d/yyyy/GGG', new Date(1980, 1, 5, 0)); + expectParse('1955/February/5/BC', 'yyyy/MMMM/d/GGG', new Date(1955, 1, 5, 0)); + expectParse('11-08-13 AD', 'd-MM-yy GGG', new Date(2013, 7, 11, 0)); + expectParse('0001/03/6 BC', 'yyyy/MM/d GGG', oldDate); + }); + + it('should work correctly for `GGGG`', function() { + expectParse('17.November.13.Anno Domini', 'd.MMMM.yy.GGGG', new Date(2013, 10, 17, 0)); + expectParse('8-March-1991-Before Christ', 'd-MMMM-yyyy-GGGG', new Date(1991, 2, 8, 0)); + expectParse('February/5/1980/Anno Domini', 'MMMM/d/yyyy/GGGG', new Date(1980, 1, 5, 0)); + expectParse('1955/February/5/Before Christ', 'yyyy/MMMM/d/GGGG', new Date(1955, 1, 5, 0)); + expectParse('11-08-13 Anno Domini', 'd-MM-yy GGGG', new Date(2013, 7, 11, 0)); + expectParse('0001/03/6 Before Christ', 'yyyy/MM/d GGGG', oldDate); + }); }); describe('with predefined formats', function() {