summaryrefslogtreecommitdiff
path: root/lib/Postdelay/TimeSpec.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Postdelay/TimeSpec.hs')
-rw-r--r--lib/Postdelay/TimeSpec.hs92
1 files changed, 71 insertions, 21 deletions
diff --git a/lib/Postdelay/TimeSpec.hs b/lib/Postdelay/TimeSpec.hs
index 4a32317..9361057 100644
--- a/lib/Postdelay/TimeSpec.hs
+++ b/lib/Postdelay/TimeSpec.hs
@@ -9,6 +9,7 @@ module Postdelay.TimeSpec
9 9
10import Postdelay.TimeSpec.Utils 10import Postdelay.TimeSpec.Utils
11import Postdelay.TimeSpec.Units 11import Postdelay.TimeSpec.Units
12import Postdelay.PrioMap
12 13
13import Text.Megaparsec 14import Text.Megaparsec
14 15
@@ -19,47 +20,67 @@ import Control.Exception (IOException)
19import Control.Monad.Catch hiding (try) 20import Control.Monad.Catch hiding (try)
20import Control.Monad.RWS hiding ((<>)) 21import Control.Monad.RWS hiding ((<>))
21 22
23import Data.Functor
22import Data.Bool 24import Data.Bool
23import Data.Semigroup hiding (option) 25import Data.Semigroup hiding (option)
24import Data.Monoid (Endo(..)) 26import Data.Monoid (Endo(..))
25import Data.Foldable 27import Data.Foldable
26import Data.VectorSpace 28import Data.VectorSpace
29import Data.Maybe
27 30
28import Data.Time 31import Data.Time as Time hiding (months)
32import Data.Time.Calendar.WeekDate
29import Data.Time.Lens 33import Data.Time.Lens
30import Data.Time.Zones 34import Data.Time.Zones
31 35
32 36
37data ModPrio = Offset | Assign | Shift | Default
38 deriving (Eq, Ord, Enum, Bounded, Show)
39
40
33seconds' :: Timeable t => Lens' t Time 41seconds' :: Timeable t => Lens' t Time
34seconds' = seconds . iso (% Second) (# Second) 42seconds' = seconds . iso (% Second) (# Second)
35 43
36utcOffset :: Iso' TimeZone Time 44utcOffset :: Iso' TimeZone Time
37utcOffset = iso ((% Minute) . fromIntegral . timeZoneMinutes) (minutesToTimeZone . round . (# Minute)) 45utcOffset = iso ((% Minute) . fromIntegral . timeZoneMinutes) (minutesToTimeZone . round . (# Minute))
38 46
39shiftBack :: MonadState LocalTime m => Time -> m a -> m a 47weekDate :: Dateable t => Lens' t (Integer, Int, Int)
40shiftBack by mod = join $ shiftBack' <$> get <*> mod <*> get 48weekDate = date . iso toWeekDate (\(y, w, d) -> fromWeekDate y w d)
41 where 49
42 shiftBack' prev ret new 50shiftBack :: (MonadReader LocalTime m, MonadWriter (PrioEndo ModPrio LocalTime) m)
43 | new < prev = ret <$ (flexDT.seconds' %= (^+^) by) 51 => Time -> PrioEndo ModPrio LocalTime -> m ()
44 | otherwise = pure ret 52shiftBack by mod@(view (prioEndo._Endo) -> modE) = do
53 tell mod
54
55 prev <- ask
56 new <- asks modE
57 case new <= prev of
58 True -> scribe (prio Shift) . Just . Endo $ flexDT.seconds' %~ ((^+^) by)
59 False -> return ()
60
61mod' :: ModPrio -> ASetter' LocalTime a -> a -> PrioEndo ModPrio LocalTime
62mod' priority setter val = mempty & prio priority .~ (Just . Endo $ set setter val)
63
64scribeMod :: MonadWriter (PrioEndo ModPrio LocalTime) m => ModPrio -> ASetter' LocalTime a -> a -> m ()
65scribeMod priority setter val = tell $ mod' priority setter val
45 66
46 67
47timeSpec :: forall s m. StringParser s m => m (Endo LocalTime) 68timeSpec :: forall s m. StringParser s m => m (Endo LocalTime)
48timeSpec = label "Relative time specification" $ 69timeSpec = label "Relative time specification" $ view prioEndo <$> choice
49 choice [ flip (<>) <$> lexeme specBase <*> option mempty (offsets True) 70 [ flip (<>) <$> lexeme specBase <*> option mempty (offsets True)
50 , lexeme (string' "now") *> offsets True 71 , lexeme (string' "now") *> offsets True
51 , offsets False 72 , offsets False
52 ] 73 ]
53 where 74 where
54 specBase = toEndo <$> mkGramSepBy spaces [ timeOfDay 75 specBase = toEndo <$> mkGramSepBy spaces [ timeOfDay
55 , dateSpec 76 , dateSpec
56 ] 77 ]
57 78
58 toEndo :: [RWS LocalTime () LocalTime a] -> Endo LocalTime 79 toEndo :: [RWS LocalTime (PrioEndo ModPrio LocalTime) () ()] -> PrioEndo ModPrio LocalTime
59 toEndo (sequence -> act) = Endo $ \t -> fst $ execRWS act t t 80 toEndo (sequence -> act) = mempty & prios .@~ (\i -> Just . Endo $ \t -> maybe t (($ t) . appEndo) . view (prio i) . snd $ execRWS act t ())
60 81
61timeOfDay, dateSpec :: StringParser s m => m (RWS LocalTime () LocalTime ()) 82timeOfDay, dateSpec :: StringParser s m => m (RWS LocalTime (PrioEndo ModPrio LocalTime) () ())
62timeOfDay = label "Time of day" $ shiftBack (1 % Day) . assign time <$> choice 83timeOfDay = label "Time of day" $ withShift <$> choice
63 [ TimeOfDay 0 0 0 <$ string' "midnight" 84 [ TimeOfDay 0 0 0 <$ string' "midnight"
64 , TimeOfDay 12 0 0 <$ string' "noon" 85 , TimeOfDay 12 0 0 <$ string' "noon"
65 , TimeOfDay 16 0 0 <$ string' "teatime" 86 , TimeOfDay 16 0 0 <$ string' "teatime"
@@ -75,22 +96,51 @@ timeOfDay = label "Time of day" $ shiftBack (1 % Day) . assign time <$> choice
75 spaceConsumer 96 spaceConsumer
76 amPm <- (0 <$ string' "am") <|> (12 <$ string' "pm") 97 amPm <- (0 <$ string' "am") <|> (12 <$ string' "pm")
77 return $ TimeOfDay (h + amPm) m s 98 return $ TimeOfDay (h + amPm) m s
99 , try $ do
100 h <- hour24 False
101 m <- option 0 $ char ':' *> minute False
102 s <- option 0 $ char ':' *> second False
103 return $ TimeOfDay h m s
78 ] 104 ]
79 where 105 where
80 hour12 pad = label "Modulus 12 hour" . fmap (flip mod 12) . boundedNatural pad $ 1 <=..<= 12 106 hour12 pad = label "Modulus 12 hour" . fmap (flip mod 12) . boundedNatural pad $ 1 <=..<= 12
81 hour24 pad = label "Modulus 24 hour" . boundedNatural pad $ 0 <=..< 24 107 hour24 pad = label "Modulus 24 hour" . boundedNatural pad $ 0 <=..< 24
82 minute pad = label "Minute" . boundedNatural pad $ 0 <=..< 60 108 minute pad = label "Minute" . boundedNatural pad $ 0 <=..< 60
83 second pad = label "Second" . boundedRational pad $ 0 <=..< 61 109 second pad = label "Second" . boundedRational pad $ 0 <=..< 61
110
111 withShift :: TimeOfDay -> RWS LocalTime (PrioEndo ModPrio LocalTime) () ()
112 withShift = shiftBack (1 % Day) . mod' Assign time
84 113
85dateSpec = label "Date" $ (date <~) <$> choice 114dateSpec = label "Date" $ choice
86 [ view date <$ string' "today" 115 [ (scribeMod Assign date =<< view date) <$ string' "today"
87 , views date succ <$ string' "tomorrow" 116 , (scribeMod Assign date =<< views date succ) <$ string' "tomorrow"
117 , do
118 string' "next"
119 spaces
120 choice
121 [ string' "day" $> do
122 scribeMod Assign (flexDT.date.days) =<< views (date.days) succ
123 scribeMod Default time midnight
124 , string' "week" $> do
125 scribeMod Assign (flexDT.date.days) =<< views (date.days) (+ 7)
126 scribeMod Assign (weekDate._3) 1
127 scribeMod Default time midnight
128 , string' "month" $> do
129 scribeMod Assign (flexDT.date.months) =<< views (date.months) succ
130 scribeMod Assign (date.days) 1
131 scribeMod Default time midnight
132 , string' "year" $> do
133 scribeMod Assign (flexDT.date.years) =<< views (date.years) succ
134 scribeMod Assign (date.months) 1
135 scribeMod Assign (date.days) 1
136 scribeMod Default time midnight
137 ]
88 ] 138 ]
89 139
90offsets :: forall s m. StringParser s m 140offsets :: forall s m. StringParser s m
91 => Bool -- ^ Require sign on first offset? 141 => Bool -- ^ Require sign on first offset?
92 -> m (Endo LocalTime) 142 -> m (PrioEndo ModPrio LocalTime)
93offsets reqSgn = fmap fold $ (:) <$> offset reqSgn <*> many (offset False) 143offsets reqSgn = fmap (foldMap . review $ prioEndo' Offset) $ (:) <$> offset reqSgn <*> many (offset False)
94 where 144 where
95 asOffset :: Time -> Endo LocalTime 145 asOffset :: Time -> Endo LocalTime
96 asOffset by = Endo $ flexDT.seconds' %~ (^+^ by) 146 asOffset by = Endo $ flexDT.seconds' %~ (^+^ by)