From: Tim Wood Date: Tue, 8 Mar 2011 05:02:50 +0000 (-0800) Subject: started adding Date.add and Date.subtract X-Git-Tag: 0.3.0~2^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0e3d040194e829f3013f07aa9684809d1ae997b8;p=thirdparty%2Fmoment.git started adding Date.add and Date.subtract --- diff --git a/lib/underscore.date.js b/lib/underscore.date.js index 97371b33a..b5557f660 100644 --- a/lib/underscore.date.js +++ b/lib/underscore.date.js @@ -9,6 +9,8 @@ (function(Date, _, undefined){ + // create shortcuts to Date.prototype for minification + var dateProto = Date.prototype; // assign variables here so they can be overwritten for i18n or customization var self = this, _d, wordsMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], @@ -47,7 +49,7 @@ } // Date.prototype.humanize - function humanize(inputString) { + dateProto.humanize = function(inputString) { // shortcuts to this and getting time functions // done to save bytes in minification var self = this, @@ -190,6 +192,48 @@ return inputString.replace(charactersToReplace, replaceFunction); } + // is leap year + dateProto.isleapyear = function() { + var year = this.getFullYear(); + return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; + } + + // is daylight savings time + dateProto.isdst = function() { + var year = this.getFullYear(); + var offset = new Date(year, 0, 1).getTimezoneOffset(); + offset = Math.max(offset, new Date(year, 6, 1).getTimezoneOffset()); + return this.getTimezoneOffset() < offset; + } + + // helper function for Date.prototype.add and Date.prototype.subtract + function dateAddRemove(input, self, adding){ + var ms = input.ms, + s = input.s, + m = input.m, + h = input.h, + d = input.d, + w = input.w, + M = input.M, + y = input.y; + ms && self.addMilliseconds(ms * adding); + s && self.addSeconds(s * adding); + m && self.addMinutes(m * adding); + h && self.addHours(h * adding); + d && self.addDays(d * adding); + w && self.addWeeks(w * adding); + M && self.addMonths(M * adding); + y && self.addYears(y * adding); + } + + dateProto.add = function (input) { + dateAddRemove(input, this, 1); + }; + + dateProto.subtract = function (input) { + dateAddRemove(input, this, -1); + }; + /* * Underscore mixins */ @@ -248,9 +292,6 @@ } }; - // assign to prototype - Date.prototype.humanize = humanize; - // assign to global self._d = _d;