chore(term): generalize termToRational by using Enum instances

This commit is contained in:
Steffen Jost 2021-09-24 12:34:27 +02:00
parent bcbaad0da8
commit 94b4ff2e7e

View File

@ -110,24 +110,16 @@ termFromText t
(~=) = (==) `on` CI.mk
termToRational :: TermIdentifier -> Rational
termToRational TermIdentifier{..} = fromInteger year + seasonOffset
termToRational TermIdentifier{..} = toRational year + seasonOffset
where
seasonOffset
| Q1 <- season = 0
| Q2 <- season = 0.25
| Q3 <- season = 0.5
| Q4 <- season = 0.75
seasonOffset = fromIntegral (fromEnum season) % fromIntegral numSeasons
termFromRational :: Rational -> TermIdentifier
termFromRational n = TermIdentifier{..}
where
year = floor n
remainder = n - fromInteger (floor n)
season
| remainder == 0 = Q1
| remainder == 0.25 = Q2
| remainder == 0.5 = Q3
| otherwise = Q4
year = floor n
remainder = n - fromInteger (floor n) -- properFraction problematic for negative year values
season = toEnum $ floor $ remainder * fromIntegral numSeasons
instance PersistField TermIdentifier where
toPersistValue = PersistRational . termToRational