shrub/pkg/hs/king/lib/Urbit/Time.hs

110 lines
3.0 KiB
Haskell
Raw Normal View History

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 Control.Lens
2019-07-12 22:24:44 +03:00
import Prelude
2019-05-08 21:47:20 +03:00
import Data.Bits (shiftL, shiftR)
2019-07-12 22:18:14 +03:00
import Data.Time.Clock (DiffTime, UTCTime)
2019-07-12 22:24:44 +03:00
import Data.Time.Clock (diffTimeToPicoseconds, picosecondsToDiffTime)
2019-07-02 05:51:26 +03:00
import Data.Time.Clock.System (SystemTime(..), getSystemTime)
2019-07-12 22:24:44 +03:00
import Data.Time.Clock.System (systemToUTCTime, utcToSystemTime)
2020-01-23 05:58:22 +03:00
import Ur.Noun (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 }
2019-08-01 03:27:13 +03:00
deriving newtype (Eq, Ord, Show, Num, 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)