]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
added formatToken 'x' for unix offset in milliseconds #1938
authorRemy Blom <remy.blom@hku.nl>
Mon, 6 Oct 2014 13:11:39 +0000 (15:11 +0200)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 17 Nov 2014 05:43:24 +0000 (21:43 -0800)
moment.js
test/moment/create.js
test/moment/format.js
test/moment/is_valid.js

index b25397f014c212f74d3cee3a72c640bc9812c1b2..8217a7ade7d1a496331580f6ea724c977ecc75ac 100644 (file)
--- a/moment.js
+++ b/moment.js
@@ -44,7 +44,7 @@
         isoDurationRegex = /^(-)?P(?:(?:([0-9,.]*)Y)?(?:([0-9,.]*)M)?(?:([0-9,.]*)D)?(?:T(?:([0-9,.]*)H)?(?:([0-9,.]*)M)?(?:([0-9,.]*)S)?)?|([0-9,.]*)W)$/,
 
         // format tokens
-        formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|Q|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|S{1,4}|X|zz?|ZZ?|.)/g,
+        formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|Q|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|S{1,4}|x|X|zz?|ZZ?|.)/g,
         localFormattingTokens = /(\[[^\[]*\])|(\\)?(LTS|LT|LL?L?L?|l{1,4})/g,
 
         // parsing token regexes
             zz : function () {
                 return this.zoneName();
             },
+            x    : function () {
+                return this.unixms();
+            },
             X    : function () {
                 return this.unix();
             },
         case 'a':
         case 'A':
             return config._locale._meridiemParse;
+        case 'x':
         case 'X':
             return parseTokenTimestampMs;
         case 'Z':
         case 'SSSS' :
             datePartArray[MILLISECOND] = toInt(('0.' + input) * 1000);
             break;
+        // UNIX OFFSET (MILLISECONDS)
+        case 'x':
+            config._d = new Date(parseInt(input, 10));
+            break;
         // UNIX TIMESTAMP WITH MS
         case 'X':
             config._d = new Date(parseFloat(input) * 1000);
         return makeMoment(c).utc();
     };
 
+    // creating with unix offset (in milliseconds)
+    moment.unixms = function (input) {
+        return moment(input);
+    };
+
     // creating with unix timestamp (in seconds)
     moment.unix = function (input) {
         return moment(input * 1000);
             return +this._d + ((this._offset || 0) * 60000);
         },
 
+        unixms: function () {
+            return +this;
+        },
+
         unix : function () {
             return Math.floor(+this / 1000);
         },
index 7bbfc48cbbe8b3cb207f7643ef090e9e1f09fa11..193a3678a831c7897ed7fd288e52bdb680bafc37 100644 (file)
@@ -259,6 +259,7 @@ exports.create = {
                 ['HH:mm:ss S',          '00:30:00 7'],
                 ['HH:mm:ss SS',         '00:30:00 78'],
                 ['HH:mm:ss SSS',        '00:30:00 789'],
+                ['x',                   '1234567890123'],
                 ['X',                   '1234567890'],
                 ['LT',                  '12:30 AM'],
                 ['LTS',                 '12:30:29 AM'],
@@ -298,6 +299,12 @@ exports.create = {
         test.done();
     },
 
+    'unix offset milliseconds' :  function (test) {
+        test.expect(1);
+        test.equal(moment('1234567890123', 'x').valueOf(), 1234567890123, 'x matches unix offset in milliseconds');
+        test.done();
+    },
+
     'milliseconds format' : function (test) {
         test.expect(5);
         test.equal(moment('1', 'S').get('ms'), 100, 'deciseconds');
index cf5e3e1067fc296c6ac34df6717a9bfaf46640d9..3252c339b4e73368bf38e0768e4a47f469192a1d 100644 (file)
@@ -128,6 +128,18 @@ exports.format = {
         test.done();
     },
 
+    'unix offset milliseconds' :  function (test) {
+        test.expect(2);
+
+        var m = moment('1234567890123', 'x');
+        test.equals(m.format('x'), '1234567890123', 'unix offset in milliseconds');
+
+        m = moment(1234567890123, 'x');
+        test.equals(m.format('x'), '1234567890123', 'unix offset in milliseconds as integer');
+
+        test.done();
+    },
+
     'zone' : function (test) {
         test.expect(3);
 
index bc16a5bdaad6eb895c28ff65e05dd58d0f96a31e..d6670c75eb7f4ed06983d19d44de1b853cdb3113 100644 (file)
@@ -225,6 +225,36 @@ exports.isValid = {
         test.done();
     },
 
+    'valid Unix offset milliseconds' : function (test) {
+        test.expect(2);
+        test.equal(moment(1234567890123, 'x').isValid(), true, 'number integer');
+        test.equal(moment('1234567890123', 'x').isValid(), true, 'string integer');
+        test.done();
+    },
+
+    'invalid Unix offset milliseconds' : function (test) {
+        test.expect(8);
+        test.equal(moment(undefined, 'x').isValid(), false, 'undefined');
+        test.equal(moment('undefined', 'x').isValid(), false, 'string undefined');
+        try {
+            test.equal(moment(null, 'x').isValid(), false, 'null');
+        } catch (e) {
+            test.ok(true, 'null');
+        }
+
+        test.equal(moment('null', 'x').isValid(), false, 'string null');
+        test.equal(moment([], 'x').isValid(), false, 'array');
+        test.equal(moment('{}', 'x').isValid(), false, 'object');
+        try {
+            test.equal(moment('', 'x').isValid(), false, 'string empty');
+        } catch (e) {
+            test.ok(true, 'string empty');
+        }
+
+        test.equal(moment(' ', 'x').isValid(), false, 'string space');
+        test.done();
+    },
+
     'empty' : function (test) {
         test.equal(moment(null).isValid(), false, 'null');
         test.equal(moment('').isValid(), false, 'empty string');