Skip to content

Commit

Permalink
Support ISO 8601 extended datetime format troughout angular.
Browse files Browse the repository at this point in the history
Support ISO 8601 extended format datetime strings (YYYY-MM-DDTHH:mm:ss.SSSZ) as defined
  in EcmaScript 5 throughout angular. This means that the following apis switched from
  YYYY-MM-DDTHH:mm:ssZ to YYYY-MM-DDTHH:mm:ss.SSSZ (note the added millis) when representing dates:
  - angular.Date.toString
  - angular.String.toDate
  - JSON serialization and deserialization (used by json filter, $xhr and $resource)
  • Loading branch information
IgorMinar committed Nov 9, 2010
1 parent 1a43f36 commit e5c135a
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 27 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
### Api
- date filter now accepts strings that angular.String.toDate can convert to Date objects

### Breaking changes
- we now support ISO 8601 extended format datetime strings (YYYY-MM-DDTHH:mm:ss.SSSZ) as defined
in EcmaScript 5 throughout angular. This means that the following apis switched from
YYYY-MM-DDTHH:mm:ssZ to YYYY-MM-DDTHH:mm:ss.SSSZ (note the added millis) when representing dates:
- angular.Date.toString
- angular.String.fromDate
- JSON serialization and deserialization (used by json filter, $xhr and $resource)


# <angular/> 0.9.2 faunal-mimicry (2010-11-03) #

Expand Down
18 changes: 9 additions & 9 deletions src/apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ var angularString = {
'toDate':function(string){
var match;
if (typeof string == 'string' &&
(match = string.match(/^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z$/))){
(match = string.match(/^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)\.(\d{3})Z$/))){
var date = new Date(0);
date.setUTCFullYear(match[1], match[2] - 1, match[3]);
date.setUTCHours(match[4], match[5], match[6], 0);
date.setUTCHours(match[4], match[5], match[6], match[7]);
return date;
}
return string;
Expand All @@ -223,14 +223,14 @@ var angularString = {

var angularDate = {
'toString':function(date){
function pad(n) { return n < 10 ? "0" + n : n; }
return !date ? date :
date.getUTCFullYear() + '-' +
pad(date.getUTCMonth() + 1) + '-' +
pad(date.getUTCDate()) + 'T' +
pad(date.getUTCHours()) + ':' +
pad(date.getUTCMinutes()) + ':' +
pad(date.getUTCSeconds()) + 'Z' ;
padNumber(date.getUTCFullYear(), 4) + '-' +
padNumber(date.getUTCMonth() + 1, 2) + '-' +
padNumber(date.getUTCDate(), 2) + 'T' +
padNumber(date.getUTCHours(), 2) + ':' +
padNumber(date.getUTCMinutes(), 2) + ':' +
padNumber(date.getUTCSeconds(), 2) + '.' +
padNumber(date.getUTCMilliseconds(), 3) + 'Z';
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ var NUMBER_STRING = /^\d+$/;
* * `'Z'`: 4 digit (+sign) representation of the timezone offset (-1200‒1200)
*
* @param {(Date|number|string)} date Date to format either as Date object, milliseconds (string or
* number) or ISO 8601 string (yyyy-MM-ddTHH:mm:ssZ).
* number) or ISO 8601 extended datetime string (yyyy-MM-ddTHH:mm:ss.SSSZ).
* @param {string=} format Formatting rules. If not specified, Date#toLocaleDateString is used.
* @returns {string} Formatted string or the input if input is not recognized as date/millis.
*
Expand Down
2 changes: 1 addition & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var OPERATORS = {
var ESCAPE = {"n":"\n", "f":"\f", "r":"\r", "t":"\t", "v":"\v", "'":"'", '"':'"'};

function lex(text, parseStringsForObjects){
var dateParseLength = parseStringsForObjects ? 20 : -1,
var dateParseLength = parseStringsForObjects ? 24 : -1,
tokens = [],
token,
index = 0,
Expand Down
9 changes: 5 additions & 4 deletions test/ApiSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,19 @@ describe('api', function(){
it('DateToUTC', function(){
var date = new Date("Sep 10 2003 13:02:03 GMT");
assertEquals("date", angular.Object.typeOf(date));
assertEquals("2003-09-10T13:02:03Z", angular.Date.toString(date));
assertEquals("2003-09-10T13:02:03.000Z", angular.Date.toString(date));
assertEquals(date.getTime(), angular.String.toDate(angular.Date.toString(date)).getTime());
});

it('UTCtoDate', function(){
expect(angular.String.toDate("2003-09-10T13:02:03Z")).toEqual(new Date("Sep 10 2003 13:02:03 GMT"));
expect(angular.String.toDate("2003-09-10T13:02:03.000Z")).
toEqual(new Date("Sep 10 2003 13:02:03 GMT"));
});

it('StringFromUTC', function(){
var date = angular.String.toDate("2003-09-10T13:02:03Z");
var date = angular.String.toDate("2003-09-10T13:02:03.000Z");
assertEquals("date", angular.Object.typeOf(date));
assertEquals("2003-09-10T13:02:03Z", angular.Date.toString(date));
assertEquals("2003-09-10T13:02:03.000Z", angular.Date.toString(date));
assertEquals("str", angular.String.toDate("str"));
});

Expand Down
8 changes: 4 additions & 4 deletions test/FiltersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ describe('filter', function(){

describe('date', function(){

var morning = new TzDate(+5, '2010-09-03T12:05:08Z'); //7am
var noon = new TzDate(+5, '2010-09-03T17:05:08Z'); //12pm
var midnight = new TzDate(+5, '2010-09-03T05:05:08Z'); //12am
var morning = new TzDate(+5, '2010-09-03T12:05:08.000Z'); //7am
var noon = new TzDate(+5, '2010-09-03T17:05:08.000Z'); //12pm
var midnight = new TzDate(+5, '2010-09-03T05:05:08.000Z'); //12am


it('should ignore falsy inputs', function() {
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('filter', function(){
});

it('should be able to parse ISO 8601 dates/times using', function() {
var isoString = '2010-09-03T05:05:08Z';
var isoString = '2010-09-03T05:05:08.872Z';
expect(filter.date(isoString)).
toEqual(angular.String.toDate(isoString).toLocaleDateString());
});
Expand Down
6 changes: 3 additions & 3 deletions test/JsonSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ describe('json', function(){
});

it('should serialize UTC dates', function() {
var date = angular.String.toDate("2009-10-09T01:02:03Z");
expect(toJson(date)).toEqual('"2009-10-09T01:02:03Z"');
expect(fromJson('"2009-10-09T01:02:03Z"').getTime()).toEqual(date.getTime());
var date = angular.String.toDate("2009-10-09T01:02:03.027Z");
expect(toJson(date)).toEqual('"2009-10-09T01:02:03.027Z"');
expect(fromJson('"2009-10-09T01:02:03.027Z"').getTime()).toEqual(date.getTime());
});

it('should prevent recursion', function() {
Expand Down
10 changes: 5 additions & 5 deletions test/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ describe('TzDate', function() {
});

it('should take dateString as constructor argument', function() {
expect(new TzDate(0, '1970-01-01T00:00:00Z').getTime()).toBe(0);
expect(new TzDate(0, '2010-09-03T23:05:08Z').getTime()).toBe(1283555108000);
expect(new TzDate(0, '1970-01-01T00:00:00.000Z').getTime()).toBe(0);
expect(new TzDate(0, '2010-09-03T23:05:08.023Z').getTime()).toBe(1283555108023);
});


Expand Down Expand Up @@ -88,7 +88,7 @@ describe('TzDate', function() {


it('should create a date representing new year in Bratislava', function() {
var newYearInBratislava = new TzDate(-1, '2009-12-31T23:00:00Z');
var newYearInBratislava = new TzDate(-1, '2009-12-31T23:00:00.000Z');
expect(newYearInBratislava.getTimezoneOffset()).toBe(-60);
expect(newYearInBratislava.getFullYear()).toBe(2010);
expect(newYearInBratislava.getMonth()).toBe(0);
Expand All @@ -100,7 +100,7 @@ describe('TzDate', function() {

it('should delegate all the UTC methods to the original UTC Date object', function() {
//from when created from string
var date1 = new TzDate(-1, '2009-12-31T23:00:00Z');
var date1 = new TzDate(-1, '2009-12-31T23:00:00.000Z');
expect(date1.getUTCFullYear()).toBe(2009);
expect(date1.getUTCMonth()).toBe(11);
expect(date1.getUTCDate()).toBe(31);
Expand All @@ -110,7 +110,7 @@ describe('TzDate', function() {


//from when created from millis
var date2 = new TzDate(-1, angular.String.toDate('2009-12-31T23:00:00Z').getTime());
var date2 = new TzDate(-1, angular.String.toDate('2009-12-31T23:00:00.000Z').getTime());
expect(date2.getUTCFullYear()).toBe(2009);
expect(date2.getUTCMonth()).toBe(11);
expect(date2.getUTCDate()).toBe(31);
Expand Down

0 comments on commit e5c135a

Please sign in to comment.