From 014514a4dae28e0815dad21501925c3791299dde Mon Sep 17 00:00:00 2001 From: Stefan Thomas Date: Sat, 25 Jan 2014 00:18:57 -0800 Subject: [PATCH] Add a utility function for converting back from JS time to Ripple epoch. --- src/js/ripple/utils.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/js/ripple/utils.js b/src/js/ripple/utils.js index 07eca14e..966d9d18 100644 --- a/src/js/ripple/utils.js +++ b/src/js/ripple/utils.js @@ -137,6 +137,19 @@ function toTimestamp(rpepoch) { return (rpepoch + 0x386D4380) * 1000; }; +/** + * Convert a JavaScript timestamp or Date to a Ripple epoch. + * + * JavaScript timestamps are unix epoch in milliseconds. + */ +function fromTimestamp(rpepoch) { + if (rpepoch instanceof Date) { + rpepoch = rpepoch.getTime(); + } + + return Math.round(rpepoch/1000) - 0x386D4380; +}; + exports.trace = trace; exports.arraySet = arraySet; exports.hexToString = hexToString; @@ -148,6 +161,7 @@ exports.logObject = logObject; exports.assert = assert; exports.arrayUnique = arrayUnique; exports.toTimestamp = toTimestamp; +exports.fromTimestamp = fromTimestamp; // Going up three levels is needed to escape the src-cov folder used for the // test coverage stuff.