2018-05-14 19:48:57 +02:00
/ * *
* @ author n1474335 [ n1474335 @ gmail . com ]
* @ copyright Crown Copyright 2016
* @ license Apache - 2.0
* /
2019-07-09 13:23:59 +02:00
import Operation from "../Operation.mjs" ;
2018-05-14 19:48:57 +02:00
import moment from "moment-timezone" ;
2019-07-09 13:23:59 +02:00
import { UNITS } from "../lib/DateTime.mjs" ;
import OperationError from "../errors/OperationError.mjs" ;
2018-05-14 19:48:57 +02:00
/ * *
* From UNIX Timestamp operation
* /
class FromUNIXTimestamp extends Operation {
/ * *
* FromUNIXTimestamp constructor
* /
constructor ( ) {
super ( ) ;
this . name = "From UNIX Timestamp" ;
this . module = "Default" ;
this . description = "Converts a UNIX timestamp to a datetime string.<br><br>e.g. <code>978346800</code> becomes <code>Mon 1 January 2001 11:00:00 UTC</code><br><br>A UNIX timestamp is a 32-bit value representing the number of seconds since January 1, 1970 UTC (the UNIX epoch)." ;
2018-08-21 20:07:13 +02:00
this . infoURL = "https://wikipedia.org/wiki/Unix_time" ;
2018-05-14 19:48:57 +02:00
this . inputType = "number" ;
this . outputType = "string" ;
this . args = [
{
"name" : "Units" ,
"type" : "option" ,
"value" : UNITS
}
] ;
2020-03-24 12:06:37 +01:00
this . checks = [
{
pattern : "^1?\\d{9}$" ,
flags : "" ,
args : [ "Seconds (s)" ]
} ,
{
pattern : "^1?\\d{12}$" ,
flags : "" ,
args : [ "Milliseconds (ms)" ]
} ,
{
pattern : "^1?\\d{15}$" ,
flags : "" ,
args : [ "Microseconds (μs)" ]
} ,
{
pattern : "^1?\\d{18}$" ,
flags : "" ,
args : [ "Nanoseconds (ns)" ]
2020-02-25 12:27:03 +01:00
}
2020-03-24 12:06:37 +01:00
] ;
2018-05-14 19:48:57 +02:00
}
/ * *
* @ param { number } input
* @ param { Object [ ] } args
* @ returns { string }
2018-05-15 19:01:04 +02:00
*
* @ throws { OperationError } if invalid unit
2018-05-14 19:48:57 +02:00
* /
run ( input , args ) {
const units = args [ 0 ] ;
let d ;
input = parseFloat ( input ) ;
if ( units === "Seconds (s)" ) {
d = moment . unix ( input ) ;
return d . tz ( "UTC" ) . format ( "ddd D MMMM YYYY HH:mm:ss" ) + " UTC" ;
} else if ( units === "Milliseconds (ms)" ) {
d = moment ( input ) ;
return d . tz ( "UTC" ) . format ( "ddd D MMMM YYYY HH:mm:ss.SSS" ) + " UTC" ;
} else if ( units === "Microseconds (μs)" ) {
d = moment ( input / 1000 ) ;
return d . tz ( "UTC" ) . format ( "ddd D MMMM YYYY HH:mm:ss.SSS" ) + " UTC" ;
} else if ( units === "Nanoseconds (ns)" ) {
d = moment ( input / 1000000 ) ;
return d . tz ( "UTC" ) . format ( "ddd D MMMM YYYY HH:mm:ss.SSS" ) + " UTC" ;
} else {
2018-05-15 19:01:04 +02:00
throw new OperationError ( "Unrecognised unit" ) ;
2018-05-14 19:48:57 +02:00
}
}
}
export default FromUNIXTimestamp ;