2018-05-21 19:37:32 +02:00
/ * *
* @ author n1474335 [ n1474335 @ gmail . com ]
* @ copyright Crown Copyright 2017
* @ license Apache - 2.0
* /
2019-07-09 13:23:59 +02:00
import Operation from "../Operation.mjs" ;
2018-05-21 19:37:32 +02:00
import BigNumber from "bignumber.js" ;
2019-07-09 13:23:59 +02:00
import OperationError from "../errors/OperationError.mjs" ;
2018-05-21 19:37:32 +02:00
/ * *
* Windows Filetime to UNIX Timestamp operation
* /
class WindowsFiletimeToUNIXTimestamp extends Operation {
/ * *
* WindowsFiletimeToUNIXTimestamp constructor
* /
constructor ( ) {
super ( ) ;
this . name = "Windows Filetime to UNIX Timestamp" ;
this . module = "Default" ;
this . description = "Converts a Windows Filetime value to a UNIX timestamp.<br><br>A Windows Filetime is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 UTC.<br><br>A UNIX timestamp is a 32-bit value representing the number of seconds since January 1, 1970 UTC (the UNIX epoch).<br><br>This operation also supports UNIX timestamps in milliseconds, microseconds and nanoseconds." ;
2018-08-21 20:07:13 +02:00
this . infoURL = "https://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx" ;
2018-05-21 19:37:32 +02:00
this . inputType = "string" ;
this . outputType = "string" ;
this . args = [
{
"name" : "Output units" ,
"type" : "option" ,
"value" : [ "Seconds (s)" , "Milliseconds (ms)" , "Microseconds (μs)" , "Nanoseconds (ns)" ]
} ,
{
"name" : "Input format" ,
"type" : "option" ,
2021-02-01 17:11:39 +01:00
"value" : [ "Decimal" , "Hex (big endian)" , "Hex (little endian)" ]
2018-05-21 19:37:32 +02:00
}
] ;
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
2021-02-01 17:11:39 +01:00
const [ units , format ] = args ;
if ( ! input ) return "" ;
2020-08-24 11:39:18 +02:00
2021-02-01 17:11:39 +01:00
if ( format === "Hex (little endian)" ) {
// Swap endianness
2020-08-24 11:39:18 +02:00
let result = "" ;
for ( let i = input . length - 2 ; i >= 0 ; i -= 2 ) {
result += input . charAt ( i ) ;
result += input . charAt ( i + 1 ) ;
}
input = result ;
}
2018-05-21 19:37:32 +02:00
2021-02-01 17:11:39 +01:00
if ( format . startsWith ( "Hex" ) ) {
2018-05-21 19:37:32 +02:00
input = new BigNumber ( input , 16 ) ;
} else {
input = new BigNumber ( input ) ;
}
input = input . minus ( new BigNumber ( "116444736000000000" ) ) ;
2019-08-27 19:13:33 +02:00
if ( units === "Seconds (s)" ) {
2018-05-21 19:37:32 +02:00
input = input . dividedBy ( new BigNumber ( "10000000" ) ) ;
} else if ( units === "Milliseconds (ms)" ) {
input = input . dividedBy ( new BigNumber ( "10000" ) ) ;
} else if ( units === "Microseconds (μs)" ) {
input = input . dividedBy ( new BigNumber ( "10" ) ) ;
} else if ( units === "Nanoseconds (ns)" ) {
input = input . multipliedBy ( new BigNumber ( "100" ) ) ;
} else {
throw new OperationError ( "Unrecognised unit" ) ;
}
return input . toFixed ( ) ;
}
}
export default WindowsFiletimeToUNIXTimestamp ;