]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Adding utc mode #158
authorTim Wood <washwithcare@gmail.com>
Fri, 2 Mar 2012 00:00:22 +0000 (12:00 +1200)
committerTim Wood <washwithcare@gmail.com>
Fri, 2 Mar 2012 00:00:22 +0000 (12:00 +1200)
moment.js
test/moment/utc.js [new file with mode: 0644]

index fede61db091dc43347afb939061885ddcc401daa..b1eb831ef62bb91d006608fde33e5b2257deaf66 100644 (file)
--- a/moment.js
+++ b/moment.js
@@ -30,8 +30,9 @@
         shortcuts = 'Month|Date|Hours|Minutes|Seconds|Milliseconds'.split('|');
 
     // Moment prototype object
-    function Moment(date) {
+    function Moment(date, isUTC) {
         this._d = date;
+        this._isUTC = !!isUTC;
     }
 
     // left zero fill a number
@@ -89,9 +90,8 @@
     }
 
     // format date using native date object
-    function formatDate(date, inputString) {
-        var m = new Moment(date),
-            currentMonth = m.month(),
+    function formatMoment(m, inputString) {
+        var currentMonth = m.month(),
             currentDate = m.date(),
             currentYear = m.year(),
             currentDay = m.day(),
             case 'zz' :
                 // depreciating 'zz' fall through to 'z'
             case 'z' :
-                return (date.toString().match(timezoneRegex) || [''])[0].replace(nonuppercaseLetters, '');
+                return (m._d.toString().match(timezoneRegex) || [''])[0].replace(nonuppercaseLetters, '');
             case 'Z' :
                 return (currentZone < 0 ? '-' : '+') + leftZeroFill(~~(Math.abs(currentZone) / 60), 2) + ':' + leftZeroFill(~~(Math.abs(currentZone) % 60), 2);
             case 'ZZ' :
             case 'LLL' :
             case 'LLLL' :
             case 'LT' :
-                return formatDate(date, moment.longDateFormat[input]);
+                return formatMoment(m, moment.longDateFormat[input]);
             // DEFAULT
             default :
                 return input.replace(/(^\[)|(\\)|\]$/g, "");
             curScore;
         for (i = 0; i < formats.length; i++) {
             curDate = makeDateFromStringAndFormat(string, formats[i]);
-            curScore = compareArrays(inputParts, formatDate(curDate, formats[i]).match(inputCharacters));
+            curScore = compareArrays(inputParts, formatMoment(new Moment(curDate), formats[i]).match(inputCharacters));
             if (curScore < scoreToBeat) {
                 scoreToBeat = curScore;
                 output = curDate;
         return new Moment(date);
     };
 
+    // creating with utc
+    moment.utc = function(input, format) {
+        if (isArray(input)) {
+            return new Moment(new Date(Date.UTC.apply({}, input)), true);
+        }
+        return (format && input) ? moment(input + ' 0', format + ' Z').utc() : moment(input).utc();
+    };
+
     // version number
     moment.version = VERSION;
 
             return this._d;
         },
 
+        utc : function () {
+            this._isUTC = true;
+            return this;
+        },
+
+        local : function () {
+            this._isUTC = false;
+            return this;
+        },
+
         format : function (inputString) {
-            return inputString ? formatDate(this._d, inputString) :
-                formatDate(this._d, moment.defaultFormat);
+            return formatMoment(this, inputString ? inputString : moment.defaultFormat);
         },
 
         add : function (input, val) {
         },
 
         zone : function () {
-            return this._d.getTimezoneOffset();
+            return this._isUTC ? 0 : this._d.getTimezoneOffset();
         },
 
         daysInMonth : function () {
     // helper for adding shortcuts
     function makeShortcut(name, key) {
         moment.fn[name] = function (input) {
+            var utc = this._isUTC ? 'UTC' : '';
             if (input != null) {
-                this._d['set' + key](input);
+                this._d['set' + utc + key](input);
                 return this;
             } else {
-                return this._d['get' + key]();
+                return this._d['get' + utc + key]();
             }
         };
     }
diff --git a/test/moment/utc.js b/test/moment/utc.js
new file mode 100644 (file)
index 0000000..3fc6209
--- /dev/null
@@ -0,0 +1,43 @@
+var moment = require("../../moment");
+
+exports.utc = {
+    "utc and local" : function(test) {
+        test.expect(5);
+
+        var m = moment(Date.UTC(2011, 1, 2, 3, 4, 5, 6));
+        m.utc();
+        // utc
+        test.equal(m.date(), 2, "the day should be correct for utc");
+        test.equal(m.hours(), 3, "the hours should be correct for utc");
+
+        // local
+        m.local();
+        if (moment().zone() > 180) {
+            test.equal(m.date(), 1, "the day should be correct for utc");
+        } else {
+            test.equal(m.date(), 2, "the day should be correct for utc");
+        }
+        var expected = (24 + 3 - Math.floor(moment().zone() / 60)) % 24;
+        test.equal(m.hours(), expected, "the hours (" + m.hours() + ") should be correct for utc");
+        test.equal(moment().utc().zone(), 0, "timezone in utc should always be zero");
+        test.done();
+    },
+
+    "creating with utc" : function(test) {
+        test.expect(6);
+
+        var m = moment.utc([2011, 1, 2, 3, 4, 5, 6]);
+        test.equal(m.date(), 2, "the day should be correct for utc array");
+        test.equal(m.hours(), 3, "the hours should be correct for utc array");
+
+        m = moment.utc("2011-02-02 3:04:05", "YYYY-MM-DD HH:mm:ss");
+        test.equal(m.date(), 2, "the day should be correct for utc parsing format");
+        test.equal(m.hours(), 3, "the hours should be correct for utc parsing format");
+
+        m = moment.utc("2011-02-02T03:04:05+00:00");
+        test.equal(m.date(), 2, "the day should be correct for utc parsing iso");
+        test.equal(m.hours(), 3, "the hours should be correct for utc parsing iso");
+
+        test.done();
+    }
+};