@@ -718,6 +718,21 @@ describe('input', function() {
718718 } ) ;
719719
720720
721+ it ( 'should be possible to override the timezone' , function ( ) {
722+ var inputElm = helper . compileInput ( '<input type="month" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />' ) ;
723+
724+ helper . changeInputValueTo ( '2013-07' ) ;
725+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 2013 , 6 , 1 ) ) ;
726+
727+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : '-0500' } ) ;
728+
729+ $rootScope . $apply ( function ( ) {
730+ $rootScope . value = new Date ( Date . UTC ( 2013 , 6 , 1 ) ) ;
731+ } ) ;
732+ expect ( inputElm . val ( ) ) . toBe ( '2013-06' ) ;
733+ } ) ;
734+
735+
721736 they ( 'should use any timezone if specified in the options (format: $prop)' ,
722737 { '+HHmm' : '+0500' , '+HH:mm' : '+05:00' } ,
723738 function ( tz ) {
@@ -1004,6 +1019,30 @@ describe('input', function() {
10041019 } ) ;
10051020
10061021
1022+ it ( 'should be possible to override the timezone' , function ( ) {
1023+ var inputElm = helper . compileInput ( '<input type="week" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />' ) ;
1024+
1025+ // January 19 2013 is a Saturday
1026+ $rootScope . $apply ( function ( ) {
1027+ $rootScope . value = new Date ( Date . UTC ( 2013 , 0 , 19 ) ) ;
1028+ } ) ;
1029+
1030+ expect ( inputElm . val ( ) ) . toBe ( '2013-W03' ) ;
1031+
1032+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : '+2400' } ) ;
1033+
1034+ // To check that the timezone overwrite works, apply an offset of +24 hours.
1035+ // Since January 19 is a Saturday, +24 will turn the formatted Date into January 20 - Sunday -
1036+ // which is in calendar week 4 instead of 3.
1037+ $rootScope . $apply ( function ( ) {
1038+ $rootScope . value = new Date ( Date . UTC ( 2013 , 0 , 19 ) ) ;
1039+ } ) ;
1040+
1041+ // Verifying that the displayed week is week 4 confirms that overriding the timezone worked
1042+ expect ( inputElm . val ( ) ) . toBe ( '2013-W04' ) ;
1043+ } ) ;
1044+
1045+
10071046 they ( 'should use any timezone if specified in the options (format: $prop)' ,
10081047 { '+HHmm' : '+0500' , '+HH:mm' : '+05:00' } ,
10091048 function ( tz ) {
@@ -1229,6 +1268,25 @@ describe('input', function() {
12291268 } ) ;
12301269
12311270
1271+ it ( 'should be possible to override the timezone' , function ( ) {
1272+ var inputElm = helper . compileInput ( '<input type="datetime-local" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />' ) ;
1273+
1274+ helper . changeInputValueTo ( '2000-01-01T01:02' ) ;
1275+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 2000 , 0 , 1 , 1 , 2 , 0 ) ) ;
1276+
1277+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : '+0500' } ) ;
1278+ $rootScope . $apply ( function ( ) {
1279+ $rootScope . value = new Date ( Date . UTC ( 2001 , 0 , 1 , 1 , 2 , 0 ) ) ;
1280+ } ) ;
1281+ expect ( inputElm . val ( ) ) . toBe ( '2001-01-01T06:02:00.000' ) ;
1282+
1283+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : 'UTC' } ) ;
1284+
1285+ helper . changeInputValueTo ( '2000-01-01T01:02' ) ;
1286+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 2000 , 0 , 1 , 1 , 2 , 0 ) ) ;
1287+ } ) ;
1288+
1289+
12321290 they ( 'should use any timezone if specified in the options (format: $prop)' ,
12331291 { '+HHmm' : '+0500' , '+HH:mm' : '+05:00' } ,
12341292 function ( tz ) {
@@ -1591,6 +1649,25 @@ describe('input', function() {
15911649 } ) ;
15921650
15931651
1652+ it ( 'should be possible to override the timezone' , function ( ) {
1653+ var inputElm = helper . compileInput ( '<input type="time" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />' ) ;
1654+
1655+ helper . changeInputValueTo ( '23:02:00' ) ;
1656+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 1970 , 0 , 1 , 23 , 2 , 0 ) ) ;
1657+
1658+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : '-0500' } ) ;
1659+ $rootScope . $apply ( function ( ) {
1660+ $rootScope . value = new Date ( Date . UTC ( 1971 , 0 , 1 , 23 , 2 , 0 ) ) ;
1661+ } ) ;
1662+ expect ( inputElm . val ( ) ) . toBe ( '18:02:00.000' ) ;
1663+
1664+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : 'UTC' } ) ;
1665+ helper . changeInputValueTo ( '23:02:00' ) ;
1666+ // The year is still set from the previous date
1667+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 1971 , 0 , 1 , 23 , 2 , 0 ) ) ;
1668+ } ) ;
1669+
1670+
15941671 they ( 'should use any timezone if specified in the options (format: $prop)' ,
15951672 { '+HHmm' : '+0500' , '+HH:mm' : '+05:00' } ,
15961673 function ( tz ) {
@@ -1920,6 +1997,24 @@ describe('input', function() {
19201997 } ) ;
19211998
19221999
2000+ it ( 'should be possible to override the timezone' , function ( ) {
2001+ var inputElm = helper . compileInput ( '<input type="date" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />' ) ;
2002+
2003+ helper . changeInputValueTo ( '2000-01-01' ) ;
2004+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 2000 , 0 , 1 ) ) ;
2005+
2006+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : '-0500' } ) ;
2007+ $rootScope . $apply ( function ( ) {
2008+ $rootScope . value = new Date ( Date . UTC ( 2001 , 0 , 1 ) ) ;
2009+ } ) ;
2010+ expect ( inputElm . val ( ) ) . toBe ( '2000-12-31' ) ;
2011+
2012+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : 'UTC' } ) ;
2013+ helper . changeInputValueTo ( '2000-01-01' ) ;
2014+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 2000 , 0 , 1 , 0 ) ) ;
2015+ } ) ;
2016+
2017+
19232018 they ( 'should use any timezone if specified in the options (format: $prop)' ,
19242019 { '+HHmm' : '+0500' , '+HH:mm' : '+05:00' } ,
19252020 function ( tz ) {
@@ -2005,6 +2100,34 @@ describe('input', function() {
20052100 dealoc ( formElm ) ;
20062101 } ) ;
20072102
2103+ it ( 'should not reuse the hours part of a previous date object after changing the timezone' , function ( ) {
2104+ var inputElm = helper . compileInput ( '<input type="date" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />' ) ;
2105+
2106+ helper . changeInputValueTo ( '2000-01-01' ) ;
2107+ // The Date parser sets the hours part of the Date to 0 (00:00) (UTC)
2108+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 2000 , 0 , 1 , 0 ) ) ;
2109+
2110+ // Change the timezone offset so that the display date is a day earlier
2111+ // This does not change the model, but our implementation
2112+ // internally caches a Date object with this offset
2113+ // and re-uses it if part of the Date changes.
2114+ // See https://github.com/angular/angular.js/commit/1a1ef62903c8fdf4ceb81277d966a8eff67f0a96
2115+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : '-0500' } ) ;
2116+ $rootScope . $apply ( function ( ) {
2117+ $rootScope . value = new Date ( Date . UTC ( 2000 , 0 , 1 , 0 ) ) ;
2118+ } ) ;
2119+ expect ( inputElm . val ( ) ) . toBe ( '1999-12-31' ) ;
2120+
2121+ // At this point, the cached Date has its hours set to to 19 (00:00 - 05:00 = 19:00)
2122+ inputElm . controller ( 'ngModel' ) . $overrideModelOptions ( { timezone : 'UTC' } ) ;
2123+
2124+ // When changing the timezone back to UTC, the hours part of the Date should be set to
2125+ // the default 0 (UTC) and not use the modified value of the cached Date object.
2126+ helper . changeInputValueTo ( '2000-01-01' ) ;
2127+ expect ( + $rootScope . value ) . toBe ( Date . UTC ( 2000 , 0 , 1 , 0 ) ) ;
2128+ } ) ;
2129+
2130+
20082131 describe ( 'min' , function ( ) {
20092132
20102133 it ( 'should invalidate' , function ( ) {
0 commit comments