summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@cryptonomic.net>2022-09-15 23:33:47 -0400
committerAndrew Cady <d@cryptonomic.net>2022-09-15 23:36:49 -0400
commitf5645f4fa3b00c68bacd7f8ddd8da71e1430d667 (patch)
treef51319bc54b4286c99042856975c960b97f2e063
parentfab434598d0958c9cdc339822017748cc5bd2461 (diff)
fix clock
Now we use the queued time as the time to display (regardless of the clock time when it triggers). This helps prevent dropping seconds from the display when time warp is used.
-rwxr-xr-xcountdown.hs32
1 files changed, 17 insertions, 15 deletions
diff --git a/countdown.hs b/countdown.hs
index b53a3a4..a7d1377 100755
--- a/countdown.hs
+++ b/countdown.hs
@@ -12,6 +12,7 @@
12{-# language TemplateHaskell #-} 12{-# language TemplateHaskell #-}
13{-# language ViewPatterns #-} 13{-# language ViewPatterns #-}
14import Rebase.Prelude hiding (toList, on, (<+>)) 14import Rebase.Prelude hiding (toList, on, (<+>))
15import qualified Rebase.Prelude as Prelude
15import Control.Lens hiding ((<|)) 16import Control.Lens hiding ((<|))
16import Data.Foldable (toList) 17import Data.Foldable (toList)
17import Data.Ratio 18import Data.Ratio
@@ -266,21 +267,23 @@ commasF precision = prettyF cfg
266commas :: Integral i => i -> Text 267commas :: Integral i => i -> Text
267commas = prettyI (Just ',') . fromIntegral 268commas = prettyI (Just ',') . fromIntegral
268 269
269microsecondsUntil :: Integral i => UTCTime -> UTCTime -> i 270nextWholeSecond :: ZonedTime -> ZonedTime
270microsecondsUntil now later = floor $ nominalDiffTimeToSeconds $ (later `diffUTCTime` now) * 1000 * 1000 271nextWholeSecond (ZonedTime (LocalTime day (TimeOfDay h m s)) z) = (ZonedTime (LocalTime day (TimeOfDay h m s')) z)
271
272nextWholeSecond :: UTCTime -> UTCTime
273nextWholeSecond (UTCTime day dayTime) = (UTCTime day dayTime')
274 where 272 where
275 dayTime' = secondsToDiffTime $ floor dayTime + 1 273 s' = fromIntegral $ (floor s + 1 :: Int)
276 274
277threadDelayUntil :: UTCTime -> UTCTime -> IO () 275diffZonedTime :: ZonedTime -> ZonedTime -> NominalDiffTime
278threadDelayUntil now t = threadDelay $ microsecondsUntil now t 276diffZonedTime = diffLocalTime `Prelude.on` zonedTimeToLocalTime
279 277
280queueNextEvent :: MonadIO m => BChan CustomEvent -> ZonedTime -> m () 278queueNextEvent :: MonadIO m => BChan CustomEvent -> m ZonedTime
281queueNextEvent chan (zonedTimeToUTC -> now) = liftIO . void . forkIO $ do 279queueNextEvent chan = liftIO $ do
282 threadDelayUntil now (nextWholeSecond now) 280 now <- getZonedTime
283 getZonedTime >>= writeBChan chan . TimeChanged 281 void . forkIO $ do
282 let next = nextWholeSecond now
283 let delay = next `diffZonedTime` now
284 threadDelay $ floor $ delay * 1000 * 1000
285 writeBChan chan $ TimeChanged next
286 return now
284 287
285isSimulatedTime :: St -> Bool 288isSimulatedTime :: St -> Bool
286isSimulatedTime st = st ^. stDisplayTime /= st ^. stClockTime 289isSimulatedTime st = st ^. stDisplayTime /= st ^. stClockTime
@@ -307,7 +310,7 @@ handleEvent chan st e =
307 VtyEvent (V.EvKey (V.KChar 'p') []) -> cont $ st & stPaused %~ not 310 VtyEvent (V.EvKey (V.KChar 'p') []) -> cont $ st & stPaused %~ not
308 VtyEvent _ -> cont st 311 VtyEvent _ -> cont st
309 AppEvent (TimeChanged now) -> do 312 AppEvent (TimeChanged now) -> do
310 queueNextEvent chan now 313 void $ queueNextEvent chan
311 let oldTime = st ^. stClockTime 314 let oldTime = st ^. stClockTime
312 cont $ st & stClockTime .~ (zonedTimeToLocalTime now) 315 cont $ st & stClockTime .~ (zonedTimeToLocalTime now)
313 & stDisplayTime %~ if st ^. stPaused then id else (addLocalTime ((zonedTimeToLocalTime now) `diffLocalTime` oldTime)) 316 & stDisplayTime %~ if st ^. stPaused then id else (addLocalTime ((zonedTimeToLocalTime now) `diffLocalTime` oldTime))
@@ -383,6 +386,5 @@ main = do
383 386
384 let buildVty = V.mkVty V.defaultConfig 387 let buildVty = V.mkVty V.defaultConfig
385 initialVty <- buildVty 388 initialVty <- buildVty
386 now <- getZonedTime 389 now <- queueNextEvent chan
387 queueNextEvent chan now
388 void $ customMain initialVty buildVty (Just chan) (theApp chan) (initialState $ zonedTimeToLocalTime now) 390 void $ customMain initialVty buildVty (Just chan) (theApp chan) (initialState $ zonedTimeToLocalTime now)