Skip to content

Commit 08d228e

Browse files
committed
Merge pull request #24 from MartinNuc/new-timezone-token
New timezone token ZZ for +02:00 timezone format
2 parents 9cb880a + 95c7cea commit 08d228e

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

Gruntfile.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ module.exports = function(grunt) {
3030
},
3131
eslint: {
3232
target: ["src/*.js"]
33+
},
34+
karma: {
35+
unit: {
36+
configFile: 'karma.conf.js'
37+
}
3338
}
3439
});
3540

@@ -39,8 +44,10 @@ module.exports = function(grunt) {
3944
grunt.loadNpmTasks("grunt-contrib-watch");
4045
grunt.loadNpmTasks("grunt-ng-annotate");
4146
grunt.loadNpmTasks("grunt-eslint");
47+
grunt.loadNpmTasks('grunt-karma');
4248

4349
// Default task(s).
4450
grunt.registerTask("default", ["eslint", "ngAnnotate"]);
51+
grunt.registerTask('test', ['ngAnnotate', 'karma']);
4552

4653
};

karma.conf.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ module.exports = function(config) {
1212
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
1313
frameworks: ['jasmine'],
1414

15-
1615
// list of files / patterns to load in the browser
1716
files: [
1817
'bower_components/angular/angular.js',
@@ -58,7 +57,7 @@ module.exports = function(config) {
5857

5958
// start these browsers
6059
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
61-
browsers: ['Firefox'],
60+
browsers: ['PhantomJS'],
6261

6362

6463
// Continuous Integration mode

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
"grunt-contrib-concat": "^0.5.1",
1212
"grunt-contrib-watch": "^0.6.1",
1313
"grunt-eslint": "^18.0.0",
14+
"grunt-karma": "^0.12.2",
1415
"grunt-ng-annotate": "^0.10.0",
1516
"jasmine-core": "^2.2.0",
16-
"karma": "^0.12.31",
17+
"karma": "^0.13.0",
1718
"karma-chrome-launcher": "^0.1.8",
1819
"karma-firefox-launcher": "^0.1.4",
19-
"karma-jasmine": "^0.3.5"
20+
"karma-jasmine": "^0.3.5",
21+
"karma-phantomjs-launcher": "^1.0.0",
22+
"phantomjs-prebuilt": "^2.1.7"
2023
}
2124
}

src/factory.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ angular.module("datetime").factory("datetime", function($locale){
22
// Fetch date and time formats from $locale service
33
var formats = $locale.DATETIME_FORMATS;
44
// Valid format tokens. 1=sss, 2=''
5-
var tokenRE = /yyyy|yy|y|M{1,4}|dd?|EEEE?|HH?|hh?|mm?|ss?|([.,])sss|a|Z|ww|w|'(([^']+|'')*)'/g;
5+
var tokenRE = /yyyy|yy|y|M{1,4}|dd?|EEEE?|HH?|hh?|mm?|ss?|([.,])sss|a|Z{1,2}|ww|w|'(([^']+|'')*)'/g;
66
// Token definition
77
var definedTokens = {
88
"y": {
@@ -184,6 +184,11 @@ angular.module("datetime").factory("datetime", function($locale){
184184
type: "regex",
185185
regex: /[+-]\d{4}/
186186
},
187+
"ZZ": {
188+
name: "timezoneWithColon",
189+
type: "regex",
190+
regex: /[+-]\d{2}:\d{2}/
191+
},
187192
"string": {
188193
name: "string",
189194
type: "static"
@@ -339,6 +344,9 @@ angular.module("datetime").factory("datetime", function($locale){
339344
case "timezone":
340345
node.value = (date.getTimezoneOffset() > 0 ? "-" : "+") + num2str(Math.abs(date.getTimezoneOffset() / 60), 2, 2) + "00";
341346
break;
347+
case "timezoneWithColon":
348+
node.value = (date.getTimezoneOffset() > 0 ? "-" : "+") + num2str(Math.abs(date.getTimezoneOffset() / 60), 2, 2) + ":00";
349+
break;
342350
}
343351

344352
if (node.value < 0) {

test/test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ describe("datetime directive", function(){
153153
$rootScope.date = new Date;
154154

155155
var element = $compile("<input type='text' datetime='{{format}}' ng-model='date'>")($rootScope);
156-
156+
157157
$rootScope.$digest();
158158

159159
expect(element.val()).toEqual($date($rootScope.date, format));
@@ -169,4 +169,23 @@ describe("datetime directive", function(){
169169

170170
expect(element.val()).toEqual("+0000");
171171
});
172+
173+
it("should allow : when using Z:Z token", function(){
174+
$rootScope.date = new Date;
175+
176+
var element = $compile("<input type='text' datetime='ZZ' ng-model='date'>")($rootScope);
177+
178+
$rootScope.$digest();
179+
180+
var timezoneOffset = -new Date().getTimezoneOffset() / 60;
181+
var isNegative = timezoneOffset < 0;
182+
183+
// add leading zeros
184+
if (timezoneOffset < 10) {
185+
timezoneOffset = (isNegative ? "-0" : "+0") + Math.abs(timezoneOffset);
186+
}
187+
188+
expect(element.val()).toEqual(timezoneOffset + ":00");
189+
});
190+
172191
});

0 commit comments

Comments
 (0)