Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

Commit d3295d5

Browse files
wabsoneromano
authored andcommitted
Make parsing of dates from ECM timezone-aware (#133)
* Make parsing of dates from ECM timezone-aware Refs #134 * Set correct tab size for generated code Refs #134 * Add changelog entry * Support timezones specified in different formats Refs #134 * Add new dist files Refs #134
1 parent 8909407 commit d3295d5

File tree

9 files changed

+648
-404
lines changed

9 files changed

+648
-404
lines changed

.editorconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ end_of_line = lf
1010
insert_final_newline = true
1111
trim_trailing_whitespace = true
1212

13+
[src/alfresco-activiti-rest-api/**.js]
14+
indent_size = 2
15+
16+
[src/alfresco-core-rest-api/**.js]
17+
indent_size = 2
18+
1319
[package.json]
1420
indent_style = space
1521
indent_size = 2

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ _This project provides a JavaScript client API into the v1 Alfresco REST API_
1111
## fix
1212
- [/api/enterprise/script-files/controllers sending wrong accept header #130](https://github.com/Alfresco/alfresco-js-api/pull/130)
1313
- [CSRF Token is not working #128](https://github.com/Alfresco/alfresco-js-api/pull/128)
14+
- [Timestamp timezones are ignored #134](https://github.com/Alfresco/alfresco-js-api/issues/134)
1415

1516
<a name="0.5.5"></a>
1617
# [0.5.5](https://github.com/Alfresco/alfresco-js-api/releases/tag/0.5.5) (09-12-2016)

dist/alfresco-js-api.js

Lines changed: 494 additions & 380 deletions
Large diffs are not rendered by default.

dist/alfresco-js-api.min.js

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"babel-plugin-transform-proto-to-assign": "^6.9.0",
3939
"browserify": "^13.0.1",
4040
"chai": "^3.5.0",
41+
"chai-datetime": "^1.4.1",
4142
"expect.js": "~0.3.1",
4243
"grunt": "~0.4.0",
4344
"grunt-cli": "^1.1.0",

src/alfresco-core-rest-api/src/ApiClient.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,43 @@
400400
* @returns {Date} The parsed date object.
401401
*/
402402
exports.parseDate = function(str) {
403+
var dateLength = 10;
404+
var separatorPos = str.substring(dateLength).search(/[\+\-]/) + dateLength;
405+
var dateStr = separatorPos > dateLength ? str.substring(0, separatorPos) : str;
406+
var tzStr = separatorPos > dateLength ? str.substring(separatorPos) : '';
407+
var parsedDate = exports.parseDateTime(dateStr);
408+
var tzOffsetMins = exports.parseDateTimeZone(tzStr);
409+
parsedDate.setTime(parsedDate.getTime() + tzOffsetMins * 60000);
410+
return parsedDate;
411+
};
412+
413+
/**
414+
* Parses the date component of a ISO-8601 string representation of a date value.
415+
* @param {String} str The date value as a string.
416+
* @returns {Date} The parsed date object.
417+
*/
418+
exports.parseDateTime = function(str) {
403419
// TODO: review when Safari 10 is released
404420
// return new Date(str.replace(/T/i, ' '));
405421

406422
// Compatible with Safari 9.1.2
407-
var a = str.split(/[^0-9]/).map(function(s) { return parseInt(s, 10) });
408-
return new Date(a[0], a[1]-1 || 0, a[2] || 1, a[3] || 0, a[4] || 0, a[5] || 0, a[6] || 0);
423+
var parts = str.split('+');
424+
var dateParts = str.split(/[^0-9]/).map(function(s) { return parseInt(s, 10) });
425+
return new Date(Date.UTC(dateParts[0], dateParts[1]-1 || 0, dateParts[2] || 1, dateParts[3] || 0, dateParts[4] || 0, dateParts[5] || 0, dateParts[6] || 0));
426+
};
427+
428+
/**
429+
* Parses the timezone component of a ISO-8601 string representation of a date value.
430+
* @param {String} str The timezone offset as a string, e.g. '+0000', '+2000' or '-0500'.
431+
* @returns {number} The number of minutes offset from UTC.
432+
*/
433+
exports.parseDateTimeZone = function(str) {
434+
var match = /([\+\-])(\d{2}):?(\d{2})?/.exec(str);
435+
if (match !== null) {
436+
return (parseInt(match[1] + '1') * -1 * (parseInt(match[2]) * 60) + parseInt(match[3] || 0))
437+
} else {
438+
return 0;
439+
}
409440
};
410441

411442
/**

test/alfrescoApiClient.spec.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*global describe, it */
2+
3+
var ApiClient = require('../src/alfresco-core-rest-api/src/ApiClient');
4+
var chai = require('chai');
5+
var expect = chai.expect;
6+
7+
chai.use(require('chai-datetime'));
8+
9+
describe('Alfresco Core API Client', function () {
10+
11+
it('should convert dates falling in GMT without a timezone', function () {
12+
expect(ApiClient.parseDate('2015-11-17T03:33:17')).to.equalTime(new Date(Date.UTC(2015, 10, 17, 3, 33, 17)));
13+
});
14+
15+
it('should convert dates falling in BST without a timezone', function () {
16+
expect(ApiClient.parseDate('2015-10-17T03:33:17')).to.equalTime(new Date(Date.UTC(2015, 9, 17, 3, 33, 17)));
17+
});
18+
19+
it('should convert dates with a UTC Zulu-time timezone', function () {
20+
expect(ApiClient.parseDate('2015-11-17T03:33:17Z')).to.equalTime(new Date(Date.UTC(2015, 10, 17, 3, 33, 17)));
21+
});
22+
23+
it('should convert dates with a UTC zero-offset timezone', function () {
24+
expect(ApiClient.parseDate('2015-11-17T03:33:17+0000')).to.equalTime(new Date(Date.UTC(2015, 10, 17, 3, 33, 17)));
25+
});
26+
27+
it('should convert dates with a positive offset timezone', function () {
28+
expect(ApiClient.parseDate('2015-11-17T03:33:17+0200')).to.equalTime(new Date(Date.UTC(2015, 10, 17, 1, 33, 17)));
29+
});
30+
31+
it('should convert dates with a negative offset timezone', function () {
32+
expect(ApiClient.parseDate('2015-11-17T03:33:17-0200')).to.equalTime(new Date(Date.UTC(2015, 10, 17, 5, 33, 17)));
33+
});
34+
35+
it('should convert dates with a part-hour offset', function () {
36+
expect(ApiClient.parseDate('2015-11-17T03:23:17-0930')).to.equalTime(new Date(Date.UTC(2015, 10, 17, 12, 53, 17)));
37+
});
38+
39+
it('should convert dates with a timezone HH:MM separator', function () {
40+
expect(ApiClient.parseDate('2015-11-17T03:33:17+02:00')).to.equalTime(new Date(Date.UTC(2015, 10, 17, 1, 33, 17)));
41+
});
42+
43+
it('should convert dates with a timezone with hours only', function () {
44+
expect(ApiClient.parseDate('2015-11-17T03:33:17+02')).to.equalTime(new Date(Date.UTC(2015, 10, 17, 1, 33, 17)));
45+
});
46+
47+
});

test/mockObjects/alfresco/nodeMock.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,36 @@ class NodeMock extends BaseMock {
137137
});
138138
}
139139

140+
get200ResponseChildrenNonUTCTimes() {
141+
nock(this.host, {'encodedQueryParams': true})
142+
.get('/alfresco/api/-default-/public/alfresco/versions/1/nodes/b4cff62a-664d-4d45-9302-98723eac1320/children')
143+
.reply(200, {
144+
'list': {
145+
'pagination': {
146+
'count': 5,
147+
'hasMoreItems': false,
148+
'totalItems': 5,
149+
'skipCount': 0,
150+
'maxItems': 100
151+
},
152+
'entries': [{
153+
'entry': {
154+
'createdAt': '2011-03-15T12:04:54.290-0500',
155+
'isFolder': true,
156+
'isFile': false,
157+
'createdByUser': {'id': 'mjackson', 'displayName': 'Mike Jackson'},
158+
'modifiedAt': '2011-03-15T12:04:54.290-0500',
159+
'modifiedByUser': {'id': 'mjackson', 'displayName': 'Mike Jackson'},
160+
'name': 'discussions',
161+
'id': '059c5bc7-2d38-4dc5-96b8-d09cd3c69b4c',
162+
'nodeType': 'cm:folder',
163+
'parentId': 'b4cff62a-664d-4d45-9302-98723eac1320'
164+
}
165+
}]
166+
}
167+
});
168+
}
169+
140170
get404ChildrenNotExist() {
141171
nock(this.host, {'encodedQueryParams': true})
142172
.get('/alfresco/api/-default-/public/alfresco/versions/1/nodes/b4cff62a-664d-4d45-9302-98723eac1319/children')

test/nodeApi.spec.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
/*global describe, it, beforeEach */
22

33
var AlfrescoApi = require('../main');
4-
var expect = require('chai').expect;
4+
var chai = require('chai');
5+
var expect = chai.expect;
56
var AuthResponseMock = require('../test/mockObjects/mockAlfrescoApi').Auth;
67
var NodeMock = require('../test/mockObjects/mockAlfrescoApi').Node;
78
var fs = require('fs');
89

10+
chai.use(require('chai-datetime'));
11+
912
describe('Node', function () {
1013
beforeEach(function (done) {
1114
this.hostEcm = 'http://127.0.0.1:8080';
@@ -111,6 +114,17 @@ describe('Node', function () {
111114
done();
112115
});
113116
});
117+
118+
it('should return dates as timezone-aware', function (done) {
119+
this.nodeMock.get200ResponseChildrenNonUTCTimes();
120+
121+
this.alfrescoJsApi.nodes.getNodeChildren('b4cff62a-664d-4d45-9302-98723eac1320').then(function (data) {
122+
expect(data.list.entries.length).to.be.equal(1);
123+
expect(data.list.entries[0].entry.createdAt).to.equalTime(new Date(Date.UTC(2011, 2, 15, 17, 4, 54, 290)));
124+
done();
125+
});
126+
127+
});
114128
});
115129

116130
describe('Delete', function () {

0 commit comments

Comments
 (0)