Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 8cf76e4

Browse files
committed
fixup! remove incorrect date reset bugfix
1 parent 0960096 commit 8cf76e4

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

src/.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"toJsonReplacer": false,
8080
"toJson": false,
8181
"fromJson": false,
82+
"addDateMinutes": false,
8283
"convertTimezoneToLocal": false,
8384
"timezoneToOffset": false,
8485
"startingTag": false,

src/Angular.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
fromJson,
7676
convertTimezoneToLocal,
7777
timezoneToOffset,
78+
addDateMinutes,
7879
startingTag,
7980
tryDecodeURIComponent,
8081
parseKeyValue,

src/ng/directive/input.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,18 +1432,25 @@ function createDateInputType(type, regexp, parseDate, format) {
14321432
badInputChecker(scope, element, attr, ctrl, type);
14331433
baseInputType(scope, element, attr, ctrl, $sniffer, $browser);
14341434
var previousDate;
1435+
var previousTimezone;
14351436

14361437
ctrl.$parsers.push(function(value) {
1437-
if (ctrl.$isEmpty(value)) {
1438-
previousDate = null;
1439-
return null;
1440-
}
1438+
if (ctrl.$isEmpty(value)) return null;
1439+
14411440
if (regexp.test(value)) {
1441+
var timezone = ctrl.$options.getOption('timezone');
1442+
14421443
// Note: We cannot read ctrl.$modelValue, as there might be a different
14431444
// parser/formatter in the processing chain so that the model
14441445
// contains some different data format!
1446+
if (timezone && previousTimezone && previousTimezone !== timezone) {
1447+
// If the timezone has changed, adjust the previousDate to the default timzeone
1448+
// so that the new date is converted with the correct timezone offset
1449+
previousDate = addDateMinutes(previousDate, timezoneToOffset(previousTimezone, 0));
1450+
}
1451+
14451452
var parsedDate = parseDate(value, previousDate);
1446-
var timezone = ctrl.$options.getOption('timezone');
1453+
14471454
if (timezone) {
14481455
parsedDate = convertTimezoneToLocal(parsedDate, timezone);
14491456
}
@@ -1460,12 +1467,14 @@ function createDateInputType(type, regexp, parseDate, format) {
14601467
if (isValidDate(value)) {
14611468
previousDate = value;
14621469
var timezone = ctrl.$options.getOption('timezone');
1463-
if (previousDate && timezone) {
1470+
if (timezone) {
1471+
previousTimezone = timezone;
14641472
previousDate = convertTimezoneToLocal(previousDate, timezone, true);
14651473
}
14661474
return $filter('date')(value, format, timezone);
14671475
} else {
14681476
previousDate = null;
1477+
previousTimezone = null;
14691478
return '';
14701479
}
14711480
});

src/ng/directive/ngModelOptions.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ defaultModelOptions = new ModelOptions({
462462
* continental US time zone abbreviations, but for general use, use a time zone offset, for
463463
* example, `'+0430'` (4 hours, 30 minutes east of the Greenwich meridian)
464464
* If not specified, the timezone of the browser will be used.
465+
* Note that changing the timezone will have no effect on the current date, and is only applied after
466+
* the next input / model change.
465467
*
466468
*/
467469
var ngModelOptionsDirective = function() {

test/ng/directive/inputSpec.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -702,9 +702,9 @@ describe('input', function() {
702702
inputElm.controller('ngModel').$overrideModelOptions({timezone: '-0500'});
703703

704704
$rootScope.$apply(function() {
705-
$rootScope.value = new Date(Date.UTC(2014, 6, 1));
705+
$rootScope.value = new Date(Date.UTC(2013, 6, 1));
706706
});
707-
expect(inputElm.val()).toBe('2014-06');
707+
expect(inputElm.val()).toBe('2013-06');
708708
});
709709

710710

@@ -986,9 +986,10 @@ describe('input', function() {
986986
inputElm.controller('ngModel').$overrideModelOptions({timezone: '+5000'});
987987

988988
$rootScope.$apply(function() {
989-
$rootScope.value = new Date(Date.UTC(2014, 0, 17));
989+
// the 17. with an offset of +5000 moves the date into next week
990+
$rootScope.value = new Date(Date.UTC(2013, 0, 18));
990991
});
991-
expect(inputElm.val()).toBe('2014-W04');
992+
expect(inputElm.val()).toBe('2013-W04');
992993
});
993994

994995

@@ -1907,7 +1908,7 @@ describe('input', function() {
19071908

19081909
inputElm.controller('ngModel').$overrideModelOptions({timezone: 'UTC'});
19091910
helper.changeInputValueTo('2000-01-01');
1910-
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 19));
1911+
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 0));
19111912
});
19121913

19131914

@@ -1996,21 +1997,21 @@ describe('input', function() {
19961997
dealoc(formElm);
19971998
});
19981999

1999-
it('should not reuse the hour part of a previous date object after emptying the input', function() {
2000+
it('should not reuse the hour part of a previous date object after changing the timezone', function() {
20002001
var inputElm = helper.compileInput('<input type="date" ng-model="value" ng-model-options="{timezone: \'UTC\'}" />');
20012002

20022003
helper.changeInputValueTo('2000-01-01');
2003-
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1));
2004+
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 0));
20042005

20052006
// Change the timezone offset so that the display date is a day earlier
20062007
// This does not change the model, but our implementation
20072008
// internally caches a Date object with this offset
20082009
// and re-uses it if part of the date changes
20092010
inputElm.controller('ngModel').$overrideModelOptions({timezone: '-0500'});
20102011
$rootScope.$apply(function() {
2011-
$rootScope.value = new Date(Date.UTC(2001, 0, 1));
2012+
$rootScope.value = new Date(Date.UTC(2000, 0, 1, 0));
20122013
});
2013-
expect(inputElm.val()).toBe('2000-12-31');
2014+
expect(inputElm.val()).toBe('1999-12-31');
20142015

20152016
// Emptying the input should clear the cached date object
20162017
helper.changeInputValueTo('');

0 commit comments

Comments
 (0)