]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Add hmm and hmmss formatting tokens
authorIskren Chernev <iskren.chernev@gmail.com>
Mon, 26 Oct 2015 08:17:31 +0000 (01:17 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 9 Nov 2015 02:34:54 +0000 (18:34 -0800)
Fixes #2418

src/lib/format/format.js
src/lib/parse/regex.js
src/lib/units/hour.js
src/test/moment/create.js
src/test/moment/format.js

index 5378170b1f4fd3cf064c11e48f7eda2f0d715dec..6c1e46d58e21602fce79ae072ea110ce769000cf 100644 (file)
@@ -1,6 +1,6 @@
 import zeroFill from '../utils/zero-fill';
 
-export var 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,9}|x|X|zz?|ZZ?|.)/g;
+export var formattingTokens = /(\[[^\[]*\])|(\\)?(hmmss|hmm|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,9}|x|X|zz?|ZZ?|.)/g;
 
 var localFormattingTokens = /(\[[^\[]*\])|(\\)?(LTS|LT|LL?L?L?|l{1,4})/g;
 
index b1ace27634cf524198ef7259f970c2bc890ec525..9541f039c1ce7203e2d8a1af77e7eeb1c9dc3fd8 100644 (file)
@@ -4,6 +4,8 @@ export var match3         = /\d{3}/;         //     000 - 999
 export var match4         = /\d{4}/;         //    0000 - 9999
 export var match6         = /[+-]?\d{6}/;    // -999999 - 999999
 export var match1to2      = /\d\d?/;         //       0 - 99
+export var match3to4      = /\d\d\d\d?/;     //     999 - 9999
+export var match5to6      = /\d\d\d\d\d\d?/; //   99999 - 999999
 export var match1to3      = /\d{1,3}/;       //       0 - 999
 export var match1to4      = /\d{1,4}/;       //       0 - 9999
 export var match1to6      = /[+-]?\d{1,6}/;  // -999999 - 999999
index 356c74d234747a6868a9b67c9245b66317d2a2c9..a217573004c96e3a265b1a370c914a430daca7ea 100644 (file)
@@ -1,17 +1,29 @@
 import { makeGetSet } from '../moment/get-set';
 import { addFormatToken } from '../format/format';
 import { addUnitAlias } from './aliases';
-import { addRegexToken, match1to2, match2 } from '../parse/regex';
+import { addRegexToken, match1to2, match2, match3to4, match5to6 } from '../parse/regex';
 import { addParseToken } from '../parse/token';
-import { HOUR } from './constants';
+import { HOUR, MINUTE, SECOND } from './constants';
 import toInt from '../utils/to-int';
+import zeroFill from '../utils/zero-fill';
 import getParsingFlags from '../create/parsing-flags';
 
 // FORMATTING
 
-addFormatToken('H', ['HH', 2], 0, 'hour');
-addFormatToken('h', ['hh', 2], 0, function () {
+function hFormat() {
     return this.hours() % 12 || 12;
+}
+
+addFormatToken('H', ['HH', 2], 0, 'hour');
+addFormatToken('h', ['hh', 2], 0, hFormat);
+
+addFormatToken('hmm', 0, 0, function () {
+    return '' + hFormat.apply(this) + zeroFill(this.minutes(), 2);
+});
+
+addFormatToken('hmmss', 0, 0, function () {
+    return '' + hFormat.apply(this) + zeroFill(this.minutes(), 2) +
+        zeroFill(this.seconds(), 2);
 });
 
 function meridiem (token, lowercase) {
@@ -40,6 +52,9 @@ addRegexToken('h',  match1to2);
 addRegexToken('HH', match1to2, match2);
 addRegexToken('hh', match1to2, match2);
 
+addRegexToken('hmm', match3to4);
+addRegexToken('hmmss', match5to6);
+
 addParseToken(['H', 'HH'], HOUR);
 addParseToken(['a', 'A'], function (input, array, config) {
     config._isPm = config._locale.isPM(input);
@@ -49,6 +64,20 @@ addParseToken(['h', 'hh'], function (input, array, config) {
     array[HOUR] = toInt(input);
     getParsingFlags(config).bigHour = true;
 });
+addParseToken('hmm', function (input, array, config) {
+    var pos = input.length - 2;
+    array[HOUR] = toInt(input.substr(0, pos));
+    array[MINUTE] = toInt(input.substr(pos));
+    getParsingFlags(config).bigHour = true;
+});
+addParseToken('hmmss', function (input, array, config) {
+    var pos1 = input.length - 4;
+    var pos2 = input.length - 2;
+    array[HOUR] = toInt(input.substr(0, pos1));
+    array[MINUTE] = toInt(input.substr(pos1, 2));
+    array[SECOND] = toInt(input.substr(pos2));
+    getParsingFlags(config).bigHour = true;
+});
 
 // LOCALES
 
index 425c47ed10451eb249c1b0531b181b4e5884e81d..7d4670a1e404a24d8e2cc34e93b499dc952bae52 100644 (file)
@@ -1000,3 +1000,20 @@ test('milliseconds', function (assert) {
     assert.equal(moment('12345678', 'SSSSSSSS').millisecond(), 123);
     assert.equal(moment('123456789', 'SSSSSSSSS').millisecond(), 123);
 });
+
+test('hmm', function (assert) {
+    assert.equal(moment('123', 'hmm', true).format('HH:mm:ss'), '01:23:00', '123 with hmm');
+    assert.equal(moment('123a', 'hmmA', true).format('HH:mm:ss'), '01:23:00', '123a with hmmA');
+    assert.equal(moment('123p', 'hmmA', true).format('HH:mm:ss'), '13:23:00', '123p with hmmA');
+
+    assert.equal(moment('1234', 'hmm', true).format('HH:mm:ss'), '12:34:00', '1234 with hmm');
+    assert.equal(moment('1234a', 'hmmA', true).format('HH:mm:ss'), '00:34:00', '1234a with hmmA');
+    assert.equal(moment('1234p', 'hmmA', true).format('HH:mm:ss'), '12:34:00', '1234p with hmmA');
+
+    assert.equal(moment('12345', 'hmmss', true).format('HH:mm:ss'), '01:23:45', '12345 with hmmss');
+    assert.equal(moment('12345a', 'hmmssA', true).format('HH:mm:ss'), '01:23:45', '12345a with hmmssA');
+    assert.equal(moment('12345p', 'hmmssA', true).format('HH:mm:ss'), '13:23:45', '12345p with hmmssA');
+    assert.equal(moment('112345', 'hmmss', true).format('HH:mm:ss'), '11:23:45', '112345 with hmmss');
+    assert.equal(moment('112345a', 'hmmssA', true).format('HH:mm:ss'), '11:23:45', '112345a with hmmssA');
+    assert.equal(moment('112345p', 'hmmssA', true).format('HH:mm:ss'), '23:23:45', '112345p with hmmssA');
+});
index 016f6d17f7176d2c78c7d2043ec602703ef098f0..f12a62ce64af053a53c6e96bdcd6bae5d531042e 100644 (file)
@@ -371,3 +371,13 @@ test('milliseconds', function (assert) {
     assert.equal(m.format('SSSSSSSS'), '12300000');
     assert.equal(m.format('SSSSSSSSS'), '123000000');
 });
+
+test('hmm and hmmss', function (assert) {
+    assert.equal(moment('12:34:56', 'HH:mm:ss').format('hmm'), '1234');
+    assert.equal(moment('01:34:56', 'HH:mm:ss').format('hmm'), '134');
+    assert.equal(moment('13:34:56', 'HH:mm:ss').format('hmm'), '134');
+
+    assert.equal(moment('12:34:56', 'HH:mm:ss').format('hmmss'), '123456');
+    assert.equal(moment('01:34:56', 'HH:mm:ss').format('hmmss'), '13456');
+    assert.equal(moment('13:34:56', 'HH:mm:ss').format('hmmss'), '13456');
+});