From e10aa1b5de2c111291cda9f82268b516dd721da3 Mon Sep 17 00:00:00 2001 From: t11s Date: Thu, 8 Sep 2022 00:33:55 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20toDaysWadUnsafe/fromDaysWadUnsafe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/SignedWadMath.sol | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/utils/SignedWadMath.sol b/src/utils/SignedWadMath.sol index c743a58f..b77e1db4 100644 --- a/src/utils/SignedWadMath.sol +++ b/src/utils/SignedWadMath.sol @@ -12,6 +12,26 @@ function toWadUnsafe(uint256 x) pure returns (int256 r) { } } +/// @dev Takes an integer amount of seconds and converts it to a wad amount of days. +/// @dev Will not revert on overflow, only use where overflow is not possible. +/// @dev Not meant for negative second amounts, it assumes x is positive. +function toDaysWadUnsafe(uint256 x) pure returns (int256 r) { + assembly { + // Multiply x by 1e18 and then divide it by 86400. + r := div(mul(x, 1000000000000000000), 86400) + } +} + +/// @dev Takes a wad amount of days and converts it to an integer amount of seconds. +/// @dev Will not revert on overflow, only use where overflow is not possible. +/// @dev Not meant for negative day amounts, it assumes x is positive. +function fromDaysWadUnsafe(int256 x) pure returns (uint256 r) { + assembly { + // Multiply x by 86400 and then divide it by 1e18. + r := div(mul(x, 86400), 1000000000000000000) + } +} + /// @dev Will not revert on overflow, only use where overflow is not possible. function unsafeWadMul(int256 x, int256 y) pure returns (int256 r) { assembly {