From: Tim Wood Date: Mon, 23 Apr 2012 17:12:03 +0000 (-0700) Subject: Merging in durations into develop branch X-Git-Tag: 1.6.0~1^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=46b168649b69689373359bca9b0f345ab0a2c1bd;p=thirdparty%2Fmoment.git Merging in durations into develop branch --- 46b168649b69689373359bca9b0f345ab0a2c1bd diff --cc moment.js index 350285cc0,f936894e7..0132338a5 --- a/moment.js +++ b/moment.js @@@ -50,12 -25,20 +50,23 @@@ ['HH:mm', /T\d\d:\d\d/], ['HH', /T\d\d/] ], - timezoneParseRegex = /([\+\-]|\d\d)/gi, - VERSION = "1.5.0", - shortcuts = 'Month|Date|Hours|Minutes|Seconds|Milliseconds'.split('|'), + + // timezone chunker "+10:00" > ["10", "00"] or "-1530" > ["-15", "30"] + parseTimezoneChunker = /([\+\-]|\d\d)/gi, + + // getter and setter names - proxyGettersAndSetters = 'Month|Date|Hours|Minutes|Seconds|Milliseconds'.split('|'); ++ proxyGettersAndSetters = 'Month|Date|Hours|Minutes|Seconds|Milliseconds'.split('|'), + durationGetters = 'years|months|days|hours|minutes|seconds|milliseconds'.split('|'), + unitMillisecondFactors = { + 'Milliseconds' : 1, + 'Seconds' : 1e3, + 'Minutes' : 6e4, + 'Hours' : 36e5, + 'Days' : 864e5, + 'Weeks' : 6048e5, + 'Months' : 2592e6, + 'Years' : 31536e6 + }; // Moment prototype object function Moment(date, isUTC) { @@@ -493,10 -489,10 +563,12 @@@ return null; } var date, -- matched; ++ matched, ++ isUTC; // parse Moment object -- if (input && input._d instanceof Date) { ++ if (moment.isMoment(input)) { date = new Date(+input._d); ++ isUTC = input._isUTC; // parse string and format } else if (format) { if (isArray(format)) { @@@ -514,7 -510,7 +586,7 @@@ typeof input === 'string' ? makeDateFromString(input) : new Date(input); } -- return new Moment(date); ++ return new Moment(date, isUTC); }; // creating with utc @@@ -801,8 -793,54 +867,54 @@@ } // add shortcut for year (uses different syntax than the getter/setter 'year' == 'FullYear') - makeShortcut('year', 'FullYear'); + makeGetterAndSetter('year', 'FullYear'); + moment.duration.fn = Duration.prototype = { + weeks : function () { + return absRound(this.days() / 7); + }, + + valueOf : function () { + return this._milliseconds + + this._days * 864e5 + + this._months * 2592e6; + }, + + humanize : function (withSuffix) { + var difference = +this, + rel = moment.relativeTime, + output = relativeTime(difference, !withSuffix); + + if (withSuffix) { + output = (difference <= 0 ? rel.past : rel.future).replace(/%s/i, output); + } + + return output; + } + }; + + function makeDurationGetter(name) { + moment.duration.fn[name] = function () { + return this._data[name]; + }; + } + + function makeDurationAsGetter(name, factor) { + moment.duration.fn['as' + name] = function () { + return +this / factor; + }; + } + + for (i = 0; i < durationGetters.length; i++) { + makeDurationGetter(durationGetters[i]); + } + + for (i in unitMillisecondFactors) { + if (unitMillisecondFactors.hasOwnProperty(i)) { + makeDurationAsGetter(i, unitMillisecondFactors[i]); + } + } + // CommonJS module is defined if (hasModule) { module.exports = moment; diff --cc test/moment/sod_eod.js index d9c14b0b1,06929bbfb..8e2cebff1 --- a/test/moment/sod_eod.js +++ b/test/moment/sod_eod.js @@@ -26,15 -26,6 +26,15 @@@ exports.eod_sod = test.equal(m.minutes(), 59, "set the minutes"); test.equal(m.seconds(), 59, "set the seconds"); test.equal(m.milliseconds(), 999, "set the seconds"); + test.done(); + }, + + "eod utc" : function(test) { + test.expect(1); + + var m2 = moment.utc(new Date(2011, 1, 2, 3, 4, 5, 6)); - test.equal(m2.eod(), m2.hours(23).minutes(59).seconds(59).milliseconds(999)); ++ test.equal(m2.eod().valueOf(), m2.hours(23).minutes(59).seconds(59).milliseconds(999).valueOf(), "Eod should equal manual hours/mins/seconds"); + test.done(); } };