]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
adding diff and removing 'asMilliseconds' from from
authorTim Wood <washwithcare@gmail.com>
Wed, 12 Oct 2011 05:35:05 +0000 (22:35 -0700)
committerTim Wood <washwithcare@gmail.com>
Wed, 12 Oct 2011 05:35:05 +0000 (22:35 -0700)
test/date.js
underscore.date.js

index 1b954e9e0afaa0f0d05463ac1ddf2fdbb85414cd..d24f8db0b41a43aefdcb4e3b9f0e5d1d03c728dc 100755 (executable)
@@ -172,18 +172,6 @@ test("from", 30, function() {
 });
 
 
-test("milliseconds", 5, function() {
-    equal(_date(1000).from(0, true, true), 1000, "1 second - 0 = 1000");
-    equal(_date(1000).from(500, false, true), 500, "1 second - .5 second = -500");
-    equal(_date(0).from(1000, false, true), -1000, "0 - 1 second = -1000");
-    equal(_date(new Date(1000)).from(1000, false, true), 0, "1 second - 1 second = 0");
-    var oneHourDate = new Date(),
-    nowDate = new Date();
-    oneHourDate.setHours(oneHourDate.getHours() + 1);
-    equal(_date(oneHourDate).from(nowDate, false, true), 60 * 60 * 1000, "1 hour from now = 360000");
-});
-
-
 test("suffix", 2, function() {
     equal(_date(30000).from(0), "in seconds", "prefix");
     equal(_date(0).from(30000), "seconds ago", "suffix");
@@ -196,6 +184,20 @@ test("fromNow", 2, function() {
 });
 
 
+module("diff");
+
+
+test("diff", 5, function() {
+    equal(_date(1000).diff(0), 1000, "1 second - 0 = 1000");
+    equal(_date(1000).diff(500), 500, "1 second - .5 second = -500");
+    equal(_date(0).diff(1000), -1000, "0 - 1 second = -1000");
+    equal(_date(new Date(1000)).diff(1000), 0, "1 second - 1 second = 0");
+    var oneHourDate = new Date(),
+    nowDate = new Date();
+    oneHourDate.setHours(oneHourDate.getHours() + 1);
+    equal(_date(oneHourDate).diff(nowDate), 60 * 60 * 1000, "1 hour from now = 360000");
+});
+
 module("leap year");
 
 
@@ -492,4 +494,7 @@ test("loaded module", 16, function() {
 
     // test 'test'
     testLang(test, testFrom);
+    
+    // switch back to en to prevent other tests from failing
+    _date.lang('en');
 });
\ No newline at end of file
index c7c3c90716b2339172e9dfb5ea43f7d98936c96f..b833583f392feea299bc16506f060d62173fb247 100644 (file)
         }
     });
 
-    // convert any input to milliseconds
-    function makeInputMilliseconds(input) {
-        return isNaN(input) ? new UnderscoreDate(input).date.getTime() : input;
-    }
-
     // helper function for _date.from() and _date.fromNow()
     function substituteTimeAgo(string, number) {
         return _date.relativeTime[string].replace(/%d/i, number || 1);
     }
 
-    function msApart(time, now) {
-        return makeInputMilliseconds(time) - makeInputMilliseconds(now);
-    }
-
     function relativeTime(milliseconds) {
         var seconds = Math.abs(milliseconds) / 1000,
             minutes = seconds / 60,
             return this;
         },
 
-        from : function (time, withoutSuffix, asMilliseconds) {
-            var difference = msApart(this.date, time),
-                string = difference < 0 ? _date.relativeTime.past : _date.relativeTime.future;
-            return asMilliseconds ? difference :
-                withoutSuffix ? relativeTime(difference) :
-                string.replace(/%s/i, relativeTime(difference));
+        diff : function (input, format) {
+            return this.date - _date(input, format).date;
+        },
+
+        from : function (time, withoutSuffix) {
+            var difference = this.diff(time),
+                string = difference < 0 ? _date.relativeTime.past : _date.relativeTime.future,
+                output = relativeTime(difference)
+            return withoutSuffix ? output : string.replace(/%s/i, output);
         },
 
-        fromNow : function (withoutSuffix, asMilliseconds) {
-            return this.from(new UnderscoreDate(), withoutSuffix, asMilliseconds);
+        fromNow : function (withoutSuffix) {
+            return this.from(new UnderscoreDate(), withoutSuffix);
         },
 
         isLeapYear : function () {