mde / timezone-js

DEPRECATED: Timezone-enabled JavaScript Date object. Uses Olson zoneinfo files for timezone data.
824 stars 182 forks source link

timezone-js doesn't work with flot.time #30

Closed markus-oehme-xceptance closed 12 years ago

markus-oehme-xceptance commented 12 years ago

I want to use the new timezone-support in flot master branch (which uses timezone-js as backend). Unfortunately this doesn't work out of the box for me. I need to apply the following patch to timezone-js and then (with another patch to flot, see http://code.google.com/p/flot/issues/detail?id=713) everything works fine.

The patch seems wrong to me in the sense that I'm trampling the semantics, but it Works For Me(tm).

Here comes the patch (why dosn't github allow attachments?):

From 8d12c8bd72d91214ef595f72aeb7891dcd750749 Mon Sep 17 00:00:00 2001
From: Markus Oehme 
Date: Thu, 5 Jul 2012 15:44:12 +0200
Subject: [PATCH] Magic fix.

I don't know why it works, but it does. The handling of UTC or local ._dateProxy objects seems bogus. It should be always UTC to cause less trouble.

---
 src/date.js |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/src/date.js b/src/date.js
index a4db1af..721852d 100644
--- a/src/date.js
+++ b/src/date.js
@@ -349,7 +349,7 @@
       var dt = this._dateProxy;
       var meth = unit === 'year' ? 'FullYear' : unit.substr(0, 1).toUpperCase() + unit.substr(1);
       dt['set' + meth](n);
-      this.setFromDateObjProxy(dt);
+   this.setFromDateObjProxy(dt, true);
     },
     setUTCAttribute: function (unit, n) {
       if (isNaN(n)) { throw new Error('Units must be a number.'); }
-- 
1.7.5.4

longlho commented 12 years ago

Can you specify your use case or be more detailed? Are you trying to convert 1 date to another or initialize a timezoneJS.Date?

markus-oehme-xceptance commented 12 years ago

Ok, here we go: On the high level I create a flot graph with a time axis, which has a timezone attached to it (this only works with a development snapshot). The result is, that the time axis has no ticks because the time gets messed up.

Here is what it does in the internals: flot creates a timezoneJS.Date d [1] afterwards it does something like d.setMinutes(d.getMinutes()) which turns out to be not a nop, but shifts the time by the timezone offset. If I apply my magic fix, the code works as a nop as expected.

[1] The creation of the timezoneJS.Date is done with the following code in the official repository:

            var d = new timezoneJS.Date();
            // timezone-js is fickle, so be sure to set the time zone before
            // setting the time.
            d.setTimezone(opts.timezone);
            d.setTime(ts);
            return d;

I had to change it to

            return new timezoneJS.Date(ts, opts.timezone);

to work reliably. The latter code is what I tested against and which exhibited the behavior described above.

longlho commented 12 years ago

You can do:

var d = new timezoneJS.Date(ts, 'Etc/UTC');
d.setTimezone(opts.timezone);
return d;

It does seem to be an issue. I'll take a look at it. In the mean time you can do d.setUTCMinutes(d.getUTCMinutes())