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

Commit

Permalink
fix(datepicker): fix OS dependent time zone issue
Browse files Browse the repository at this point in the history
- Fixes an issue with dates involving different OSes and timezones

Fixes #3079
  • Loading branch information
ayakovlevgh authored and wesleycho committed Jul 19, 2015
1 parent 68cac59 commit f141201
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ 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 @@ -236,10 +247,11 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
}

function getDates(startDate, n) {
var dates = new Array(n), current = new Date(startDate), i = 0;
current.setHours(12); // Prevent repeated dates because of timezone bug
var dates = new Array(n), current = new Date(startDate), i = 0, date;
while ( i < n ) {
dates[i++] = new Date(current);
date = new Date(current);
ctrl.fixTimeZone(date);
dates[i++] = date;
current.setDate( current.getDate() + 1 );
}
return dates;
Expand Down Expand Up @@ -341,10 +353,13 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst

ctrl._refreshView = function() {
var months = new Array(12),
year = ctrl.activeDate.getFullYear();
year = ctrl.activeDate.getFullYear(),
date;

for ( var i = 0; i < 12; i++ ) {
months[i] = angular.extend(ctrl.createDateObject(new Date(year, i, 1), ctrl.formatMonth), {
date = new Date(year, i, 1);
ctrl.fixTimeZone(date);
months[i] = angular.extend(ctrl.createDateObject(date, ctrl.formatMonth), {
uid: scope.uniqueId + '-' + i
});
}
Expand Down Expand Up @@ -401,10 +416,12 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
}

ctrl._refreshView = function() {
var years = new Array(range);
var years = new Array(range), date;

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

// issue #3079
describe('time zone bug', function () {

it('should deal with time zone bug', function() {
var ctrl = element.controller('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('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 f141201

Please sign in to comment.