]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Added format token X, that will parse/print unix timestamp with ms 581/head
authorIskren Chernev <iskren.chernev@gmail.com>
Wed, 16 Jan 2013 08:22:14 +0000 (00:22 -0800)
committerIskren Chernev <iskren.chernev@gmail.com>
Thu, 24 Jan 2013 07:13:11 +0000 (23:13 -0800)
When parsing all of X, X.S, X.SS and X.SSS will parse timestamp plus
optional millisecond (1-3 digits), but when formatting will display as
expected.

moment.js
test/moment/create.js
test/moment/format.js

index 201a4d4fc605f1cd723aa9cfecc2de306ffbcc0b..eb72570e50e9c60ff828467e745c9ec55a60f5b5 100644 (file)
--- a/moment.js
+++ b/moment.js
@@ -23,7 +23,7 @@
         aspNetJsonRegex = /^\/?Date\((\-?\d+)/i,
 
         // format tokens
-        formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|YYYYY|YYYY|YY|a|A|hh?|HH?|mm?|ss?|SS?S?|zz?|ZZ?|.)/g,
+        formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|YYYYY|YYYY|YY|a|A|hh?|HH?|mm?|ss?|SS?S?|X|zz?|ZZ?|.)/g,
         localFormattingTokens = /(\[[^\[]*\])|(\\)?(LT|LL?L?L?)/g,
 
         // parsing tokens
@@ -38,6 +38,7 @@
         parseTokenWord = /[0-9]*[a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+|[\u0600-\u06FF]+\s*?[\u0600-\u06FF]+/i, // any word (or two) characters or numbers including two word month in arabic.
         parseTokenTimezone = /Z|[\+\-]\d\d:?\d\d/i, // +00:00 -00:00 +0000 -0000 or Z
         parseTokenT = /T/i, // T (ISO seperator)
+        parseTokenTimestampMs = /[\+\-]?\d+(\.\d{1,3})?/, // 123456789 123456789.123
 
         // preliminary iso regex
         // 0000-00-00 + T + 00 or 00:00 or 00:00:00 or 00:00:00.000 + +00:00 or +0000
                     b = "-";
                 }
                 return b + leftZeroFill(~~(10 * a / 6), 4);
+            },
+            X    : function () {
+                return this.unix();
             }
         };
 
         case 'a':
         case 'A':
             return parseTokenWord;
+        case 'X':
+            return parseTokenTimestampMs;
         case 'Z':
         case 'ZZ':
             return parseTokenTimezone;
         case 'SSS' :
             datePartArray[6] = ~~ (('0.' + input) * 1000);
             break;
+        // UNIX TIMESTAMP WITH MS
+        case 'X':
+            config._d = new Date(parseFloat(input) * 1000);
+            break;
         // TIMEZONE
         case 'Z' : // fall through to ZZ
         case 'ZZ' :
     function dateFromArray(config) {
         var i, date, input = [];
 
+        if (config._d) {
+            return;
+        }
+
         for (i = 0; i < 7; i++) {
             config._a[i] = input[i] = (config._a[i] == null) ? (i === 2 ? 1 : 0) : config._a[i];
         }
index 641d5dc3f68f33e72bc5b6e0dcd5dbd038eccb35..85240c6e7edacef17f56167e8a75e09457c910a9 100644 (file)
@@ -138,7 +138,8 @@ exports.create = {
                 ['HH:mm:ss SSS',        '00:30:00 123'],
                 ['HH:mm:ss S',          '00:30:00 7'],
                 ['HH:mm:ss SS',         '00:30:00 78'],
-                ['HH:mm:ss SSS',        '00:30:00 789']
+                ['HH:mm:ss SSS',        '00:30:00 789'],
+                ['X.SSS',               '1234567890.123']
             ],
             i;
 
@@ -149,6 +150,21 @@ exports.create = {
         test.done();
     },
 
+    "unix timestamp format" : function(test) {
+        var formats = ['X', 'X.S', 'X.SS', 'X.SSS'];
+
+        test.expect(formats.length * 4);
+        for (var i = 0; i < formats.length; i++) {
+            var format = formats[i];
+            test.equal(moment('1234567890',     format).valueOf(), 1234567890 * 1000,       format + " matches timestamp without milliseconds");
+            test.equal(moment('1234567890.1',   format).valueOf(), 1234567890 * 1000 + 100, format + " matches timestamp with deciseconds");
+            test.equal(moment('1234567890.12',  format).valueOf(), 1234567890 * 1000 + 120, format + " matches timestamp with centiseconds");
+            test.equal(moment('1234567890.123', format).valueOf(), 1234567890 * 1000 + 123, format + " matches timestamp with milliseconds");
+        }
+
+        test.done();
+    },
+
     "string with format no separators" : function(test) {
         moment.lang('en');
         var a = [
index 769f26b014b3af40bda33abdfd49a9fce96b5d8c..c26613dfe72b9627eceea221c2bf7399bc719bf2 100644 (file)
@@ -87,6 +87,18 @@ exports.format = {
         test.done();
     },
 
+    "unix timestamp" : function(test) {
+        test.expect(4);
+
+        var m = moment('1234567890.123', 'X');
+        test.equals(m.format('X'), '1234567890', 'unix timestamp without milliseconds');
+        test.equals(m.format('X.S'), '1234567890.1', 'unix timestamp with deciseconds');
+        test.equals(m.format('X.SS'), '1234567890.12', 'unix timestamp with centiseconds');
+        test.equals(m.format('X.SSS'), '1234567890.123', 'unix timestamp with milliseconds');
+
+        test.done();
+    },
+
     "zone" : function(test) {
         test.expect(3);