Skip to content

Commit

Permalink
Add tsu prefix to use UTC time zone
Browse files Browse the repository at this point in the history
  • Loading branch information
maximbaz committed Oct 15, 2017
1 parent da95bea commit d40bfdc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
28 changes: 18 additions & 10 deletions src/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 27,19 @@ const OUTPUT_FORMAT = {
format: 'x'
},
ISO: {
title: 'ISO date and time',
title: 'ISO date and time ({{TIMEZONE}})',
format: ''
},
SHORT: {
title: 'Short date and time',
title: 'Short date and time ({{TIMEZONE}})',
format: 'L h:mm:ss a'
},
LONG: {
title: 'Long date and time',
title: 'Long date and time ({{TIMEZONE}})',
format: 'LLL'
},
FULL: {
title: 'Full date and time',
title: 'Full date and time ({{TIMEZONE}})',
format: 'LLLL'
}
}
Expand Down Expand Up @@ -79,23 79,31 @@ module.exports.getType = (input) => {
*
* @return momentJS
**/
module.exports.parse = (input, type) => {
module.exports.parse = (input, type, isUTC) => {
let result;

switch(type) {
case INPUT_FORMAT.NOW:
return moment()
result = moment()
break
case INPUT_FORMAT.TS_S:
return moment.unix(parseInt(input))
result = moment.unix(parseInt(input))
break
case INPUT_FORMAT.TS_MS:
return moment.unix(parseInt(input) / 1000)
result = moment.unix(parseInt(input) / 1000)
break
case INPUT_FORMAT.ARRAY:
return moment(input)
result = moment(input)
break
default:
return moment(input)
result = moment(input)
}

if (isUTC) {
result = result.utc();
}

return result;
}

/*
Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 15,16 @@ const plugin = ({ term, display, actions }) => {

//fall back on the ts prefix
if (!match) {
match = term.match(/^ts\s(.*)/);
match = term.match(/^tsu?\s(.*)/);
}

if (match) {
const isUTC = term.startsWith('tsu');
const input = match[1];

if (input) {
const type = getType(input); //String
const moment = parse(input, type); //momentJS object
const moment = parse(input, type, isUTC); //momentJS object

if (!moment.isValid()) {
display({
Expand Down Expand Up @@ -71,7 72,7 @@ const plugin = ({ term, display, actions }) => {
id: `cerebro-timestamp-${format.title}`,
icon,
clipboard: res,
subtitle: format.title,
subtitle: format.title.replace('{{TIMEZONE}}', isUTC ? 'UTC' : 'local time'),
onSelect: () => {
actions.copyToClipboard(res);
}
Expand Down

0 comments on commit d40bfdc

Please sign in to comment.