@@ -688,6 +688,21 @@ describe('input', function() {
688688 } ) ;
689689
690690
691+ it ( 'should be possible to override the timezone' , function ( ) {
692+ var inputElm = helper . compileInput ( '<input type="month" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />' ) ;
693+
694+ helper . changeInputValueTo ( '2013-07' ) ;
695+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 2013 , 6 , 1 ) ) ;
696+
697+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : '-0500' } ) ;
698+
699+ $rootScope . $apply ( function ( ) {
700+ $rootScope . value = new Date ( Date . UTC ( 2013 , 6 , 1 ) ) ;
701+ } ) ;
702+ expect ( inputElm . val ( ) ) . toBe ( '2013-06' ) ;
703+ } ) ;
704+
705+
691706 they ( 'should use any timezone if specified in the options (format: $prop)' ,
692707 { '+HHmm' : '+0500' , '+HH:mm' : '+05:00' } ,
693708 function ( tz ) {
@@ -974,6 +989,30 @@ describe('input', function() {
974989 } ) ;
975990
976991
992+ it ( 'should be possible to override the timezone' , function ( ) {
993+ var inputElm = helper . compileInput ( '<input type="week" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />' ) ;
994+
995+ // January 19 2013 is a Saturday
996+ $rootScope . $apply ( function ( ) {
997+ $rootScope . value = new Date ( Date . UTC ( 2013 , 0 , 19 ) ) ;
998+ } ) ;
999+
1000+ expect ( inputElm . val ( ) ) . toBe ( '2013-W03' ) ;
1001+
1002+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : '+2400' } ) ;
1003+
1004+ // To check that the timezone overwrite works, apply an offset of +24 hours.
1005+ // Since January 19 is a Saturday, +24 will turn the formatted Date into January 20 - Sunday -
1006+ // which is in calendar week 4 instead of 3.
1007+ $rootScope . $apply ( function ( ) {
1008+ $rootScope . value = new Date ( Date . UTC ( 2013 , 0 , 19 ) ) ;
1009+ } ) ;
1010+
1011+ // Verifying that the displayed week is week 4 confirms that overriding the timezone worked
1012+ expect ( inputElm . val ( ) ) . toBe ( '2013-W04' ) ;
1013+ } ) ;
1014+
1015+
9771016 they ( 'should use any timezone if specified in the options (format: $prop)' ,
9781017 { '+HHmm' : '+0500' , '+HH:mm' : '+05:00' } ,
9791018 function ( tz ) {
@@ -1199,6 +1238,25 @@ describe('input', function() {
11991238 } ) ;
12001239
12011240
1241+ it ( 'should be possible to override the timezone' , function ( ) {
1242+ var inputElm = helper . compileInput ( '<input type="datetime-local" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />' ) ;
1243+
1244+ helper . changeInputValueTo ( '2000-01-01T01:02' ) ;
1245+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 2000 , 0 , 1 , 1 , 2 , 0 ) ) ;
1246+
1247+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : '+0500' } ) ;
1248+ $rootScope . $apply ( function ( ) {
1249+ $rootScope . value = new Date ( Date . UTC ( 2001 , 0 , 1 , 1 , 2 , 0 ) ) ;
1250+ } ) ;
1251+ expect ( inputElm . val ( ) ) . toBe ( '2001-01-01T06:02:00.000' ) ;
1252+
1253+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : 'UTC' } ) ;
1254+
1255+ helper . changeInputValueTo ( '2000-01-01T01:02' ) ;
1256+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 2000 , 0 , 1 , 1 , 2 , 0 ) ) ;
1257+ } ) ;
1258+
1259+
12021260 they ( 'should use any timezone if specified in the options (format: $prop)' ,
12031261 { '+HHmm' : '+0500' , '+HH:mm' : '+05:00' } ,
12041262 function ( tz ) {
@@ -1561,6 +1619,25 @@ describe('input', function() {
15611619 } ) ;
15621620
15631621
1622+ it ( 'should be possible to override the timezone' , function ( ) {
1623+ var inputElm = helper . compileInput ( '<input type="time" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />' ) ;
1624+
1625+ helper . changeInputValueTo ( '23:02:00' ) ;
1626+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 1970 , 0 , 1 , 23 , 2 , 0 ) ) ;
1627+
1628+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : '-0500' } ) ;
1629+ $rootScope . $apply ( function ( ) {
1630+ $rootScope . value = new Date ( Date . UTC ( 1971 , 0 , 1 , 23 , 2 , 0 ) ) ;
1631+ } ) ;
1632+ expect ( inputElm . val ( ) ) . toBe ( '18:02:00.000' ) ;
1633+
1634+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : 'UTC' } ) ;
1635+ helper . changeInputValueTo ( '23:02:00' ) ;
1636+ // The year is still set from the previous date
1637+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 1971 , 0 , 1 , 23 , 2 , 0 ) ) ;
1638+ } ) ;
1639+
1640+
15641641 they ( 'should use any timezone if specified in the options (format: $prop)' ,
15651642 { '+HHmm' : '+0500' , '+HH:mm' : '+05:00' } ,
15661643 function ( tz ) {
@@ -1890,6 +1967,24 @@ describe('input', function() {
18901967 } ) ;
18911968
18921969
1970+ it ( 'should be possible to override the timezone' , function ( ) {
1971+ var inputElm = helper . compileInput ( '<input type="date" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />' ) ;
1972+
1973+ helper . changeInputValueTo ( '2000-01-01' ) ;
1974+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 2000 , 0 , 1 ) ) ;
1975+
1976+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : '-0500' } ) ;
1977+ $rootScope . $apply ( function ( ) {
1978+ $rootScope . value = new Date ( Date . UTC ( 2001 , 0 , 1 ) ) ;
1979+ } ) ;
1980+ expect ( inputElm . val ( ) ) . toBe ( '2000-12-31' ) ;
1981+
1982+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : 'UTC' } ) ;
1983+ helper . changeInputValueTo ( '2000-01-01' ) ;
1984+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 2000 , 0 , 1 , 0 ) ) ;
1985+ } ) ;
1986+
1987+
18931988 they ( 'should use any timezone if specified in the options (format: $prop)' ,
18941989 { '+HHmm' : '+0500' , '+HH:mm' : '+05:00' } ,
18951990 function ( tz ) {
@@ -1975,6 +2070,34 @@ describe('input', function() {
19752070 dealoc ( formElm ) ;
19762071 } ) ;
19772072
2073+ it ( 'should not reuse the hours part of a previous date object after changing the timezone' , function ( ) {
2074+ var inputElm = helper . compileInput ( '<input type="date" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />' ) ;
2075+
2076+ helper . changeInputValueTo ( '2000-01-01' ) ;
2077+ // The Date parser sets the hours part of the Date to 0 (00:00) (UTC)
2078+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 2000 , 0 , 1 , 0 ) ) ;
2079+
2080+ // Change the timezone offset so that the display date is a day earlier
2081+ // This does not change the model, but our implementation
2082+ // internally caches a Date object with this offset
2083+ // and re-uses it if part of the Date changes.
2084+ // See https://github.com/angular/angular.js/commit/1a1ef62903c8fdf4ceb81277d966a8eff67f0a96
2085+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : '-0500' } ) ;
2086+ $rootScope . $apply ( function ( ) {
2087+ $rootScope . value = new Date ( Date . UTC ( 2000 , 0 , 1 , 0 ) ) ;
2088+ } ) ;
2089+ expect ( inputElm . val ( ) ) . toBe ( '1999-12-31' ) ;
2090+
2091+ // At this point, the cached Date has its hours set to to 19 (00:00 - 05:00 = 19:00)
2092+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : 'UTC' } ) ;
2093+
2094+ // When changing the timezone back to UTC, the hours part of the Date should be set to
2095+ // the default 0 (UTC) and not use the modified value of the cached Date object.
2096+ helper . changeInputValueTo ( '2000-01-01' ) ;
2097+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 2000 , 0 , 1 , 0 ) ) ;
2098+ } ) ;
2099+
2100+
19782101 describe ( 'min' , function ( ) {
19792102
19802103 it ( 'should invalidate' , function ( ) {
0 commit comments