diff options
author | Andrew Cady <d@cryptonomic.net> | 2022-09-15 18:37:55 -0400 |
---|---|---|
committer | Andrew Cady <d@cryptonomic.net> | 2022-09-15 18:37:55 -0400 |
commit | 99196ea09ccc07574c3b1233d7885fbc1f934d5f (patch) | |
tree | 90bf7929c9814f253efe198f7a66024942ae12ad | |
parent | 79957e34c8c8e0f3682884a5888b12a6f336f735 (diff) |
work on off-by-one errors
-rwxr-xr-x | countdown.hs | 31 |
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 | ||
86 | makeLenses ''St | 87 | makeLenses ''St |
@@ -116,11 +117,18 @@ pluralizeVerb :: Integral i => i -> String | |||
116 | pluralizeVerb 1 = "s" | 117 | pluralizeVerb 1 = "s" |
117 | pluralizeVerb _ = "" | 118 | pluralizeVerb _ = "" |
118 | 119 | ||
120 | truncateTime :: LocalTime -> LocalTime | ||
121 | truncateTime (LocalTime d (TimeOfDay h m s)) = (LocalTime d (TimeOfDay h m s')) | ||
122 | where | ||
123 | s' = fromIntegral $ floor s | ||
124 | |||
119 | drawUI :: St -> [Widget ()] | 125 | drawUI :: St -> [Widget ()] |
120 | drawUI st = [a] | 126 | drawUI 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] | |||
128 | printRemain unit quantity = printf "%d %s%s remain%s" | 136 | printRemain unit quantity = printf "%d %s%s remain%s" |
129 | 137 | ||
130 | countdownWidget :: Bool -> LocalTime -> Widget n | 138 | countdownWidget :: Bool -> LocalTime -> Widget n |
131 | countdownWidget isSimulated t = | 139 | countdownWidget 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 | ||
265 | daysInYear :: LocalTime -> Int | 280 | daysInYear :: LocalTime -> Int |