2018-05-14 19:48:57 +02: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" ;
/ * *
* Parse DateTime operation
* /
class ParseDateTime extends Operation {
/ * *
* ParseDateTime constructor
* /
constructor ( ) {
super ( ) ;
this . name = "Parse DateTime" ;
this . module = "Default" ;
this . description = "Parses a DateTime string in your specified format and displays it in whichever timezone you choose with the following information:<ul><li>Date</li><li>Time</li><li>Period (AM/PM)</li><li>Timezone</li><li>UTC offset</li><li>Daylight Saving Time</li><li>Leap year</li><li>Days in this month</li><li>Day of year</li><li>Week number</li><li>Quarter</li></ul>Run with no input to see format string examples if required." ;
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 ( ) )
}
] ;
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { html }
* /
run ( input , args ) {
const inputFormat = args [ 1 ] ,
inputTimezone = args [ 2 ] ;
let date ,
output = "" ;
try {
date = moment . tz ( input , inputFormat , inputTimezone ) ;
if ( ! date || date . format ( ) === "Invalid date" ) throw Error ;
} catch ( err ) {
2018-07-15 14:25:44 +02:00
return ` Invalid format. \n \n ${ FORMAT _EXAMPLES } ` ;
2018-05-14 19:48:57 +02:00
}
output += "Date: " + date . format ( "dddd Do MMMM YYYY" ) +
"\nTime: " + date . format ( "HH:mm:ss" ) +
"\nPeriod: " + date . format ( "A" ) +
"\nTimezone: " + date . format ( "z" ) +
"\nUTC offset: " + date . format ( "ZZ" ) +
"\n\nDaylight Saving Time: " + date . isDST ( ) +
"\nLeap year: " + date . isLeapYear ( ) +
"\nDays in this month: " + date . daysInMonth ( ) +
"\n\nDay of year: " + date . dayOfYear ( ) +
"\nWeek number: " + date . weekYear ( ) +
"\nQuarter: " + date . quarter ( ) ;
return output ;
}
}
export default ParseDateTime ;