Skip to content

Commit

Permalink
Merge pull request #21 from c-trimm/fix-exception-problem-with-timezones
Browse files Browse the repository at this point in the history
Fix timezone problems
  • Loading branch information
bramski committed Jan 30, 2015
2 parents baf2daf + 6d8850e commit 5627892
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
5 changes: 3 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"dependencies": {
"moment": "< 3.0.0"
},
"devDependencies" : {
"jasmine": "~1.3.0"
"devDependencies": {
"jasmine": "~1.3.0",
"moment-timezone": "~0.3.0"
},
"ignore": [
"**/.*",
Expand Down
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function (config) {
frameworks: ['jasmine'],
files: [
'bower_components/moment/moment.js',
'bower_components/moment-timezone/builds/moment-timezone-with-data.js',
'moment-recur.js',
'tests/spec/*.js',
],
Expand Down
24 changes: 17 additions & 7 deletions moment-recur.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,22 @@
}

function matchInterval(type, units, start, date) {
// Get the difference between the start date and the provded date,
// using the required measure based on the type of rule
var diff = start.diff(date, type, true);
// Get the difference between the start date and the provided date,
// using the required measure based on the type of rule'
var diff = null;
if( date.isBefore(start) ) {
diff = start.diff(date, type, true);
} else {
diff = date.diff(start, type, true);
}
diff = parseInt(diff);

// Check to see if any of the units provided match the date
for (var unit in units) {
if (units.hasOwnProperty(unit)) {
unit = parseInt(unit, 10);

// If the units devide evenly into the difference, we have a match
// If the units divide evenly into the difference, we have a match
if ((diff % unit) === 0) {
return true;
}
Expand Down Expand Up @@ -349,7 +355,6 @@
return true;
}
}

return false;
}

Expand Down Expand Up @@ -441,7 +446,11 @@
this.rules = options.rules || [];

// Our list of exceptions. Match always fails on these dates.
this.exceptions = options.exceptions || [];
var exceptions = options.exceptions || [];
this.exceptions = [];
for(i = 0; i < exceptions.length; i++) {
this.except(exceptions[i]);
}

// Temporary units integer, array, or object. Does not get imported/exported.
this.units = null;
Expand Down Expand Up @@ -559,6 +568,7 @@

// If valid date, try to remove it from exceptions
if (whatMoment.isValid()) {
whatMoment = whatMoment.dateOnly(); // change to date only for perfect comparison
for (i = 0, len = this.exceptions.length; i < len; i++) {
if (whatMoment.isSame(this.exceptions[i])) {
this.exceptions.splice(i, 1);
Expand Down Expand Up @@ -679,7 +689,7 @@

// Plugin for removing all time information from a given date
moment.fn.dateOnly = function() {
return this.hours(0).minutes(0).seconds(0).milliseconds(0);
return this.hours(0).minutes(0).seconds(0).milliseconds(0).add('minute',this.utcOffset()).utcOffset(0);
};


Expand Down
2 changes: 1 addition & 1 deletion moment-recur.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 31 additions & 2 deletions tests/spec/jasmine-event-recur.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,26 +304,55 @@ describe("All Dates", function() {
});

describe("Exceptions", function() {
var mo, exception, recur;
var mo, exception, recur, exceptionWithTz;

beforeEach(function() {
mo = moment(startDate);
exception = mo.clone().add(1, "day");
exception = mo.clone().add(3, "day");
recur = mo.clone().recur().every(1, "days");
exceptionWithTz = moment.tz(exception.format('YYYY-MM-DD'), 'Asia/Hong_Kong');
});

it("should prevent exception days from matching", function() {
recur.except(exception);
expect(recur.matches(exception)).toBe(false);
});

it('should work when the passed in exception is in a different time zone', function() {
recur.except(exception);
expect(recur.matches(exceptionWithTz)).toBe(false);
});

it("should be removeable", function() {
recur.except(exception);
recur.forget(exception);
expect(recur.matches(exception)).toBe(true);
});
});

describe('Exceptions with weeks', function() {
var mo, exception, recur, exceptionWithTz;

beforeEach(function() {
mo = moment(startDate);
exception = mo.clone().add(7, "day");
recur = mo.clone().recur().every(1, "weeks");
exceptionWithTz = moment.tz(exception.format('YYYY-MM-DD'), 'Asia/Hong_Kong');
});

it('should not match on the exception day', function() {
expect(recur.matches(exception)).toBe(true);
recur.except(exception);
expect(recur.matches(exception)).toBe(false);
});

it('should not match on the exception day', function() {
expect(recur.matches(exceptionWithTz)).toBe(true);
recur.except(exception);
expect(recur.matches(exceptionWithTz)).toBe(false);
});
});

describe("Options", function() {
it("should be importable", function() {
var recurrence = moment().recur({
Expand Down

0 comments on commit 5627892

Please sign in to comment.