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

TimeZoneTime setters #8

Closed BenMakesGames closed 11 years ago

BenMakesGames commented 11 years ago

I'm interested in using walltime as a replacement for Date objects, to enhance other libraries. to that end, I've created several setter functions (and a toISOString function) for walltime.

I'm not very knowledgeable when it comes to github, but I'd like my work to have the potential to be useful for others; so:

  TimeZoneTime.prototype.toISOString = function()
  {
    return this.wallTime.toISOString();
  };

  // updates the UTC object, to match the walltime; should only be called internally (by the following functions); would like to be "private"
  TimeZoneTime.prototype.updateUTC = function()
  {
    this.utc = helpers.Time.WallTimeToUTC(this.offset, this.save, this.getFullYear(), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds());
  };

  TimeZoneTime.prototype.setDate = function(d) {
    this.wallTime.setUTCDate(d);
    this.updateUTC();
    return this.wallTime.getTime();
  };

  TimeZoneTime.prototype.setTime = function(ms) {
    this.wallTime.setTime(ms);
    this.updateUTC();
    return this.wallTime.getTime();
  };

  TimeZoneTime.prototype.setMonth = function(m) {
    this.wallTime.setUTCMonth(m);
    this.updateUTC();
    return this.wallTime.getTime();
  };

  TimeZoneTime.prototype.setFullYear = function(y) {
    this.wallTime.setFullYear(y);
    this.updateUTC();
    return this.wallTime.getTime();
  };

  TimeZoneTime.prototype.setHours = function(h, mi, s, ms) {
    this.wallTime.setUTCHours(h, mi, s, ms);
    this.updateUTC();
    return this.wallTime.getTime();
  };

  TimeZoneTime.prototype.setMinutes = function(mi) {
    this.wallTime.setUTCMinutes(mi);
    this.updateUTC();
    return this.wallTime.getTime();
  };

  TimeZoneTime.prototype.setSeconds = function(s) {
    this.wallTime.setUTCSeconds(s);
    this.updateUTC();
    return this.wallTime.getTime();
  };

  TimeZoneTime.prototype.setMilliseconds = function(ms) {
    this.wallTime.setUTCMilliseconds(ms);
    this.updateUTC();
    return this.wallTime.getTime();
  };
jgable commented 11 years ago

Interesting... that's a great addition. Are you sure you don't want to try and make a pull request for it? I can walk you through it, you can take as long as you need.

Here's a brief rundown...

Take your time and see if you can pull it off.

aranel616 commented 11 years ago

After you fork the repo, you can edit the files directly within GitHub, if you don't know how to clone the repo locally.

geedew commented 11 years ago

"Take your time and see if you can pull it off." Pun intended?

getaaron commented 11 years ago

The pun was obviously intended.

jgable commented 11 years ago

I take it you couldn't pull it off. I still believe in you @ThatGuyBen, but I'll try to get this integrated in by next week if I don't hear anything.

BenMakesGames commented 11 years ago

oh, sorry I haven't had time to look at this properly. it's a little hack I made for work (and am still using), and just thought it might prove useful for others; wasn't sure where else to put it. outside of work, I can't honestly say I'm interested in devoting a lot of time to it (working on other projects for fun!)... I realize that may be somewhat lazy on my part; sorry if it was inappropriate to post without having the will to carry through with unit testing and things.

jgable commented 11 years ago

The only thing I had to change was the setTime and the setFullYear. The setTime needed to recalculate rules when given a timestamp, and the setFullYear wasn't using setUTCFullYear.

Check out the relevant unit tests for more info.

Thanks again for the contribution.