From 851286bfeea3e5612446b69ac884e408dbdbe497 Mon Sep 17 00:00:00 2001 From: pingu Date: Tue, 4 Jun 2024 21:52:28 +0200 Subject: [PATCH 1/4] Fixing increase of day --- src/Model.hs | 4 ++-- src/Util.hs | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Model.hs b/src/Model.hs index d9b5c9a..e31d2be 100644 --- a/src/Model.hs +++ b/src/Model.hs @@ -40,7 +40,6 @@ import Prettyprinter ( Doc import Data.Thyme ( _localDay , _localTimeOfDay , _todHour - , _ymdDay , _zonedTimeToLocalTime , getZonedTime , gregorian @@ -57,6 +56,7 @@ import Model.Types import Model.Karen import Model.Wijkanders import Model.Linsen +import Util ( nextDay ) -- | Refreshes menus. -- The refresh function evaluates to `Some monad m => m (View model, Update signal)`, @@ -94,7 +94,7 @@ update = do let (textday, d) = if dateNow ^. _localTimeOfDay . _todHour >= nextDayHour then - ("Tomorrow", dateNow & (_localDay . gregorian . _ymdDay) %~ (+ 1)) + ("Tomorrow", dateNow & (_localDay . gregorian) %~ nextDay) else ("Today", dateNow) let day' = d ^. _localDay let karenR = fetchAndCreateRestaurant day' diff --git a/src/Util.hs b/src/Util.hs index 515d6f1..4c9858c 100644 --- a/src/Util.hs +++ b/src/Util.hs @@ -5,6 +5,7 @@ module Util where import Data.ByteString.Lazy ( ByteString ) import qualified Data.ByteString.Lazy as BL import qualified Data.Word8 as W8 +import Data.Thyme.Calendar import Text.HTML.TagSoup ( Tag , isTagText @@ -29,3 +30,12 @@ removeWhitespaceTags :: [Tag ByteString] -> [Tag ByteString] removeWhitespaceTags = filter (\t -> not (isTagText t) || tagText (not . BL.all W8.isSpace) t) +-- | Increase the date by one with and handles overflow +-- I cannot believe that this isn't in the library +nextDay :: YearMonthDay -> YearMonthDay +nextDay (YearMonthDay y m d) + | d < len = YearMonthDay y m (d + 1) + | m < 12 = YearMonthDay y (m + 1) (d - len) + | otherwise = YearMonthDay (y + 1) 1 (d - len) + where + len = gregorianMonthLength y m From 3c5785ffe46bc1e55df8df7253a6f8ad6245d318 Mon Sep 17 00:00:00 2001 From: pingu Date: Tue, 4 Jun 2024 22:39:05 +0200 Subject: [PATCH 2/4] Off by one errors are funny --- src/Util.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Util.hs b/src/Util.hs index 4c9858c..3d0030b 100644 --- a/src/Util.hs +++ b/src/Util.hs @@ -35,7 +35,7 @@ removeWhitespaceTags = nextDay :: YearMonthDay -> YearMonthDay nextDay (YearMonthDay y m d) | d < len = YearMonthDay y m (d + 1) - | m < 12 = YearMonthDay y (m + 1) (d - len) - | otherwise = YearMonthDay (y + 1) 1 (d - len) + | m < 12 = YearMonthDay y (m + 1) 1 + | otherwise = YearMonthDay (y + 1) 1 1 where len = gregorianMonthLength y m From 1a320f25636d6f6d786d18cae44156ff25a5efa5 Mon Sep 17 00:00:00 2001 From: pingu Date: Wed, 5 Jun 2024 13:10:15 +0200 Subject: [PATCH 3/4] Cleaner code ;P --- mat-chalmers.cabal | 1 + src/Model.hs | 5 ++--- src/Util.hs | 10 ---------- 3 files changed, 3 insertions(+), 13 deletions(-) diff --git a/mat-chalmers.cabal b/mat-chalmers.cabal index f60e142..f63c5e3 100644 --- a/mat-chalmers.cabal +++ b/mat-chalmers.cabal @@ -54,6 +54,7 @@ library , thyme >= 0.4 && <= 0.5 , word8 == 0.1.3 , extra >= 1.7.10 && <= 1.8 + , vector-space >= 0.16 && <0.18 executable mat-chalmers main-is: Main.hs diff --git a/src/Model.hs b/src/Model.hs index e31d2be..0b27ed0 100644 --- a/src/Model.hs +++ b/src/Model.hs @@ -37,12 +37,12 @@ import Data.Text.Lazy ( fromStrict import Prettyprinter ( Doc , pretty ) +import Data.AffineSpace ( (.+^) ) import Data.Thyme ( _localDay , _localTimeOfDay , _todHour , _zonedTimeToLocalTime , getZonedTime - , gregorian ) import Lens.Micro.Platform ( (^.) , (&) @@ -56,7 +56,6 @@ import Model.Types import Model.Karen import Model.Wijkanders import Model.Linsen -import Util ( nextDay ) -- | Refreshes menus. -- The refresh function evaluates to `Some monad m => m (View model, Update signal)`, @@ -94,7 +93,7 @@ update = do let (textday, d) = if dateNow ^. _localTimeOfDay . _todHour >= nextDayHour then - ("Tomorrow", dateNow & (_localDay . gregorian) %~ nextDay) + ("Tomorrow", dateNow & _localDay %~ (.+^ 1)) else ("Today", dateNow) let day' = d ^. _localDay let karenR = fetchAndCreateRestaurant day' diff --git a/src/Util.hs b/src/Util.hs index 3d0030b..515d6f1 100644 --- a/src/Util.hs +++ b/src/Util.hs @@ -5,7 +5,6 @@ module Util where import Data.ByteString.Lazy ( ByteString ) import qualified Data.ByteString.Lazy as BL import qualified Data.Word8 as W8 -import Data.Thyme.Calendar import Text.HTML.TagSoup ( Tag , isTagText @@ -30,12 +29,3 @@ removeWhitespaceTags :: [Tag ByteString] -> [Tag ByteString] removeWhitespaceTags = filter (\t -> not (isTagText t) || tagText (not . BL.all W8.isSpace) t) --- | Increase the date by one with and handles overflow --- I cannot believe that this isn't in the library -nextDay :: YearMonthDay -> YearMonthDay -nextDay (YearMonthDay y m d) - | d < len = YearMonthDay y m (d + 1) - | m < 12 = YearMonthDay y (m + 1) 1 - | otherwise = YearMonthDay (y + 1) 1 1 - where - len = gregorianMonthLength y m From 723a5b7e65e6d865a102c716697cc8b748eed9ff Mon Sep 17 00:00:00 2001 From: pingu Date: Wed, 5 Jun 2024 13:17:29 +0200 Subject: [PATCH 4/4] Can be one line --- src/Model.hs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Model.hs b/src/Model.hs index 0b27ed0..ea9d730 100644 --- a/src/Model.hs +++ b/src/Model.hs @@ -92,8 +92,7 @@ update = do dateNow <- liftIO $ fmap (view _zonedTimeToLocalTime) getZonedTime let (textday, d) = if dateNow ^. _localTimeOfDay . _todHour >= nextDayHour - then - ("Tomorrow", dateNow & _localDay %~ (.+^ 1)) + then ("Tomorrow", dateNow & _localDay %~ (.+^ 1)) else ("Today", dateNow) let day' = d ^. _localDay let karenR = fetchAndCreateRestaurant day'