king: joe had good suggestions re time precision

This commit is contained in:
pilfer-pandex 2021-03-19 14:44:52 -04:00
parent 6fa380ddc1
commit 0e332c3259
2 changed files with 20 additions and 12 deletions

View File

@ -604,7 +604,7 @@ processWork serf maxSize q onResp spin = do
Nothing -> do
atomically (writeTVar vDone True)
Just evErr@(EvErr ev _) -> do
now <- Time.chop <$> Time.now
now <- Time.now
let cb = onResp now evErr
atomically $ modifyTVar' vInFlight (:|> (ev, cb))
sendWrit serf (WWork 0 now ev)

View File

@ -17,22 +17,22 @@ import Data.Time.Clock.System (systemToUTCTime, utcToSystemTime)
import Data.Time.LocalTime (TimeOfDay(..), timeToTimeOfDay)
import Data.Word (Word64)
import Text.Printf (printf)
import Urbit.Noun (FromNoun, ToNoun)
import Urbit.Noun (deriveToNoun, FromNoun, ToNoun(..))
-- Types -----------------------------------------------------------------------
newtype Gap = Gap { _fractoSecs :: Integer }
deriving newtype (Eq, Ord, Show, Num, ToNoun, FromNoun)
deriving newtype (Eq, Ord, Show, Num, FromNoun)
newtype Unix = Unix { _sinceUnixEpoch :: Gap }
deriving newtype (Eq, Ord, Show, ToNoun, FromNoun)
deriving newtype (Eq, Ord, Show, FromNoun)
newtype Wen = Wen { _sinceUrbitEpoch :: Gap }
deriving newtype (Eq, Ord, Show, Num, ToNoun, FromNoun)
deriving newtype (Eq, Ord, Show, Num, FromNoun)
newtype Date = MkDate { _dateWen :: Wen }
deriving newtype (Eq, Ord, Num, ToNoun, FromNoun)
deriving newtype (Eq, Ord, Num, FromNoun)
-- Record Lenses ---------------------------------------------------------------
@ -45,6 +45,20 @@ makeLenses ''Date
-- Instances -------------------------------------------------------------------
instance ToNoun Gap where
toNoun (reducePrecision -> Gap fs) = toNoun fs
-- | Produce a Gap with fewer digits after the binary point, more
-- appropriately capturing the precision our clock gives us.
reducePrecision :: Gap -> Gap
reducePrecision (Gap fs) = Gap (chop fs)
where
chop fs = shiftL (shiftR fs 32) 32
deriveToNoun ''Unix
deriveToNoun ''Wen
deriveToNoun ''Date
instance Show Date where
show (MkDate wen) = if fs == 0
then printf "~%i.%u.%u..%02u.%02u.%02u" y m d h min s
@ -149,9 +163,3 @@ gap (Wen x) (Wen y) | x > y = x - y
addGap :: Wen -> Gap -> Wen
addGap (Wen x) y = Wen (x+y)
-- | Produce a Wen with precision compatible to that from vere's time.c
chop :: Wen -> Wen
chop (Wen (Gap g)) = Wen (Gap (mop g))
where
mop n = shiftL (shiftR n 48) 48