Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding milliseconds support #24

Open
wants to merge 2 commits into
base: kibana-fork
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions numeral.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,13 @@ var numeralFactory = function () {
}

function formatTime (n) {
var hours = Math.floor(Math.abs(n._value)/60/60),
minutes = Math.floor((Math.abs(n._value) - (hours * 60 * 60))/60),
seconds = Math.round(Math.abs(n._value) - (hours * 60 * 60) - (minutes * 60)),
var hours = Math.floor(Math.abs(n._value)/3600),
minutes = Math.floor((Math.abs(n._value) - (hours * 3600))/60),
seconds = Math.floor(Math.abs(n._value) - (hours * 3600) - (minutes * 60)),
milliseconds = Math.floor((n._value * 1000) % 1000)
direction = n._value < 0 ? '-' : '';
return direction + hours + ':' + ((minutes < 10) ? '0' + minutes : minutes) + ':' + ((seconds < 10) ? '0' + seconds : seconds);

return `${direction}${hours}:${padZero(minutes)}:${padZero(seconds)}.[${padZero(milliseconds, 3)}]`;
}

function unformatTime (string) {
Expand All @@ -228,12 +230,16 @@ var numeralFactory = function () {
// minutes
seconds = seconds + (Number(timeArray[1]) * 60);
// seconds
seconds = seconds + Number(timeArray[2]);
seconds = seconds + Number(timeArray[2].split(".")[0]);
//milliseconds
seconds = seconds + Number(timeArray[2].split(".")[1].substr(1, 3)) / 1000;
} else if (timeArray.length === 2) {
// minutes
seconds = seconds + (Number(timeArray[0]) * 60);
// seconds
seconds = seconds + Number(timeArray[1]);
seconds = seconds + Number(timeArray[1].split(".")[0]);
//milliseconds
seconds = seconds + Number(timeArray[1].split(".")[1].substr(1, 3)) / 1000;
}
return Number(seconds);
}
Expand Down Expand Up @@ -489,6 +495,10 @@ var numeralFactory = function () {
}
}

function padZero(value, length = 2) {
return value.toString().padStart(length, '0');
}

/************************************
Top Level Functions
************************************/
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/be-nl.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ exports['language:be-nl'] = {
['23e',23],
['€ 10 000,00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/chs.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:chs'] = {
['23.',23],
['¥10,000.00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/cs.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ exports['language:cs'] = {
['23.',23],
['10 000,00Kč',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/da-dk.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:da-dk'] = {
['23.',23],
['DK10.000,00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/de-ch.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:de-ch'] = {
['23.',23],
['CHF10 000,00',10000],
['-76%',-.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/de.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:de'] = {
['23.',23],
['€10 000,00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/en-gb.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:en-gb'] = {
['23rd',23],
['£10,000.00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ exports['language:en'] = {
['23rd',23],
['$10,000.00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/es-ES.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:es-ES'] = {
['23er',23],
['$10.000,00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:es'] = {
['23er',23],
['$10.000,00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/et.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:et'] = {
['23.',23],
['€10.000,00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/fi.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:fi'] = {
['23.',23],
['€10.000,00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/fr-CA.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:fr'] = {
['23e',23],
['10 000,00 $',10000],
['-76 %',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/fr-ch.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:fr-ch'] = {
['23e',23],
['CHF10\'000.00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/fr.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:fr'] = {
['23e',23],
['€10 000,00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/hu.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:hu'] = {
['23.',23],
['10 000,00 Ft',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/it.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:it'] = {
['23º',23],
['€10.000,00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/ja.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:ja'] = {
['23.',23],
['¥10,000.00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/nl-nl.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ exports['language:nl-nl'] = {
['23e',23],
['€ 10.000,00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/pl.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ exports['language:pl'] = {
['23.',23],
['10 000,00PLN',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/pt-br.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:pt-br'] = {
['23º',23],
['R$10.000,00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/pt-pt.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:pt-pt'] = {
['23º',23],
['€10 000,00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/ru-UA.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ exports['language:ru-UA'] = {
['23.',23],
['10 000,00₴',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/ru.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ exports['language:ru'] = {
['23.',23],
['10 000,00руб.',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/sk.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:sk'] = {
['23e',23],
['€10 000,00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/th.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:th'] = {
['23.',23],
['฿10,000.00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/tr.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exports['language:tr'] = {
['23üncü',23],
['\u20BA10.000,00',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/languages/uk-UA.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ exports['language:uk-UA'] = {
['23.',23],
['10 000,00₴',10000],
['-76%',-0.76],
['2:23:57',8637]
['2:23:57.[000]',8637]
];

for (var i = 0; i < tests.length; i++) {
Expand Down
10 changes: 5 additions & 5 deletions tests/numeral/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exports.format = {
'$0,0.00',
'0b',
'0,0%',
'00:00:00'
'00:00:00.[000]'
],
value = 12345.6,
n = numeral(value),
Expand Down Expand Up @@ -228,10 +228,10 @@ exports.format = {

times: function (test) {
var tests = [
[25,'00:00:00','0:00:25'],
[238,'00:00:00','0:03:58'],
[63846,'00:00:00','17:44:06'],
[-1,'00:00:00','-0:00:01']
[25,'00:00:00.[000]','0:00:25.[000]'],
[238,'00:00:00.[000]','0:03:58.[000]'],
[63846,'00:00:00.[000]','17:44:06.[000]'],
[-1,'00:00:00.[000]','-0:00:01.[000]']
],
i;

Expand Down
2 changes: 1 addition & 1 deletion tests/numeral/unformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ exports.unformat = {
test.expect(1);

var tests = [
['2:23:57', 8637]
['2:23:57.[012]', 8637.012]
];

for (var i = 0; i < tests.length; i++) {
Expand Down