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
|
|
|
|
|
2019-07-12 00:41:09 +03:00
|
|
|
import Noun (FromNoun, ToNoun)
|
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-02 05:51:26 +03:00
|
|
|
import Data.Time.Clock (picosecondsToDiffTime, diffTimeToPicoseconds)
|
|
|
|
import Data.Time.Clock.System (SystemTime(..), getSystemTime)
|
|
|
|
import Data.Time.Clock.System (utcToSystemTime, systemToUTCTime)
|
2019-05-08 21:47:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- Types -----------------------------------------------------------------------
|
|
|
|
|
2019-05-09 23:01:22 +03:00
|
|
|
newtype Gap = Gap { _fractoSecs :: Integer }
|
2019-07-01 07:47:21 +03:00
|
|
|
deriving newtype (Eq, Ord, Show, Num, ToNoun, FromNoun)
|
2019-05-08 21:47:20 +03:00
|
|
|
|
2019-05-09 23:01:22 +03:00
|
|
|
newtype Unix = Unix { _sinceUnixEpoch :: Gap }
|
2019-07-01 07:47:21 +03:00
|
|
|
deriving newtype (Eq, Ord, Show, ToNoun, FromNoun)
|
2019-05-08 21:47:20 +03:00
|
|
|
|
2019-05-09 23:01:22 +03:00
|
|
|
newtype Wen = Wen { _sinceUrbitEpoch :: Gap }
|
2019-07-01 07:47:21 +03:00
|
|
|
deriving newtype (Eq, Ord, Show, ToNoun, FromNoun)
|
2019-05-08 21:47:20 +03:00
|
|
|
|
|
|
|
|
2019-05-09 23:01:22 +03:00
|
|
|
-- Lenses ----------------------------------------------------------------------
|
2019-05-08 21:47:20 +03:00
|
|
|
|
2019-05-09 23:01:22 +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
|
|
|
|
|
2019-05-09 23:01:22 +03:00
|
|
|
sysUTC :: Iso' SystemTime UTCTime
|
|
|
|
sysUTC = iso systemToUTCTime utcToSystemTime
|
2019-05-08 21:47:20 +03:00
|
|
|
|
2019-05-09 23:01:22 +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
|
|
|
|
2019-05-09 23:01:22 +03:00
|
|
|
unixSystemTime :: Iso' Unix SystemTime
|
|
|
|
unixSystemTime = iso toSys fromSys
|
2019-05-08 21:47:20 +03:00
|
|
|
where
|
2019-05-09 23:01:22 +03:00
|
|
|
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
|
|
|
|
2019-05-09 23:01:22 +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
|
|
|
|
2019-05-09 23:01:22 +03:00
|
|
|
systemTime :: Iso' Wen SystemTime
|
|
|
|
systemTime = unix . unixSystemTime
|
2019-05-08 21:47:20 +03:00
|
|
|
|
|
|
|
|
2019-05-09 23:01:22 +03:00
|
|
|
--------------------------------------------------------------------------------
|
2019-05-08 21:47:20 +03:00
|
|
|
|
2019-05-09 23:01:22 +03:00
|
|
|
toDenomSecs :: Integer -> Gap -> Integer
|
|
|
|
toDenomSecs denom (Gap g) = shiftR (g * denom) 64
|
2019-05-08 21:47:20 +03:00
|
|
|
|
2019-05-09 23:01:22 +03:00
|
|
|
fromDenomSecs :: Integer -> Integer -> Gap
|
|
|
|
fromDenomSecs denom ds =
|
|
|
|
Gap $ (shiftL ds 64) `div` denom
|
2019-05-08 21:47:20 +03:00
|
|
|
|
2019-05-09 23:01:22 +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
|
|
|
|
2019-05-09 23:01:22 +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
|
|
|
|
2019-05-09 23:01:22 +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
|
|
|
|
2019-05-09 23:01:22 +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
|
2019-05-09 23:01:22 +03:00
|
|
|
now = view (from systemTime) <$> getSystemTime
|
2019-05-08 21:47:20 +03:00
|
|
|
|
|
|
|
gap :: Wen -> Wen -> Gap
|
2019-05-09 23:01:22 +03:00
|
|
|
gap (Wen x) (Wen y) | x > y = x - y
|
|
|
|
| otherwise = y - x
|
2019-05-08 21:47:20 +03:00
|
|
|
|
|
|
|
addGap :: Wen -> Gap -> Wen
|
2019-05-09 23:01:22 +03:00
|
|
|
addGap (Wen x) y = Wen (x+y)
|