Skip to content

Commit

Permalink
Add nanoseconds precision
Browse files Browse the repository at this point in the history
  • Loading branch information
maximbaz committed Oct 13, 2017
1 parent da95bea commit 3970ab1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 10,7 @@ const INPUT_FORMAT = {
NOW: 'now',
TS_S: 'ts_s',
TS_MS: 'ts_ms',
TS_NS: 'ts_ns',
ARRAY: 'array',
OTHER: 'other'
}
Expand All @@ -26,6 27,10 @@ const OUTPUT_FORMAT = {
title: 'Unix millisecond time',
format: 'x'
},
TIMESTAMP_NS: {
title: 'Unix nanosecond time',
format: 'x000000' // momentjs doesn't support ns formatting :(
},
ISO: {
title: 'ISO date and time',
format: ''
Expand Down Expand Up @@ -63,12 68,14 @@ module.exports.getType = (input) => {
}
//if this is a timestamp
else if (parseInt(input) == String(input)) {
//is it a ms or a s timestamp
// guess timestamp precision
if (String(input).length < 12) {
return INPUT_FORMAT.TS_S
}
else {
else if (String(input).length < 15) {
return INPUT_FORMAT.TS_MS
} else {
return INPUT_FORMAT.TS_NS
}
}

Expand All @@ -90,6 97,9 @@ module.exports.parse = (input, type) => {
case INPUT_FORMAT.TS_MS:
return moment.unix(parseInt(input) / 1000)
break
case INPUT_FORMAT.TS_NS:
return moment.unix(parseInt(input) / 1000000000)
break
case INPUT_FORMAT.ARRAY:
return moment(input)
break
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 39,14 @@ const plugin = ({ term, display, actions }) => {
switch (type) {
case INPUT_FORMAT.TS_S:
case INPUT_FORMAT.TS_MS:
case INPUT_FORMAT.TS_NS:
typesInOrder = [
OUTPUT_FORMAT.SHORT,
OUTPUT_FORMAT.LONG,
OUTPUT_FORMAT.FULL,
OUTPUT_FORMAT.ISO,
OUTPUT_FORMAT.TIMESTAMP_MS,
OUTPUT_FORMAT.TIMESTAMP_NS,
OUTPUT_FORMAT.TIMESTAMP
];
break;
Expand All @@ -55,6 57,7 @@ const plugin = ({ term, display, actions }) => {
typesInOrder = [
OUTPUT_FORMAT.TIMESTAMP,
OUTPUT_FORMAT.TIMESTAMP_MS,
OUTPUT_FORMAT.TIMESTAMP_NS,
OUTPUT_FORMAT.SHORT,
OUTPUT_FORMAT.LONG,
OUTPUT_FORMAT.FULL,
Expand Down

0 comments on commit 3970ab1

Please sign in to comment.