Skip to content

New timezone token ZZ for +02:00 timezone format #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ module.exports = function(grunt) {
},
eslint: {
target: ["src/*.js"]
},
karma: {
unit: {
configFile: 'karma.conf.js'
}
}
});

Expand All @@ -39,8 +44,10 @@ module.exports = function(grunt) {
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-ng-annotate");
grunt.loadNpmTasks("grunt-eslint");
grunt.loadNpmTasks('grunt-karma');

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

};
3 changes: 1 addition & 2 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ module.exports = function(config) {
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],


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

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Firefox'],
browsers: ['PhantomJS'],


// Continuous Integration mode
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
"grunt-contrib-concat": "^0.5.1",
"grunt-contrib-watch": "^0.6.1",
"grunt-eslint": "^18.0.0",
"grunt-karma": "^0.12.2",
"grunt-ng-annotate": "^0.10.0",
"jasmine-core": "^2.2.0",
"karma": "^0.12.31",
"karma": "^0.13.0",
"karma-chrome-launcher": "^0.1.8",
"karma-firefox-launcher": "^0.1.4",
"karma-jasmine": "^0.3.5"
"karma-jasmine": "^0.3.5",
"karma-phantomjs-launcher": "^1.0.0",
"phantomjs-prebuilt": "^2.1.7"
}
}
10 changes: 9 additions & 1 deletion src/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ angular.module("datetime").factory("datetime", function($locale){
// Fetch date and time formats from $locale service
var formats = $locale.DATETIME_FORMATS;
// Valid format tokens. 1=sss, 2=''
var tokenRE = /yyyy|yy|y|M{1,4}|dd?|EEEE?|HH?|hh?|mm?|ss?|([.,])sss|a|Z|ww|w|'(([^']+|'')*)'/g;
var tokenRE = /yyyy|yy|y|M{1,4}|dd?|EEEE?|HH?|hh?|mm?|ss?|([.,])sss|a|Z{1,2}|ww|w|'(([^']+|'')*)'/g;
// Token definition
var definedTokens = {
"y": {
Expand Down Expand Up @@ -184,6 +184,11 @@ angular.module("datetime").factory("datetime", function($locale){
type: "regex",
regex: /[+-]\d{4}/
},
"ZZ": {
name: "timezoneWithColon",
type: "regex",
regex: /[+-]\d{2}:\d{2}/
},
"string": {
name: "string",
type: "static"
Expand Down Expand Up @@ -339,6 +344,9 @@ angular.module("datetime").factory("datetime", function($locale){
case "timezone":
node.value = (date.getTimezoneOffset() > 0 ? "-" : "+") + num2str(Math.abs(date.getTimezoneOffset() / 60), 2, 2) + "00";
break;
case "timezoneWithColon":
node.value = (date.getTimezoneOffset() > 0 ? "-" : "+") + num2str(Math.abs(date.getTimezoneOffset() / 60), 2, 2) + ":00";
break;
}

if (node.value < 0) {
Expand Down
21 changes: 20 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe("datetime directive", function(){
$rootScope.date = new Date;

var element = $compile("<input type='text' datetime='{{format}}' ng-model='date'>")($rootScope);

$rootScope.$digest();

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

expect(element.val()).toEqual("+0000");
});

it("should allow : when using Z:Z token", function(){
$rootScope.date = new Date;

var element = $compile("<input type='text' datetime='ZZ' ng-model='date'>")($rootScope);

$rootScope.$digest();

var timezoneOffset = -new Date().getTimezoneOffset() / 60;
var isNegative = timezoneOffset < 0;

// add leading zeros
if (timezoneOffset < 10) {
timezoneOffset = (isNegative ? "-0" : "+0") + Math.abs(timezoneOffset);
}

expect(element.val()).toEqual(timezoneOffset + ":00");
});

});