Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support timezone using timezone-js. #45

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 74 additions & 26 deletions lib/rrule.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
(function(root){

var serverSide = typeof module !== 'undefined' && module.exports;
var _;
var _, timezoneJS;

if (serverSide) {
_ = require('underscore');
Expand All @@ -27,6 +27,20 @@ if (serverSide) {
_ = require('underscore');
}

if (serverSide) {
try {
timezoneJS = require('timezone-js');
} catch(err) {
// No timezone-js installed
}
} else if (root.timezoneJS) {
timezoneJS = root.timezoneJS;
} else if (!timezoneJS && (typeof require !== 'undefined')){
//Client needs to load timezone-js with requirejs.
timezoneJS = require('timezone-js');
}

var DateClass = timezoneJS ? timezoneJS.Date : Date;

var getnlp = function() {
if (!getnlp._nlp) {
Expand Down Expand Up @@ -132,7 +146,7 @@ var dateutil = {
var millisecsFromBase = ordinal * dateutil.ONE_DAY;
return new Date(dateutil.ORDINAL_BASE.getTime()
- dateutil.tzOffset(dateutil.ORDINAL_BASE)
+ millisecsFromBase
+ millisecsFromBase
+ dateutil.tzOffset(new Date(millisecsFromBase)));
},

Expand Down Expand Up @@ -163,15 +177,24 @@ var dateutil = {
*/
combine: function(date, time) {
time = time || date;
return new Date(
date.getFullYear(), date.getMonth(), date.getDate(),
time.getHours(), time.getMinutes(), time.getSeconds()
);
return timezoneJS && time.getTimezone()
? new DateClass(
date.getFullYear(), date.getMonth(), date.getDate(),
time.getHours(), time.getMinutes(), time.getSeconds(),
time.getTimezone()
)
: new DateClass(
date.getFullYear(), date.getMonth(), date.getDate(),
time.getHours(), time.getMinutes(), time.getSeconds()
);
},

clone: function(date) {
var dolly = new Date(date.getTime());
var dolly = new DateClass(date.getTime());
dolly.setMilliseconds(0);
if (timezoneJS) {
dolly.setTimezone(date.getTimezone());
}
return dolly;
},

Expand Down Expand Up @@ -213,28 +236,38 @@ var dateutil = {
return comps.join('');
},

untilStringToDate: function(until) {
untilStringToDate: function(until, timezone) {
var re = /^(\d{4})(\d{2})(\d{2})(T(\d{2})(\d{2})(\d{2})Z)?$/;
var bits = re.exec(until);
if (!bits) {
throw new Error('Invalid UNTIL value: ' + until)
}
return new Date(
Date.UTC(bits[1],
bits[2] - 1,
bits[3],
bits[5] || 0,
bits[6] || 0,
bits[7] || 0
));
return timezoneJS && timezone
? new DateClass(
Date.UTC(bits[1],
bits[2] - 1,
bits[3],
bits[5] || 0,
bits[6] || 0,
bits[7] || 0
), timezone)
: new DateClass(
Date.UTC(bits[1],
bits[2] - 1,
bits[3],
bits[5] || 0,
bits[6] || 0,
bits[7] || 0
));
}

};

dateutil.Time = function(hour, minute, second) {
dateutil.Time = function(hour, minute, second, timezone) {
this.hour = hour;
this.minute = minute;
this.second = second;
this.timezone = timezone;
};

dateutil.Time.prototype = {
Expand All @@ -247,6 +280,9 @@ dateutil.Time.prototype = {
getSeconds: function() {
return this.second;
},
getTimezone: function() {
return this.timezone;
},
getTime: function() {
return ((this.hour * 60 * 60)
+ (this.minute * 60)
Expand Down Expand Up @@ -495,10 +531,12 @@ var RRule = function(options, noCache) {
}

if (!opts.dtstart) {
opts.dtstart = new Date();
opts.dtstart = new DateClass();
opts.dtstart.setMilliseconds(0);
}

this.timezone = opts.dtstart.timezone;

if (opts.wkst === null) {
opts.wkst = RRule.MO.weekday;
} else if (typeof opts.wkst == 'number') {
Expand Down Expand Up @@ -663,7 +701,7 @@ var RRule = function(options, noCache) {
// python:
// datetime.time(hour, minute, second,
// tzinfo=self._tzinfo))
this.timeset.push(new dateutil.Time(hour, minute, second));
this.timeset.push(new dateutil.Time(hour, minute, second, this.timezone));
}
}
}
Expand Down Expand Up @@ -777,6 +815,10 @@ RRule.optionsToString = function(options) {
value = strValues;
break;
case'DTSTART':
if (value.timezone) {
pairs.push(['TZID', value.timezone]);
}
/* falls through */
case'UNTIL':
value = dateutil.timeToUntilString(value);
break;
Expand Down Expand Up @@ -928,7 +970,7 @@ RRule.prototype = {
if (!this._cache) return;

if (value) {
value = (value instanceof Date)
value = (value instanceof DateClass)
? dateutil.clone(value)
: dateutil.cloneDates(value);
}
Expand Down Expand Up @@ -991,7 +1033,7 @@ RRule.prototype = {

return cached instanceof Array
? dateutil.cloneDates(cached)
: (cached instanceof Date
: (cached instanceof DateClass
? dateutil.clone(cached)
: cached);
},
Expand Down Expand Up @@ -1380,7 +1422,7 @@ RRule.parseString = function(rfcString) {
return null;
}

var i, j, key, value, attr,
var i, j, key, value, attr, dtstart, timezone,
attrs = rfcString.split(';'),
options = {};

Expand Down Expand Up @@ -1436,7 +1478,10 @@ RRule.parseString = function(rfcString) {
}
break;
case 'DTSTART':
options.dtstart = dateutil.untilStringToDate(value);
dtstart = value;
break;
case 'TZID':
timezone = value;
break;
case 'UNTIL':
options.until = dateutil.untilStringToDate(value);
Expand All @@ -1448,6 +1493,9 @@ RRule.parseString = function(rfcString) {
throw new Error("Unknown RRULE property '" + key + "'");
}
}
if (dtstart) {
options.dtstart = dateutil.untilStringToDate(dtstart, timezone);
}
return options;
};

Expand Down Expand Up @@ -1729,7 +1777,7 @@ Iterinfo.prototype.htimeset = function(hour, minute, second) {
minute = rr.options.byminute[i];
for (var j = 0; j < rr.options.bysecond.length; j++) {
second = rr.options.bysecond[j];
set.push(new dateutil.Time(hour, minute, second));
set.push(new dateutil.Time(hour, minute, second, rr.timezone));
}
}
dateutil.sort(set);
Expand All @@ -1740,14 +1788,14 @@ Iterinfo.prototype.mtimeset = function(hour, minute, second) {
var set = [], rr = this.rrule;
for (var j = 0; j < rr.options.bysecond.length; j++) {
second = rr.options.bysecond[j];
set.push(new dateutil.Time(hour, minute, second));
set.push(new dateutil.Time(hour, minute, second, rr.timezone));
}
dateutil.sort(set);
return set;
};

Iterinfo.prototype.stimeset = function(hour, minute, second) {
return [new dateutil.Time(hour, minute, second)];
return [new dateutil.Time(hour, minute, second, this.rrule.timezone)];
};


Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
},
"dependencies": {
"underscore": ">= 1.3.3"
},
"devDependencies": {
"timezone-js": ">= 0.4.7"
}
}
1 change: 1 addition & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<script src="vendor/jquery.js"></script>
<script src="vendor/qunit.js"></script>
<script src="vendor/underscore.js"></script>
<script src="vendor/timezone-js-date.js"></script>

<script src="../lib/rrule.js"></script>
<script src="../lib/nlp.js"></script>
Expand Down
33 changes: 32 additions & 1 deletion tests/tests.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
if (window.timezoneJS) {
timezoneJS.timezone.zoneFileBasePath = 'vendor/tz';
timezoneJS.timezone.init();
}

module("RRule", {

Expand Down Expand Up @@ -2259,7 +2263,34 @@ testRecurring('testMaxYear', new RRule({freq: RRule.YEARLY,
dtstart:parse("99970902T090000")}),
[]);


/* test timezone */
testRecurring('testTimezone', new RRule({freq: RRule.WEEKLY,
count:5,
byweekday:[RRule.TU],
dtstart:datetimeTz('America/New_York', 1997, 9, 2, 23, 0)}),
[datetimeTz('America/New_York', 1997, 9, 2, 23, 0),
datetimeTz('America/New_York', 1997, 9, 9, 23, 0),
datetimeTz('America/New_York', 1997, 9, 16, 23, 0),
datetimeTz('America/New_York', 1997, 9, 23, 23, 0),
datetimeTz('America/New_York', 1997, 9, 30, 23, 0)]);

testRecurring('testTimezoneNoDST', new RRule({freq: RRule.HOURLY,
count:5,
dtstart:datetimeTz('Europe/London', 2013, 3, 30, 0, 0)}),
[datetimeTz('Asia/Bangkok', 2013, 3, 30, 7, 0),
datetimeTz('Asia/Bangkok', 2013, 3, 30, 8, 0),
datetimeTz('Asia/Bangkok', 2013, 3, 30, 9, 0),
datetimeTz('Asia/Bangkok', 2013, 3, 30, 10, 0),
datetimeTz('Asia/Bangkok', 2013, 3, 30, 11, 0)]);

testRecurring('testTimezoneDST', new RRule({freq: RRule.HOURLY,
count:5,
dtstart:datetimeTz('Europe/London', 2013, 4, 1, 0, 0)}),
[datetimeTz('Asia/Bangkok', 2013, 4, 1, 6, 0),
datetimeTz('Asia/Bangkok', 2013, 4, 1, 7, 0),
datetimeTz('Asia/Bangkok', 2013, 4, 1, 8, 0),
datetimeTz('Asia/Bangkok', 2013, 4, 1, 9, 0),
datetimeTz('Asia/Bangkok', 2013, 4, 1, 10, 0)]);


/* these tests basically test the iterator implementation only */
Expand Down
17 changes: 15 additions & 2 deletions tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ date = datetime = function(y, m, d, h, i, s) {
return new Date(y, m - 1, d, h, i, s);
};

var dateTz, datetimeTz;
dateTz = datetimeTz = function(timezone, y, m, d, h, i, s) {
h = h || 0;
i = i || 0;
s = s || 0;
return window.timezoneJS
? new timezoneJS.Date(y, m - 1, d, h, i, s, timezone)
: new Date(y, m - 1, d, h, i, s);
};


/**
* dateutil.parser.parse
Expand Down Expand Up @@ -49,8 +59,11 @@ var assertDatesEqual = function(actual, expected, msg) {
for (var exp, act, i = 0; i < expected.length; i++) {
act = actual[i];
exp = expected[i];
equal(exp instanceof Date ? exp.toString() : exp,
act.toString(), msg + (i + 1) + '/' + expected.length);
equal(act.toUTCString(),
(exp instanceof Date) ?
exp.toUTCString() : (exp instanceof timezoneJS.Date) ?
exp.toUTCString() : exp,
msg + (i + 1) + '/' + expected.length);
}
};

Expand Down
Loading