From: Remy Blom Date: Mon, 6 Oct 2014 13:11:39 +0000 (+0200) Subject: added formatToken 'x' for unix offset in milliseconds #1938 X-Git-Tag: 2.8.4~21^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8ee2ec676551cbc30121b55e11fe82e2606ea77e;p=thirdparty%2Fmoment.git added formatToken 'x' for unix offset in milliseconds #1938 --- diff --git a/moment.js b/moment.js index b25397f01..8217a7ade 100644 --- 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 @@ -271,6 +271,9 @@ zz : function () { return this.zoneName(); }, + x : function () { + return this.unixms(); + }, X : function () { return this.unix(); }, @@ -1100,6 +1103,7 @@ case 'a': case 'A': return config._locale._meridiemParse; + case 'x': case 'X': return parseTokenTimestampMs; case 'Z': @@ -1238,6 +1242,10 @@ 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); @@ -1851,6 +1859,11 @@ 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); @@ -2087,6 +2100,10 @@ return +this._d + ((this._offset || 0) * 60000); }, + unixms: function () { + return +this; + }, + unix : function () { return Math.floor(+this / 1000); }, diff --git a/test/moment/create.js b/test/moment/create.js index 7bbfc48cb..193a3678a 100644 --- a/test/moment/create.js +++ b/test/moment/create.js @@ -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'); diff --git a/test/moment/format.js b/test/moment/format.js index cf5e3e106..3252c339b 100644 --- a/test/moment/format.js +++ b/test/moment/format.js @@ -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); diff --git a/test/moment/is_valid.js b/test/moment/is_valid.js index bc16a5bda..d6670c75e 100644 --- a/test/moment/is_valid.js +++ b/test/moment/is_valid.js @@ -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');