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

Commit 3a985cf

Browse files
committed
fixup! remove incorrect date reset bugfix
1 parent d172e8e commit 3a985cf

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,19 +1432,26 @@ function createDateInputType(type, regexp, parseDate, format) {
14321432
badInputChecker(scope, element, attr, ctrl);
14331433
baseInputType(scope, element, attr, ctrl, $sniffer, $browser);
14341434
var previousDate;
1435+
var previousTimezone;
14351436

14361437
ctrl.$$parserName = type;
14371438
ctrl.$parsers.push(function(value) {
1438-
if (ctrl.$isEmpty(value)) {
1439-
previousDate = null;
1440-
return null;
1441-
}
1439+
if (ctrl.$isEmpty(value)) return null;
1440+
14421441
if (regexp.test(value)) {
1442+
var timezone = ctrl.$options.getOption('timezone');
1443+
14431444
// Note: We cannot read ctrl.$modelValue, as there might be a different
14441445
// parser/formatter in the processing chain so that the model
14451446
// contains some different data format!
1447+
if (timezone && previousTimezone && previousTimezone !== timezone) {
1448+
// If the timezone has changed, adjust the previousDate to the default timzeone
1449+
// so that the new date is converted with the correct timezone offset
1450+
previousDate = addDateMinutes(previousDate, timezoneToOffset(previousTimezone, 0));
1451+
}
1452+
14461453
var parsedDate = parseDate(value, previousDate);
1447-
var timezone = ctrl.$options.getOption('timezone');
1454+
14481455
if (timezone) {
14491456
parsedDate = convertTimezoneToLocal(parsedDate, timezone);
14501457
}
@@ -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
@@ -330,6 +330,8 @@ defaultModelOptions = new ModelOptions({
330330
* continental US time zone abbreviations, but for general use, use a time zone offset, for
331331
* example, `'+0430'` (4 hours, 30 minutes east of the Greenwich meridian)
332332
* If not specified, the timezone of the browser will be used.
333+
* Note that changing the timezone will have no effect on the current date, and is only applied after
334+
* the next input / model change.
333335
*
334336
*/
335337
var ngModelOptionsDirective = function() {

test/ng/directive/inputSpec.js

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

673673
$rootScope.$apply(function() {
674-
$rootScope.value = new Date(Date.UTC(2014, 6, 1));
674+
$rootScope.value = new Date(Date.UTC(2013, 6, 1));
675675
});
676-
expect(inputElm.val()).toBe('2014-06');
676+
expect(inputElm.val()).toBe('2013-06');
677677
});
678678

679679

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

957957
$rootScope.$apply(function() {
958-
$rootScope.value = new Date(Date.UTC(2014, 0, 17));
958+
// the 17. with an offset of +5000 moves the date into next week
959+
$rootScope.value = new Date(Date.UTC(2013, 0, 18));
959960
});
960-
expect(inputElm.val()).toBe('2014-W04');
961+
expect(inputElm.val()).toBe('2013-W04');
961962
});
962963

963964

@@ -1876,7 +1877,7 @@ describe('input', function() {
18761877

18771878
inputElm.controller('ngModel').$overrideModelOptions({timezone: 'UTC'});
18781879
helper.changeInputValueTo('2000-01-01');
1879-
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 19));
1880+
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 0));
18801881
});
18811882

18821883

@@ -1965,21 +1966,21 @@ describe('input', function() {
19651966
dealoc(formElm);
19661967
});
19671968

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

19711972
helper.changeInputValueTo('2000-01-01');
1972-
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1));
1973+
expect(+$rootScope.value).toBe(Date.UTC(2000, 0, 1, 0));
19731974

19741975
// Change the timezone offset so that the display date is a day earlier
19751976
// This does not change the model, but our implementation
19761977
// internally caches a Date object with this offset
19771978
// and re-uses it if part of the date changes
19781979
inputElm.controller('ngModel').$overrideModelOptions({timezone: '-0500'});
19791980
$rootScope.$apply(function() {
1980-
$rootScope.value = new Date(Date.UTC(2001, 0, 1));
1981+
$rootScope.value = new Date(Date.UTC(2000, 0, 1, 0));
19811982
});
1982-
expect(inputElm.val()).toBe('2000-12-31');
1983+
expect(inputElm.val()).toBe('1999-12-31');
19831984

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

0 commit comments

Comments
 (0)