]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Merging in durations into develop branch
authorTim Wood <washwithcare@gmail.com>
Mon, 23 Apr 2012 17:12:03 +0000 (10:12 -0700)
committerTim Wood <washwithcare@gmail.com>
Mon, 23 Apr 2012 17:12:03 +0000 (10:12 -0700)
1  2 
moment.js
test/moment/sod_eod.js

diff --cc moment.js
index 350285cc025da32ccd6dce419cbd3981346954fe,f936894e7aa5b5ce47b5018442ba6a9a3f200c07..0132338a5d8e1e6496151b3add351940e0f16811
+++ b/moment.js
              ['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) {
              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)) {
                  typeof input === 'string' ? makeDateFromString(input) :
                  new Date(input);
          }
--        return new Moment(date);
++        return new Moment(date, isUTC);
      };
  
      // creating with utc
      }
  
      // 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;
index d9c14b0b1f62e9e1c38db10da526f34314b8e759,06929bbfb57c26dd9d3f5cd15381591b7318dfec..8e2cebff13afdfebdaa295c3a96a6f753f32a5c8
@@@ -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.equal(m2.eod(), m2.hours(23).minutes(59).seconds(59).milliseconds(999));
 +        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().valueOf(), m2.hours(23).minutes(59).seconds(59).milliseconds(999).valueOf(), "Eod should equal manual hours/mins/seconds");
 +        
          test.done();
      }
  };