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 Z support
Browse files Browse the repository at this point in the history
- Add support for the `Z` format, as referred to in the Angular
  documentation [here](https://docs.angularjs.org/api/ng/filter/date)

Closes #4831
  • Loading branch information
wesleycho committed Nov 6, 2015
1 parent 628039b commit 2fb812b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
57 changes: 46 additions & 11 deletions src/dateparser/dateparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ angular.module('ui.bootstrap.dateparser', [])
this.hours += 12;
}
}
},
{
key: 'Z',
regex: '[+-]\\d{4}',
apply: function(value) {
var matches = value.match(/([+-])(\d{2})(\d{2})/),
sign = matches[1],
hours = matches[2],
minutes = matches[3];
this.hours += toInt(sign + hours);
this.minutes += toInt(sign + minutes);
}
}
];
};
Expand Down Expand Up @@ -185,7 +197,11 @@ angular.module('ui.bootstrap.dateparser', [])
}
format = format.join('');

map.push({ index: index, apply: data.apply });
map.push({
index: index,
apply: data.apply,
matcher: data.regex
});
}
});

Expand Down Expand Up @@ -214,15 +230,19 @@ angular.module('ui.bootstrap.dateparser', [])
var parser = this.parsers[format],
regex = parser.regex,
map = parser.map,
results = input.match(regex);

results = input.match(regex),
tzOffset = false;
if (results && results.length) {
var fields, dt;
if (angular.isDate(baseDate) && !isNaN(baseDate.getTime())) {
fields = {
year: baseDate.getFullYear(),
month: baseDate.getMonth(),
date: baseDate.getDate()
date: baseDate.getDate(),
hours: baseDate.getHours(),
minutes: baseDate.getMinutes(),
seconds: baseDate.getSeconds(),
milliseconds: baseDate.getMilliseconds()
};
} else {
if (baseDate) {
Expand All @@ -232,21 +252,32 @@ angular.module('ui.bootstrap.dateparser', [])
}

for (var i = 1, n = results.length; i < n; i++) {
var mapper = map[i-1];
var mapper = map[i - 1];
if (mapper.matcher === 'Z') {
tzOffset = true;
}

if (mapper.apply) {
mapper.apply.call(fields, results[i]);
}
}

var datesetter = tzOffset ? Date.prototype.setUTCFullYear :
Date.prototype.setFullYear;
var timesetter = tzOffset ? Date.prototype.setUTCHours :
Date.prototype.setHours;

if (isValid(fields.year, fields.month, fields.date)) {
if (angular.isDate(baseDate) && !isNaN(baseDate.getTime())) {
if (angular.isDate(baseDate) && !isNaN(baseDate.getTime()) && !tzOffset) {
dt = new Date(baseDate);
dt.setFullYear(fields.year, fields.month, fields.date);
datesetter.call(dt, fields.year, fields.month, fields.date);
timesetter.call(dt, fields.hours, fields.minutes,
fields.seconds, fields.milliseconds);
} else {
dt = new Date(fields.year, fields.month, fields.date,
fields.hours, fields.minutes, fields.seconds,
fields.milliseconds || 0);
dt.setFullYear(fields.year);
dt = new Date(0);
datesetter.call(dt, fields.year, fields.month, fields.date);
timesetter.call(dt, fields.hours || 0, fields.minutes || 0,
fields.seconds || 0, fields.milliseconds || 0);
}
}

Expand All @@ -271,4 +302,8 @@ angular.module('ui.bootstrap.dateparser', [])

return true;
}

function toInt(str) {
return parseInt(str, 10);
}
}]);
4 changes: 4 additions & 0 deletions src/dateparser/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ Certain format codes support i18n. Check this [guide](https://docs.angularjs.org
* `a`
_(Example: `10AM`)_ -
Parses a 12 hours time with AM/PM.

* `Z'
_(Example: `-0800`)_ -
Parses the timezone offset in a signed 4 digit representation

\* 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
10 changes: 10 additions & 0 deletions src/dateparser/test/dateparser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ describe('date parser', function() {
expectParse('11-08-13 09AM', 'd-MM-yy hha', new Date(2013, 7, 11, 9));
expectParse('11-08-13 09PM', 'd-MM-yy hha', new Date(2013, 7, 11, 21));
});

it('should work correctly for `Z`', function() {
expectParse('22.March.15 -0700', 'd.MMMM.yy Z', new Date(2015, 2, 21, 17, 0, 0));
expectParse('8-March-1991 +0800', 'd-MMMM-yyyy Z', new Date(1991, 2, 8, 8, 0, 0));
expectParse('February/5/1980 -0200', 'MMMM/d/yyyy Z', new Date(1980, 1, 4, 22, 0, 0));
expectParse('1955/February/5 +0400', 'yyyy/MMMM/d Z', new Date(1955, 1, 5, 4, 0, 0));
expectParse('11-08-13 -1234', 'd-MM-yy Z', new Date(2013, 7, 10, 11, 26, 0));
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));
});
});

describe('with predefined formats', function() {
Expand Down

0 comments on commit 2fb812b

Please sign in to comment.