Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
feat(datepicker): preserve timezone with model
Browse files Browse the repository at this point in the history
- Default to using the date instance for any date manipulation, allowing
  for the timezone to automatically be preserved

Closes #4676
  • Loading branch information
wesleycho committed Oct 23, 2015
1 parent d3056c7 commit 0d64aad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 47 deletions.
13 changes: 10 additions & 3 deletions src/dateparser/dateparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,16 @@ angular.module('ui.bootstrap.dateparser', [])
}

if (isValid(fields.year, fields.month, fields.date)) {
dt = new Date(fields.year, fields.month, fields.date,
fields.hours, fields.minutes, fields.seconds,
fields.milliseconds || 0);
if (angular.isDate(baseDate) && !isNaN(baseDate.getTime())) {
dt = new Date(baseDate);
dt.setFullYear(fields.year, fields.month, fields.date,
fields.hours, fields.minutes, fields.seconds,
fields.milliseconds || 0);
} else {
dt = new Date(fields.year, fields.month, fields.date,
fields.hours, fields.minutes, fields.seconds,
fields.milliseconds || 0);
}
}

return dt;
Expand Down
27 changes: 9 additions & 18 deletions src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,6 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
return arrays;
};

// Fix a hard-reprodusible bug with timezones
// The bug depends on OS, browser, current timezone and current date
// i.e.
// var date = new Date(2014, 0, 1);
// console.log(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours());
// can result in "2013 11 31 23" because of the bug.
this.fixTimeZone = function(date) {
var hours = date.getHours();
date.setHours(hours === 23 ? hours + 2 : 0);
};

$scope.select = function(date) {
if ($scope.datepickerMode === self.minMode) {
var dt = ngModelCtrl.$viewValue ? new Date(ngModelCtrl.$viewValue) : new Date(0, 0, 0, 0, 0, 0, 0);
Expand Down Expand Up @@ -238,7 +227,6 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
var dates = new Array(n), current = new Date(startDate), i = 0, date;
while (i < n) {
date = new Date(current);
this.fixTimeZone(date);
dates[i++] = date;
current.setDate(current.getDate() + 1);
}
Expand All @@ -248,8 +236,11 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
this._refreshView = function() {
var year = this.activeDate.getFullYear(),
month = this.activeDate.getMonth(),
firstDayOfMonth = new Date(year, month, 1),
difference = this.startingDay - firstDayOfMonth.getDay(),
firstDayOfMonth = new Date(this.activeDate);

firstDayOfMonth.setFullYear(year, month, 1);

var difference = this.startingDay - firstDayOfMonth.getDay(),
numDisplayedFromPreviousMonth = (difference > 0) ? 7 - difference : - difference,
firstDate = new Date(firstDayOfMonth);

Expand Down Expand Up @@ -340,8 +331,8 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
date;

for (var i = 0; i < 12; i++) {
date = new Date(year, i, 1);
this.fixTimeZone(date);
date = new Date(this.activeDate);
date.setFullYear(year, i, 1);
months[i] = angular.extend(this.createDateObject(date, this.formatMonth), {
uid: scope.uniqueId + '-' + i
});
Expand Down Expand Up @@ -395,8 +386,8 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
var years = new Array(range), date;

for (var i = 0, start = getStartingYear(this.activeDate.getFullYear()); i < range; i++) {
date = new Date(start + i, 0, 1);
this.fixTimeZone(date);
date = new Date(this.activeDate);
date.setFullYear(start + i, 0, 1);
years[i] = angular.extend(this.createDateObject(date, this.formatYear), {
uid: scope.uniqueId + '-' + i
});
Expand Down
26 changes: 0 additions & 26 deletions src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,32 +397,6 @@ describe('datepicker directive', function() {
expect(element.html()).toBe('baz');
});

// issue #3079
describe('time zone bug', function() {
it('should deal with time zone bug', function() {
var ctrl = element.controller('uib-datepicker'),
date = new Date('January 1, 2014');
spyOn(date, 'getHours').and.returnValue(23);
spyOn(date, 'setHours').and.returnValue();

ctrl.fixTimeZone(date);

expect(date.setHours).toHaveBeenCalledWith(25);
});

it('should not change hours if time zone bug does not occur', function() {
var ctrl = element.controller('uib-datepicker'),
date = new Date('January 1, 2014');
spyOn(date, 'getHours').and.returnValue(0);
spyOn(date, 'setHours').and.returnValue();

ctrl.fixTimeZone(date);

expect(date.setHours).toHaveBeenCalledWith(0);
});

});

describe('when `model` changes', function() {
function testCalendar() {
expect(getTitle()).toBe('November 2005');
Expand Down

0 comments on commit 0d64aad

Please sign in to comment.