shrub/pkg/hs-urbit/lib/Urbit/Time.hs

112 lines
3.1 KiB
Haskell
Raw Normal View History

2019-05-08 21:47:20 +03:00
{-# LANGUAGE NumericUnderscores, GeneralizedNewtypeDeriving #-}
2019-05-08 23:00:12 +03:00
-- TODO This is slow.
2019-05-08 21:47:20 +03:00
module Urbit.Time where
import Prelude
import Control.Lens
import Data.Bits (shiftL, shiftR)
2019-07-02 05:51:26 +03:00
import Data.Time.Clock (DiffTime, UTCTime, picosecondsToDiffTime)
import Data.Time.Clock (picosecondsToDiffTime, diffTimeToPicoseconds)
import Data.Time.Clock.System (SystemTime(..), getSystemTime)
import Data.Time.Clock.System (utcToSystemTime, systemToUTCTime)
import Noun.Poet (FromNoun, ToNoun)
2019-05-08 21:47:20 +03:00
-- Types -----------------------------------------------------------------------
newtype Gap = Gap { _fractoSecs :: Integer }
deriving newtype (Eq, Ord, Show, Num, ToNoun, FromNoun)
2019-05-08 21:47:20 +03:00
newtype Unix = Unix { _sinceUnixEpoch :: Gap }
deriving newtype (Eq, Ord, Show, ToNoun, FromNoun)
2019-05-08 21:47:20 +03:00
newtype Wen = Wen { _sinceUrbitEpoch :: Gap }
deriving newtype (Eq, Ord, Show, ToNoun, FromNoun)
2019-05-08 21:47:20 +03:00
-- Lenses ----------------------------------------------------------------------
2019-05-08 21:47:20 +03:00
makeLenses ''Gap
makeLenses ''Unix
makeLenses ''Wen
2019-05-08 21:47:20 +03:00
diffTime :: Iso' Gap DiffTime
diffTime = iso fromGap toGap
where
fromGap = picosecondsToDiffTime . view picoSecs
toGap = view (from picoSecs) . diffTimeToPicoseconds
sysUTC :: Iso' SystemTime UTCTime
sysUTC = iso systemToUTCTime utcToSystemTime
2019-05-08 21:47:20 +03:00
utcTime :: Iso' Wen UTCTime
utcTime = systemTime . sysUTC
unixEpoch :: Wen
unixEpoch = Wen (Gap 0x8000_000c_ce9e_0d80_0000_0000_0000_0000)
2019-05-08 21:47:20 +03:00
unixSystemTime :: Iso' Unix SystemTime
unixSystemTime = iso toSys fromSys
2019-05-08 21:47:20 +03:00
where
toSys (Unix gap) = MkSystemTime (fromInteger sec) (fromInteger ns)
where (sec, ns) = quotRem (gap ^. nanoSecs) 1_000_000_000
fromSys (MkSystemTime sec ns) =
Unix $ (toInteger sec ^. from secs)
+ (toInteger ns ^. from nanoSecs)
2019-05-08 21:47:20 +03:00
unix :: Iso' Wen Unix
unix = iso toUnix fromUnix
where
toUnix (Wen g) = Unix (g - unWen unixEpoch)
fromUnix (Unix g) = Wen (unWen unixEpoch + g)
unWen (Wen x) = x
2019-05-08 21:47:20 +03:00
systemTime :: Iso' Wen SystemTime
systemTime = unix . unixSystemTime
2019-05-08 21:47:20 +03:00
--------------------------------------------------------------------------------
2019-05-08 21:47:20 +03:00
toDenomSecs :: Integer -> Gap -> Integer
toDenomSecs denom (Gap g) = shiftR (g * denom) 64
2019-05-08 21:47:20 +03:00
fromDenomSecs :: Integer -> Integer -> Gap
fromDenomSecs denom ds =
Gap $ (shiftL ds 64) `div` denom
2019-05-08 21:47:20 +03:00
picoSecs :: Iso' Gap Integer
picoSecs = iso (toDenomSecs denom) (fromDenomSecs denom)
where denom = 1_000_000_000_000
2019-05-08 21:47:20 +03:00
nanoSecs :: Iso' Gap Integer
nanoSecs = iso (toDenomSecs denom) (fromDenomSecs denom)
where denom = 1_000_000_000
2019-05-08 21:47:20 +03:00
microSecs :: Iso' Gap Integer
microSecs = iso (toDenomSecs denom) (fromDenomSecs denom)
where denom = 1_000_000
2019-05-08 21:47:20 +03:00
milliSecs :: Iso' Gap Integer
milliSecs = iso (toDenomSecs denom) (fromDenomSecs denom)
where denom = 1_000
secs :: Iso' Gap Integer
secs = iso (toDenomSecs denom) (fromDenomSecs denom)
where denom = 1
2019-05-08 21:47:20 +03:00
--------------------------------------------------------------------------------
now :: IO Wen
now = view (from systemTime) <$> getSystemTime
2019-05-08 21:47:20 +03:00
gap :: Wen -> Wen -> Gap
gap (Wen x) (Wen y) | x > y = x - y
| otherwise = y - x
2019-05-08 21:47:20 +03:00
addGap :: Wen -> Gap -> Wen
addGap (Wen x) y = Wen (x+y)