Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #52 from elarkin/master

setHours, setFullYear and UTC counterparts should accept optional parameters
  • Loading branch information...
commit 582127e558c7ef4447210a3ca7284a23b6d7da6e 2 parents 3103bbd + 0a30191
@longlho longlho authored
Showing with 106 additions and 4 deletions.
  1. +82 −0 spec/date.spec.js
  2. +24 −4 src/date.js
View
82 spec/date.spec.js
@@ -233,4 +233,86 @@ describe('timezoneJS.Date', function () {
expect(dt.toString()).toEqual('2011-12-31 00:00:00');
expect(dt.getTime()).toEqual(t);
});
+
+ it("accepts an optional parameter for month in setUTCFullYear", function() {
+ var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "Etc/UTC");
+ expect(date.getUTCMonth()).toEqual(11);
+ expect(date.getUTCDate()).toEqual(31);
+ date.setUTCFullYear(2012, 1);
+ //Febuary does not have a 31st, thus we should wrap to march
+ expect(date.getUTCMonth()).toEqual(2);
+ expect(date.getUTCDate()).toEqual(2);
+ });
+
+ it("accepts a second optional parameter for date in setUTCFullYear", function() {
+ var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "Etc/UTC");
+ expect(date.getUTCMonth()).toEqual(11);
+ expect(date.getUTCDate()).toEqual(31);
+ date.setUTCFullYear(2012, 1, 1);
+ //Do not wrap to March because we are setting the date as well
+ expect(date.getUTCMonth()).toEqual(1);
+ expect(date.getUTCDate()).toEqual(1);
+ });
+
+ it("accepts an optional parameter for month in setFullYear", function() {
+ var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "America/New_York");
+ expect(date.getMonth()).toEqual(11);
+ expect(date.getDate()).toEqual(31);
+ date.setFullYear(2012, 1);
+ //Febuary does not have a 31st, thus we should wrap to march
+ expect(date.getMonth()).toEqual(2);
+ expect(date.getDate()).toEqual(2);
+ });
+
+ it("accepts a second optional parameter for date in setFullYear", function() {
+ var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "America/New_York");
+ expect(date.getMonth()).toEqual(11);
+ expect(date.getDate()).toEqual(31);
+ date.setFullYear(2012, 1, 1);
+ //Do not wrap to March because we are setting the date as well
+ expect(date.getMonth()).toEqual(1);
+ expect(date.getDate()).toEqual(1);
+ });
+
+ it("accepts an optional parameter for minutes in setUTCHours", function() {
+ var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "Etc/UTC");
+ expect(date.getUTCMinutes()).toEqual(0);
+ date.setUTCHours(0, 1);
+ expect(date.getUTCMinutes()).toEqual(1);
+ });
+
+ it("accepts a second optional parameter for seconds in setUTCHours", function() {
+ var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "Etc/UTC");
+ expect(date.getUTCSeconds()).toEqual(0);
+ date.setUTCHours(0, 1, 2);
+ expect(date.getUTCSeconds()).toEqual(2);
+ });
+
+ it("accepts an optional parameter for minutes in setUTCHours", function() {
+ var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "Etc/UTC");
+ expect(date.getUTCMilliseconds()).toEqual(0);
+ date.setUTCHours(0, 1, 2, 3);
+ expect(date.getUTCMilliseconds()).toEqual(3);
+ });
+
+ it("accepts an optional parameter for minutes in setHours", function() {
+ var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "America/New_York");
+ expect(date.getMinutes()).toEqual(0);
+ date.setHours(0, 1);
+ expect(date.getMinutes()).toEqual(1);
+ });
+
+ it("accepts a second optional parameter for seconds in setHours", function() {
+ var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "America/New_York");
+ expect(date.getSeconds()).toEqual(0);
+ date.setHours(0, 1, 2);
+ expect(date.getSeconds()).toEqual(2);
+ });
+
+ it("accepts an optional parameter for minutes in setHours", function() {
+ var date = new timezoneJS.Date(2000, 11, 31, 0, 0, 0, "America/New_York");
+ expect(date.getMilliseconds()).toEqual(0);
+ date.setHours(0, 1, 2, 3);
+ expect(date.getMilliseconds()).toEqual(3);
+ });
});
View
28 src/date.js
@@ -256,10 +256,20 @@
return dt;
},
setDate: function (n) { this.setAttribute('date', n); },
- setFullYear: function (n) { this.setAttribute('year', n); },
+ setFullYear: function (year, month, date) {
+ if (date !== undefined) { this.setAttribute('date', 1); }
+ this.setAttribute('year', year);
+ if (month !== undefined) { this.setAttribute('month', month); }
+ if (date !== undefined) { this.setAttribute('date', date); }
+ },
setMonth: function (n) { this.setAttribute('month', n); },
setYear: function (n) { this.setUTCAttribute('year', n); },
- setHours: function (n) { this.setAttribute('hours', n); },
+ setHours: function (hours, minutes, seconds, milliseconds) {
+ this.setAttribute('hours', hours);
+ if (minutes) { this.setAttribute('minutes', minutes); }
+ if (seconds) { this.setAttribute('seconds', seconds); }
+ if (milliseconds) { this.setAttribute('milliseconds', milliseconds); }
+ },
setMilliseconds: function (n) { this.setAttribute('milliseconds', n); },
setMinutes: function (n) { this.setAttribute('minutes', n); },
setSeconds: function (n) { this.setAttribute('seconds', n); },
@@ -268,8 +278,18 @@
this.setFromTimeProxy(n, this.timezone);
},
setUTCDate: function (n) { this.setUTCAttribute('date', n); },
- setUTCFullYear: function (n) { this.setUTCAttribute('year', n); },
- setUTCHours: function (n) { this.setUTCAttribute('hours', n); },
+ setUTCFullYear: function (year, month, date) {
+ if (date !== undefined) { this.setUTCAttribute('date', 1); }
+ this.setUTCAttribute('year', year);
+ if (month !== undefined) { this.setUTCAttribute('month', month); }
+ if (date !== undefined) { this.setUTCAttribute('date', date); }
+ },
+ setUTCHours: function (hours, minutes, seconds, milliseconds) {
+ this.setUTCAttribute('hours', hours);
+ if (minutes) { this.setUTCAttribute('minutes', minutes); }
+ if (seconds) { this.setUTCAttribute('seconds', seconds); }
+ if (milliseconds) { this.setUTCAttribute('milliseconds', milliseconds); }
+ },
setUTCMilliseconds: function (n) { this.setUTCAttribute('milliseconds', n); },
setUTCMinutes: function (n) { this.setUTCAttribute('minutes', n); },
setUTCMonth: function (n) { this.setUTCAttribute('month', n); },
Please sign in to comment.
Something went wrong with that request. Please try again.