sproutsocial / walltime-js

A JavaScript library for easily translating a UTC time to a "Wall Time" for a particular time zone.
MIT License
121 stars 12 forks source link

WallTime.toIsoString() should show the offset instead of Z #30

Closed mattjohnsonpint closed 11 years ago

mattjohnsonpint commented 11 years ago
var dt = new Date(Date.UTC(2013, 5, 10, 0, 0, 0));
var wt = WallTime.UTCToWallTime(dt, "America/New_York");

console.log(dt.toISOString());  //  2013-06-10T00:00:00.000Z
console.log(wt.toISOString());  //  2013-06-09T20:00:00.000Z 

Since these represent the same moment, shouldn't the walltime output the offset instead of the Z?

I would expect: 2013-06-09T20:00:00.000-04:00

jgable commented 11 years ago

That seems like a reasonable expectation. I'll try to fix this up and get an update out soon.

jgable commented 11 years ago

Actually, according to the MDN docs on Date.toISOString() it should always give the UTC string with a Z on the end. But, since we don't do that currently I'll still be fixing it up.

jgable commented 11 years ago

Now published as version 0.0.16 to npm, and updated in source.

mattjohnsonpint commented 11 years ago

I think this is one place you should break with the MDN specs. After all, a WallTime is not the same as a Date, so it shouldn't have to match exactly. ISO8601 dates allow either format, and so does the stricter RFC3339.

Since the intent of the library is to help with areas that aren't handled by Date, I would request that you please provide the form with the offset, rather than the Z. If someone wants the UTC timestamp, they can always go back to the underlying Date object to get it.

If, on the other hand, you prefer strict adherence to the behavior of Date, then would you please add a separate method to get the other form? perhaps toISOStringWithOffset() or similar.

jgable commented 11 years ago

I'm not going to do that. But, you are free to extend the prototype of WallTime.TimeZoneTime on your own. Ideally, that is something that a formatting library sitting on top of WallTime can provide.

mattjohnsonpint commented 11 years ago

Ok. Thanks.

mattjohnsonpint commented 11 years ago

If you are interested, here is the function I use:

WallTime.TimeZoneTime.prototype.toISOStringWithOffset = function() {
    var o = this.getTimezoneOffset();
    var hh = Math.abs(o / 60);
    var mm = Math.abs(o % 60);
    if (hh < 10) hh = '0' + hh;
    if (mm < 10) mm = '0' + mm;
    var sign = o > 0 ? '-' : '+'; // invert for iso
    var offset = sign + hh + ':' + mm;
    return this.wallTime.toISOString().slice(0, -1) + offset;
};

Feel free to add it if you want.