Skip to content

Commit

Permalink
Fix int conversion in adjust (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
natefaubion authored and themattchan committed Oct 31, 2018
1 parent aa82379 commit 2355b34
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/Data/PreciseDateTime.purs
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,15 @@ adjust pd (PreciseDateTime dt ns) = do
then tenPowSix - adjustedNsInt
else adjustedNsInt

adjustedNs <- toInt inverted >>= toEnum
adjustedNs <- toEnum $ decimalToInt inverted
pure (PreciseDateTime adjustedDateTime adjustedNs)

where
zero = Decimal.fromInt 0
ten = Decimal.fromInt 10
tenPowSix = ten `pow` Decimal.fromInt 6
maxNano = tenPowSix - Decimal.fromInt 1

-- | Coerce a `Data.Decimal` to an Int, preserving precision.
toInt :: Decimal -> Maybe Int
toInt = Int.fromString <<< Decimal.toString
tenPowSix = (Decimal.fromInt 10 `pow` Decimal.fromInt 6) :: Decimal
maxNano = (tenPowSix - one) :: Decimal

-- | Coerce a `Data.Decimal` to an Int, truncating it if it is out of range
decimalToInt :: Decimal -> Int
decimalToInt = Int.floor <<< Decimal.toNumber <<< Decimal.truncated


diff :: PreciseDateTime -> PreciseDateTime -> PreciseDuration
Expand Down

0 comments on commit 2355b34

Please sign in to comment.