Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
feat(dateparser): add new format support
Browse files Browse the repository at this point in the history
- Adds `ww` support
- Adds `w` support
- Adds `G`, `GG`, `GGG` support
- Adds `GGGG` support

Closes #3418
Closes #4833
  • Loading branch information
wesleycho committed Nov 7, 2015
1 parent 8c3c38d commit ea388b3
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/dateparser/dateparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('|')
}
];
};
Expand Down
15 changes: 15 additions & 0 deletions src/dateparser/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
54 changes: 54 additions & 0 deletions src/dateparser/test/dateparser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit ea388b3

Please sign in to comment.