2018-05-14 17:48:57 +00:00
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation" ;
import moment from "moment-timezone" ;
import { DATETIME_FORMATS , FORMAT_EXAMPLES } from "../lib/DateTime" ;
/**
* Translate DateTime Format operation
*/
class TranslateDateTimeFormat extends Operation {
/**
* TranslateDateTimeFormat constructor
*/
constructor () {
super ();
this . name = "Translate DateTime Format" ;
this . module = "Default" ;
this . description = "Parses a datetime string in one format and re-writes it in another.<br><br>Run with no input to see the relevant format string examples." ;
this . inputType = "string" ;
this . outputType = "html" ;
this . args = [
{
"name" : "Built in formats" ,
"type" : "populateOption" ,
"value" : DATETIME_FORMATS ,
"target" : 1
},
{
"name" : "Input format string" ,
"type" : "binaryString" ,
"value" : "DD/MM/YYYY HH:mm:ss"
},
{
"name" : "Input timezone" ,
"type" : "option" ,
"value" : [ "UTC" ]. concat ( moment . tz . names ())
},
{
"name" : "Output format string" ,
"type" : "binaryString" ,
"value" : "dddd Do MMMM YYYY HH:mm:ss Z z"
},
{
"name" : "Output timezone" ,
"type" : "option" ,
"value" : [ "UTC" ]. concat ( moment . tz . names ())
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {html}
*/
run ( input , args ) {
2018-08-16 03:01:19 +01:00
const [ inputFormat , inputTimezone , outputFormat , outputTimezone ] = args . splice ( 1 );
2018-05-14 17:48:57 +00:00
let date ;
try {
date = moment . tz ( input , inputFormat , inputTimezone );
if ( ! date || date . format () === "Invalid date" ) throw Error ;
} catch ( err ) {
2018-07-15 13:25:44 +01:00
return `Invalid format.\n\n ${ FORMAT_EXAMPLES } ` ;
2018-05-14 17:48:57 +00:00
}
return date . tz ( outputTimezone ). format ( outputFormat );
}
}
export default TranslateDateTimeFormat ;