master
Raw Download raw file
 1
 2/*
 3 * TimezoneJS Shim for Sugar
 4 *
 5 * This shim creates a class DateWithTimezone that extends timezoneJS.Date
 6 * and mixes in Sugar methods to allow Sugar's internal constructor to use
 7 * a timezoneJS object instead of a native date. This means that Sugar's
 8 * Date.create, internal comparison methods etc will all use timezoneJS
 9 * objects instead.
10 *
11 * Note that the better way to do this would be to subclass the native Date
12 * class itself, however Javascript does not allow this.
13 *
14 *
15 *
16 * Usage with Sugar (example):
17 *
18 * Date.SugarNewDate = function () {
19 *   return new DateWithTimezone('America/New_York');
20 * }
21 *
22 * Also note that Olson timezone files etc need to all be loaded before using!
23 *
24 */
25(function() {
26
27  function DateWithTimezone () {
28    timezoneJS.Date.apply(this, arguments);
29  };
30
31  function mixInSugar(target) {
32    var key, m, methods = Date.SugarMethods;
33    for (key in methods) {
34      if(!methods.hasOwnProperty(key)) continue;
35      m = methods[key];
36      if(m.instance && !target[key]) {
37        target[key] = m.method;
38      }
39    };
40  }
41
42  DateWithTimezone.prototype = new timezoneJS.Date();
43  mixInSugar(DateWithTimezone.prototype);
44
45  this.DateWithTimezone = DateWithTimezone;
46
47})();