summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@cryptonomic.net>2022-09-15 18:37:55 -0400
committerAndrew Cady <d@cryptonomic.net>2022-09-15 18:37:55 -0400
commit99196ea09ccc07574c3b1233d7885fbc1f934d5f (patch)
tree90bf7929c9814f253efe198f7a66024942ae12ad
parent79957e34c8c8e0f3682884a5888b12a6f336f735 (diff)
work on off-by-one errors
-rwxr-xr-xcountdown.hs31
1 files changed, 23 insertions, 8 deletions
diff --git a/countdown.hs b/countdown.hs
index 4d43224..e2a667e 100755
--- a/countdown.hs
+++ b/countdown.hs
@@ -81,6 +81,7 @@ data St =
81 , _stClockTime :: LocalTime 81 , _stClockTime :: LocalTime
82 , _stDisplayTime :: LocalTime 82 , _stDisplayTime :: LocalTime
83 , _stNextEvent :: Maybe UTCTime 83 , _stNextEvent :: Maybe UTCTime
84 , _stPaused :: Bool
84 } 85 }
85 86
86makeLenses ''St 87makeLenses ''St
@@ -116,11 +117,18 @@ pluralizeVerb :: Integral i => i -> String
116pluralizeVerb 1 = "s" 117pluralizeVerb 1 = "s"
117pluralizeVerb _ = "" 118pluralizeVerb _ = ""
118 119
120truncateTime :: LocalTime -> LocalTime
121truncateTime (LocalTime d (TimeOfDay h m s)) = (LocalTime d (TimeOfDay h m s'))
122 where
123 s' = fromIntegral $ floor s
124
119drawUI :: St -> [Widget ()] 125drawUI :: St -> [Widget ()]
120drawUI st = [a] 126drawUI st = [a]
121 where 127 where
122 a = -- (str $ "Last event: " <> (show $ st ^. stLastBrickEvent)) 128 a = (str $ "Last event: " <> (show $ st ^. stLastBrickEvent))
123 -- <=> 129 <=>
130 (str "\n")
131 <=>
124 (str "\n") 132 (str "\n")
125 <=> 133 <=>
126 (countdownWidget (isSimulatedTime st) $ st ^. stDisplayTime) 134 (countdownWidget (isSimulatedTime st) $ st ^. stDisplayTime)
@@ -128,7 +136,7 @@ drawUI st = [a]
128printRemain unit quantity = printf "%d %s%s remain%s" 136printRemain unit quantity = printf "%d %s%s remain%s"
129 137
130countdownWidget :: Bool -> LocalTime -> Widget n 138countdownWidget :: Bool -> LocalTime -> Widget n
131countdownWidget isSimulated t = 139countdownWidget isSimulated (truncateTime -> t) =
132 (hCenter (borderWithLabel (str $ printf "Current time%s" (if isSimulated then " (SIMULATED)" else "")) $ 140 (hCenter (borderWithLabel (str $ printf "Current time%s" (if isSimulated then " (SIMULATED)" else "")) $
133 padLeftRight 3 $ (str (formatTime defaultTimeLocale "%A, %B %e%n%Y-%m-%d %r" t)))) 141 padLeftRight 3 $ (str (formatTime defaultTimeLocale "%A, %B %e%n%Y-%m-%d %r" t))))
134 142
@@ -171,16 +179,19 @@ countdownWidget isSimulated t =
171 dayNum 179 dayNum
172 numDays 180 numDays
173 currentYear 181 currentYear
174 (commas $ toSeconds yearElapsed) 182 (commas $ 1 + toSeconds yearElapsed)
175 (commas $ toSeconds yearLength) 183 (commas $ toSeconds yearLength)
176 currentYear)) 184 currentYear))
177 where 185 where
178 cosmicYearsAgo = (realToFrac ageOfUniverseInYears) * (realToFrac $ 1 - (yearElapsed // yearLength)) :: Rational 186 cosmicYearsAgo = realToFrac ageOfUniverseInYears * (realToFrac $ 1 - (yearElapsed // yearLength)) :: Rational
187 -- cosmicYearsAgo = if yearElapsed == yearLength
188 -- then 0
189 -- else realToFrac ageOfUniverseInYears * (realToFrac $ 1 - (yearElapsed // yearLength)) :: Rational
179 currentYear = yearNumber t 190 currentYear = yearNumber t
180 dayNum = dayNumOfYear t 191 dayNum = dayNumOfYear t
181 numDays = daysInYear t 192 numDays = daysInYear t
182 yearLength = fromIntegral numDays * nominalDay 193 -- yearLength = fromIntegral numDays * nominalDay
183 -- yearLength = (zonedTimeToLocalTime $ yearEnd t) `diffLocalTime` (zonedTimeToLocalTime $ yearStart t) 194 yearLength = yearEnd t `diffLocalTime` yearStart t
184 yearElapsed = t `diffLocalTime` (yearStart t) 195 yearElapsed = t `diffLocalTime` (yearStart t)
185 196
186 daysLeft = numDays - dayNum 197 daysLeft = numDays - dayNum
@@ -233,11 +244,14 @@ handleEvent chan st e =
233 VtyEvent (V.EvKey V.KHome []) -> cont $ st & stDisplayTime .~ (st ^. stClockTime) 244 VtyEvent (V.EvKey V.KHome []) -> cont $ st & stDisplayTime .~ (st ^. stClockTime)
234 VtyEvent (V.EvKey V.KHome [MShift]) -> cont $ st & stDisplayTime .~ (newYearsEveNoon $ st ^. stClockTime) 245 VtyEvent (V.EvKey V.KHome [MShift]) -> cont $ st & stDisplayTime .~ (newYearsEveNoon $ st ^. stClockTime)
235 VtyEvent (V.EvKey V.KHome [MCtrl]) -> cont $ st & stDisplayTime .~ (newYearsEveLast10 $ st ^. stClockTime) 246 VtyEvent (V.EvKey V.KHome [MCtrl]) -> cont $ st & stDisplayTime .~ (newYearsEveLast10 $ st ^. stClockTime)
247 VtyEvent (V.EvKey V.KEnd []) -> cont $ st & stDisplayTime .~ (yearEnd $ st ^. stClockTime)
248 VtyEvent (V.EvKey (V.KChar 'p') []) -> cont $ st & stPaused %~ not
236 VtyEvent _ -> cont st 249 VtyEvent _ -> cont st
237 AppEvent (TimeChanged now) -> do 250 AppEvent (TimeChanged now) -> do
238 queueNextEvent chan now 251 queueNextEvent chan now
239 let oldTime = st ^. stClockTime 252 let oldTime = st ^. stClockTime
240 cont $ st & stClockTime .~ (zonedTimeToLocalTime now) & stDisplayTime %~ (addLocalTime ((zonedTimeToLocalTime now) `diffLocalTime` oldTime)) 253 cont $ st & stClockTime .~ (zonedTimeToLocalTime now)
254 & stDisplayTime %~ if st ^. stPaused then id else (addLocalTime ((zonedTimeToLocalTime now) `diffLocalTime` oldTime))
241 _ -> cont st 255 _ -> cont st
242 where 256 where
243 cont s = do 257 cont s = do
@@ -260,6 +274,7 @@ initialState t =
260 , _stClockTime = t 274 , _stClockTime = t
261 , _stDisplayTime = t 275 , _stDisplayTime = t
262 , _stNextEvent = Nothing 276 , _stNextEvent = Nothing
277 , _stPaused = False
263 } 278 }
264 279
265daysInYear :: LocalTime -> Int 280daysInYear :: LocalTime -> Int